· 5 years ago · Aug 22, 2020, 10:42 PM
1import discord
2from discord.ext import commands
3import sqlite3
4
5global db, cursor
6db = sqlite3.connect('./db/welcome.db')
7cursor = db.cursor()
8
9
10class Config(commands.Cog):
11 def __init__(self, client):
12 self.client = client
13
14 cursor.execute("""
15 CREATE TABLE IF NOT EXISTS welcome(
16 guild_id TEXT,
17 msg TEXT,
18 channel_id TEXT
19 )
20 """)
21
22 @commands.Cog.listener()
23 async def on_member_join(self, member):
24 db = sqlite3.connect('./db/welcome.db')
25 cursor = db.cursor()
26 cursor.execute(f'SELECT channel_id FROM welcome WHERE guild_id = {member.guild.id}')
27 result = cursor.fetchone()
28
29 if result is None:
30 return
31
32 else:
33 cursor.execute(f'SELECT msg FROM welcome WHERE guild_id = {member.guild.id}')
34 result1 = cursor.fetchone()
35 members = len(list(member.guild.members))
36 user = member.name
37 mention = member.mention
38 guild = member.guild
39
40 channel = self.client.get_channel(id=int(result[0]))
41 await channel.send(str(result1[0]).format(members=members, mention=mention, user=user, guild=guild))
42
43
44 @commands.group(brief="Type `z.welcome` to set up a welcome message for new members", invoke_without_command=True)
45 async def welcome(self, ctx):
46 await ctx.send("Setup:\nz.welcome setchannel #channel\nz.welcome setmessage <msg>\n \nExample:\nz.welcome setchannel #welcome\nz.welcome setmessage Hey {mention} welcome to {guild}, you’re the {members}th member!")
47
48 @welcome.command(brief="`z.setchannel #channel`")
49 async def setchannel(self, ctx, channel: discord.TextChannel):
50 if ctx.author.guild_permissions.manage_guild:
51 db = sqlite3.connect('./db/welcome.db')
52 cursor = db.cursor()
53 cursor.execute(f'SELECT channel_id FROM welcome WHERE guild_id = {ctx.guild.id}')
54 result = cursor.fetchone()
55
56
57 if result is None:
58 sql = ('INSERT INTO welcome(guild_id, channel_id) values(?,?)')
59 val = (ctx.guild.id, channel.id)
60 await ctx.send(f"The channel has been set to {channel.mention}")
61
62 if result is not None:
63 sql = ('UPDATE welcome SET channel_id = ? WHERE guild_id = ?')
64 val = (channel.id, ctx.guild.id)
65 await ctx.send(f"The channel has been set to {channel.mention}")
66
67 cursor.execute(sql, val)
68 db.commit()
69 cursor.close()
70 db.close()
71
72 else:
73 embed = discord.Embed(color=0xff0200)
74 embed.set_author(name="Invalid argument")
75 embed.add_field(name="You're missing the following permission:", value="```Manage Guild```")
76 await ctx.send(embed=embed)
77
78
79
80 @welcome.command(brief="`z.setmessage {message}`")
81 async def setmessage(self, ctx, *, message):
82 if ctx.author.guild_permissions.manage_guild:
83 db = sqlite3.connect('./db/welcome.db')
84 cursor = db.cursor()
85 cursor.execute(f'SELECT msg FROM welcome WHERE guild_id = {ctx.guild.id}')
86 result = cursor.fetchone()
87
88 if result is None:
89 sql = ('INSERT INTO welcome(guild_id, msg) values(?,?)')
90 val = (ctx.guild.id, message)
91 await ctx.send(f'The message has been set to {message}')
92
93 if result is not None:
94 sql = ('UPDATE welcome SET msg = ? WHERE guild_id = ?')
95 val = (message, ctx.guild.id)
96 await ctx.send(f"The message has been set to {message}")
97
98 cursor.execute(sql, val)
99 db.commit()
100 cursor.close()
101 db.close()
102
103 else:
104 embed = discord.Embed(color=0xff0200)
105 embed.set_author(name="Invalid argument")
106 embed.add_field(name="You're missing the following permission:", value="```Manage Guild```")
107 await ctx.send(embed=embed)
108
109def setup(client):
110 client.add_cog(Config(client))