· 4 years ago · Feb 03, 2021, 03:48 AM
1import discord
2from discord.ext import commands
3import asyncio
4import datetime
5import sqlite3
6
7db = sqlite3.connect('welcome.sqlite')
8cursor = db.cursor()
9cursor.execute('''
10 CREATE TABLE IF NOT EXISTS main(
11 guild_id TEXT,
12 msg TEXT,
13 channel_id TEXT
14 )
15''')
16result = cursor.fetchone()
17
18class Welcome(commands.Cog):
19
20 def __init__(self, client):
21 self.client = client
22
23 @commands.Cog.listener()
24 async def on_member_join(self, member):
25 db = sqlite3.connect('welcome.sqlite')
26 cursor = db.cursor()
27 cursor.execute(f'SELECT channel_id FROM main WHERE guild_id = {member.guild.id}')
28 result = cursor.fetchone()
29
30 if result is None:
31 return
32 else:
33 cursor.execute(f'SELECT msg FROM main WHERE guild_id = {member.guild.id}')
34 result1 = cursor.fetchone()
35 members = len(list(member.guild.members))
36 mention = member.mention
37 user = member.name
38 guild = member.guild
39 embed = discord.Embed(color=discord.Color.from_rgb(253, 254, 255), description=str(result1[0]).format(members=members, mention=mention, user=user, guild=guild))
40
41 embed.set_thumbnail(url=f'{member.avatar_url}')
42 embed.set_author(name='New Member!')
43 embed.set_footer(text=f'{member.guild}', icon_url=f'{member.guild.icon_url}')
44
45 embed.timestamp = datetime.datetime.utcnow()
46
47 channel = self.client.get_channel(id=int(result[0]))
48
49 await channel.send(embed=embed)
50
51 @commands.group(invoke_without_command=True)
52 async def welcome(self, ctx):
53 await ctx.send('Setup Commands Avaliable:\nwelcome channel <#channel>\nwelcome text <message>\n`{members} shows member count | {mention} mentions the user | {user} states the users name | {guild} states the guild name`')
54
55 @welcome.command()
56 async def channel(self, ctx, channel:discord.TextChannel):
57 if ctx.message.author.guild_permissions.manage_messages:
58 db = sqlite3.connect('welcome.sqlite')
59 cursor = db.cursor()
60 cursor.execute(f'SELECT channel_id FROM main WHERE guild_id = {ctx.guild.id}')
61 result = cursor.fetchone()
62 if result is None:
63 sql = ("INSERT INTO main(guild_id, channel_id) VALUES(?,?)")
64 val = (ctx.guild.id, channel.id)
65 await ctx.send(f"Channel has been set to {channel.mention}")
66 elif result is not None:
67 sql = ("UPDATE main SET channel_id = ? WHERE guild_id = ?")
68 val = (channel.id, ctx.guild.id)
69 await ctx.send(f"Channel has been updated to {channel.mention}")
70 cursor.execute(sql, val)
71 db.commit()
72 cursor.close()
73 db.close()
74
75 @welcome.command()
76 async def text(self, ctx, *, text):
77 if ctx.message.author.guild_permissions.manage_messages:
78 db = sqlite3.connect('welcome.sqlite')
79 cursor = db.cursor()
80 cursor.execute(f'SELECT msg FROM main WHERE guild_id = {ctx.guild.id}')
81 result = cursor.fetchone()
82 if result is None:
83 sql = ("INSERT INTO main(guild_id, msg) VALUES(?,?)")
84 val = (ctx.guild.id, text)
85 await ctx.send(f"Channel has been set to `{text}`")
86 elif result is not None:
87 sql = ("UPDATE main SET msg = ? WHERE guild_id = ?")
88 val = (text, ctx.guild.id)
89 await ctx.send(f"Welcome message has been updated to `{text}`")
90 cursor.execute(sql, val)
91 db.commit()
92 cursor.close()
93 db.close()
94
95def setup(client):
96 client.add_cog(Welcome(client))