· 4 years ago · Sep 03, 2021, 06:58 PM
1import discord
2from discord.ext import commands
3import json
4import sqlite3
5from datetime import *
6import pytz
7
8with open('config.json','r')as configuration:
9 config = json.load(configuration)
10
11
12
13
14
15bot = commands.Bot(command_prefix=config["prefix"],intents=discord.Intents.all())
16
17
18@bot.event
19async def on_ready():
20 print(discord.__version__)
21 print(str(bot.latency).split(".")[0])
22 print(bot.user.name)
23 sqlite3.connect('global.db')
24 connection = sqlite3.connect('global.db')
25 cursor = connection.cursor()
26 sql = "CREATE TABLE IF NOT EXISTS globalchat(guildid BIGINT, channelid BIGINT)"
27 cursor.execute(sql)
28
29
30
31
32def guild_exists(guildid):
33 con = sqlite3.connect('global.db')
34 cursor = con.cursor()
35 sql = f"SELECT guildid FROM globalchat WHERE guildid = {int(guildid)}"
36 cursor.execute(sql)
37 x = 0
38 for satz in cursor:
39 x+=1
40 if x == 1:
41 return True
42 return False
43
44
45
46
47
48@bot.command()
49@commands.has_permissions(manage_channels=True)
50async def addglobal(ctx):
51 con = sqlite3.connect('global.db')
52 cursor = con.cursor()
53 time = pytz.timezone('Europe/Berlin')
54 sql = "CREATE TABLE IF NOT EXISTS globalchat ("\
55 "guildid BIGINT,"\
56 "channelid BIGINT)"
57 cursor.execute(sql)
58 if guild_exists(ctx.guild.id) == True:
59 await ctx.send("Du hast bereits einen Globalchat!")
60 con.close()
61 else:
62 sql = f"INSERT INTO globalchat VALUES({ctx.guild.id}, {ctx.channel.id})"
63 cursor.execute(sql)
64 con.commit()
65 embed = discord.Embed(title="Globalchat wurde hier gesetzt!",description=f"Willkommen im Globalchat {ctx.guild.name}",color=discord.Color.dark_theme())
66 await ctx.send(embed=embed)
67 embed = discord.Embed(title=f"{ctx.guild.name} ist dem Globalchat beigetreten!",color=discord.Color.dark_theme(),timestamp=datetime.now().astimezone(tz=time))
68 embed.set_author(name=ctx.guild.name)
69 embed.set_thumbnail(url=ctx.guild.icon_url)
70 sql = "SELECT channelid FROM globalchat"
71 cursor.execute(sql)
72 for satz in cursor:
73 channel = bot.get_channel(satz[0])
74 await channel.send(embed=embed)
75 print("New Globalchat")
76 con.close()
77
78@bot.command()
79@commands.has_permissions(manage_channels=True)
80async def removeglobal(ctx):
81 if guild_exists(ctx.guild.id) == True:
82 con = sqlite3.connect('global.db')
83 cursor = con.cursor()
84 sql = f"DELETE FROM globalchat WHERE guildid = {int(ctx.guild.id)}"
85 cursor.execute(sql)
86 con.commit()
87 embed = discord.Embed(title="Der Globalchat wurde Deaktiviert",description="Danke das du mich Benutzt hast!",color=discord.Color.dark_theme())
88 await ctx.send(embed=embed)
89 else:
90 await ctx.send("Du hast keinen Globalchat!")
91
92@bot.event
93async def on_message(message):
94 if message.author == bot.user:
95 return
96 print(message.author.name + ": " + message.content)
97 con = sqlite3.connect('global.db')
98 cursor = con.cursor()
99 if not message.content.startswith("/"):
100 time = pytz.timezone('Europe/Berlin')
101 sql = f"SELECT channelid FROM globalchat WHERE guildid = {message.guild.id}"
102 cursor.execute(sql)
103 for satz in cursor:
104 if satz[0] == message.channel.id:
105 sql = "SELECT channelid FROM globalchat"
106 cursor.execute(sql)
107 for satz in cursor:
108 await message.channel.purge(limit=1, check = lambda m: m.author == message.author)
109 channel = bot.get_channel(satz[0])
110 embed = discord.Embed(color=discord.Color.dark_theme())
111 embed.add_field(name=f"Guild:", value =f"{message.guild.name}")
112 embed.add_field(name="Nachricht: ",value=message.content)
113 embed.set_author(name= f"Author: {str(message.author)}",icon_url=message.guild.icon_url)
114 embed.set_thumbnail(url=message.author.avatar_url)
115 await channel.send(embed=embed)
116
117 await bot.process_commands(message)
118
119bot.run(config["token"])