· 4 years ago · Mar 24, 2021, 09:28 PM
1import discord
2from discord.ext import commands
3import sqlite3
4import math
5
6TOKEN = 'abcdefgh'
7
8intents = discord.Intents.all()
9intents.members = True
10
11bot = commands.Bot(command_prefix='$', case_insensitive = True, help_command=None, intents=intents)
12
13@bot.event
14async def on_ready():
15 print('Bot is online!')
16
17
18@bot.event
19async def on_command_error(error):
20 print(error)
21
22
23#DATABASE
24
25db = sqlite3.connect(r'C:/Users/M4r5hall/Desktop/New folder/example.db')
26cursor = db.cursor()
27
28cursor.execute('''
29 CREATE TABLE IF NOT EXISTS welcoming(
30 guild_id TEXT,
31 message TEXT,
32 channel_id TEXT
33 )
34 ''')
35
36
37cursor.execute('''
38 CREATE TABLE IF NOT EXISTS levels(
39 guild_id TEXT,
40 user_id TEXT,
41 exp TEXT,
42 lvl TEXT
43 )
44 ''')
45
46
47#WELCOMING
48
49@bot.event
50async def on_member_join(member):
51 db = sqlite3.connect(r'C:/Users/M4r5hall/Desktop/New folder/example.db')
52 cursor = db.cursor()
53 cursor.execute(f'SELECT channel_id FROM welcoming WHERE guild_id = {member.guild.id}')
54 result = cursor.fetchone()
55 if result is None:
56 await member.guild.text_channels[0].send('please provide or smth')
57 else:
58 cursor.execute(f'SELECT message FROM welcoming WHERE guild_id = {member.guild.id}')
59 result1 = cursor.fetchone()
60 members_amountt = len(list(member.guild.members))
61 mention = member.mention
62 user = member.name
63 guild = member.guild
64
65 embed = discord.Embed(description=(result1[0]).format(member=user, mention=mention, number=members_amountt, guild=guild), color=0x95efcc)
66
67 channel = bot.get_channel(id=int(result[0]))
68
69 await channel.send(embed=embed)
70
71
72#LEVELING SYSTEM
73
74@bot.event
75async def on_message(message):
76 cursor.execute(f'SELECT user_id FROM levels WHERE guild_id = {message.author.guild.id} AND user_id = {message.author.id}')
77 result = cursor.fetchone()
78
79 if message.author == client.user:
80 return
81
82 if result is None:
83 sql = ('INSERT INTO levels(guild_id, user_id, exp, lvl) VALUES(?,?,?,?)')
84 val = (message.guild.id, message.author.id, 2, 0)
85 cursor.execute(sql, val)
86 db.commit()
87
88 else:
89 cursor.execute(f'SELECT user_id, exp, lvl FROM levels WHERE guild_id = {message.author.guild.id} AND user_id = {message.author.id}')
90 result1 = cursor.fetchone()
91 exp = int(result1[1])
92 sql = ('UPDATE levels SET exp = ? WHERE guild_id = ? AND user_id = ?')
93 val = (exp + 2, str(message.guild.id), str(message.author.id))
94 cursor.execute(sql, val)
95 db.commit()
96
97
98 cursor.execute(f'SELECT user_id, exp, lvl FROM levels WHERE guild_id = {message.author.guild.id} AND user_id = {message.author.id}')
99 result2 = cursor.fetchone()
100
101 xp_start = int(result2[1])
102 lvl_start = int(result2[2])
103 xp_end = math.floor(5 * (lvl_start ^ 2) + 50 * lvl_start + 100)
104 if xp_end < xp_start:
105 await message.channel.send(f'{message.author.mention} has leveled up to level {lvl_start + 1}.')
106 sql = ('UPDATE levels SET lvl = ? WHERE guild_id = ? AND user_id = ?')
107 val = (int(lvl_start + 1), str(message.guild.id), str(message.author.id))
108 cursor.execute(sql, val)
109 db.commit()
110
111 sql = ('UPDATE levels SET exp = ? WHERE guild_id = ? AND user_id = ?')
112 val = (0, str(message.guild.id), str(message.author.id))
113 db.commit()
114 await bot.process_commands(message)
115
116@bot.command()
117async def welcome(ctx, channel: discord.TextChannel):
118 if ctx.message.author.guild_permissions.manage_messages:
119 cursor.execute(f'SELECT channel_id FROM welcoming WHERE guild_id = {ctx.guild.id}')
120 result = cursor.fetchone()
121 if result is None:
122 sql = ('INSERT INTO welcoming(guild_id, channel_id) VALUES(?,?)')
123 val = (ctx.guild.id, channel.id)
124 await ctx.send(f'Channel has been set to **{channel.mention}**')
125 elif result is not None:
126 sql = ('UPDATE welcoming SET channel_id = ? WHERE guild_id = ?')
127 val = (channel.id, ctx.guild.id)
128 await ctx.send(f'Channel has been updated to: {channel.mention}')
129 cursor.execute(sql, val)
130 db.commit()
131
132
133@bot.command()
134async def text(ctx, *, text):
135 if ctx.message.author.guild_permissions.manage_messages:
136 cursor.execute(f'SELECT message FROM welcoming WHERE guild_id = {ctx.guild.id}')
137 result = cursor.fetchone()
138 if result is None:
139 sql = ('INSERT INTO welcoming(guild_id, message) VALUES(?,?)')
140 val = (ctx.guild.id, text)
141 await ctx.send(f'Message has been set to **{text}**')
142 elif result is not None:
143 sql = ('UPDATE welcoming SET message = ? WHERE guild_id = ?')
144 val = (text, ctx.guild.id)
145 await ctx.send(f'Message has been updated to: {text}')
146 cursor.execute(sql, val)
147 db.commit()
148
149@bot.command()
150async def rank(ctx, user: discord.User=None):
151 if user is None:
152 user = ctx.message.author
153
154 else:
155 cursor.execute(f'SELECT user_id, exp, lvl FROM levels WHERE guild_id = {ctx.guild.id} AND user_id = {user.id}')
156 result = cursor.fetchone()
157 if result is None:
158 await ctx.send('That user is not ranked yet.')
159 else:
160 await ctx.send(f'{user.name} is currently level {str(result[2])} and has {str(result[1])} XP.')
161
162
163
164bot.run(TOKEN)