import discord
import asyncio
import aiosqlite
import random
from discord.ext import commands
from discord_components import Button,Select,SelectOption,ComponentsBot
import config
#set prefix
client = commands.Bot(command_prefix = "$")
client.remove_command('help')
#ready check
@client.event
async def on_ready():
print('Bot logged in as {0.user}'.format(client))
setattr(client, 'db', await aiosqlite.connect('level.db'))
await asyncio.sleep(3)
async with client.db.cursor() as cursor:
await cursor.execute('CREATE TABLE IF NOT EXISTS levels (level INTEGER, xp INTEGER, user INTEGER, guild INTEGER)')
@client.event
async def on_message(message):
if message.author.bot:
return
author = message.author
guild = message.guild
async with client.db.cursor() as cursor:
await cursor.execute("SELECT xp FROM levels WHERE user = ? AND guild = ?", (author.id, guild.id,))
xp = await cursor.fetchone()
await cursor.execute("SELECT xp FROM levels WHERE user = ? AND guild = ?", (author.id, guild.id,))
level = await cursor.fetchone()
if not xp or not level:
await cursor.execute("INSERT INTO levels (level, xp, user, guild) VALUES (?, ?, ?, ?)", (0, 0, author.id, guild.id,))
await client.commit()
try:
xp =xp[0]
level = level[0]
except TypeError:
xp = 0
level = 0
if level <5:
xp += random.randint(1, 3)
await cursor.execute("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (xp, author.id, guild.id,))
else:
rand = random.randint(1, (level//4))
if rand == 1:
xp += random.randint(1, 3)
await cursor.execute("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (xp, author.id, guild.id,))
if xp >= 100:
level += 1
await cursor.execute("UPDATE levels SET level = ? WHERE user = ? and guild = ?", (level, author.id, guild.id,))
await cursor.execute("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (0, author.id, guild.id,))
await message.channel.send(f'{author.mention} has leveled up to level **{level}**!')
await client.db.commit()
@client.command(aliases = ['lvl', 'rank', 'r'])
async def level(ctx, member: discord.Member = None):
member = ctx.author
async with client.db.cursor() as cursor:
await cursor.execute("SELECT xp FROM levels WHERE user = ? AND guild = ?", (member.id, ctx.guild.id,))
xp = await cursor.fetchone()
await cursor.execute("SELECT xp FROM levels WHERE user = ? AND ctx.guild = ?", (member.id, ctx.guild.id,))
level = await cursor.fetchone()
if not xp or not level:
await cursor.execute("INSERT INTO levels (level, xp, user, guild) VALUES (?, ?, ?, ?)", (0, 0, ctx.author.id, ctx.guild.id,))
await client.commit()
try:
xp = xp[0]
level = level[0]
except TypeError:
xp = 0
level = 0
em = discord.Embed(title = f"{member.name}'s level", description = f"level: {level}\nXP: {xp}")
em.set_thumbnail(url = member.avatar_url)
await ctx.send(embed = em)
#help command
@client.command()
async def help(ctx):
page1 = discord.Embed(title = 'Start Page.',
description =
'Bot prefix = `' + str(config.PREFIX) + '`\n'+
'This is a start page, please select a category for more information!',
color = discord.Color.green())
page1.set_thumbnail(url = ctx.guild.icon_url)
page2 = discord.Embed(title = 'Moderation Settings.',
description =
'**clear** `amount` - clears the chat to amount.\n' +
'**mute** `user,time,reason` - disables access to all channels.\n' +
'**unmute** `user` - enables access to all channels.\n' +
'**kick** `user,reason` - kick a member from the server.\n' +
'**ban** `user,reason` - ban a member from the server.\n' +
'**unban** `user` - unban a member from the server.',
color = discord.Color.green())
page2.set_thumbnail(url = ctx.guild.icon_url)
emoji1 = client.get_emoji(973967340003815444)
message = await ctx.send(components = [
Select(
placeholder = 'Select a category!',
options = [
SelectOption(
label = 'Moderation Settings',
emoji = emoji1,
description = 'Setting up server moderation.',
value = '1'
)
],
custom_id = 'HelpSelect'
)
], embed = page1)
while True:
interaction = await client.wait_for('select_option', check = lambda inter: inter.custom_id == 'HelpSelect' and inter.user == ctx.author)
value = interaction.values[0]
if value == '1':
await message.edit(embed = page2)
else:
print('Error')
#clear command
@client.command()
@commands.has_permissions(administrator = True)
async def clear(ctx, amount = 0):
await ctx.channel.purge(limit = amount + 1)
#kick command
@client.command()
@commands.has_permissions(kick_members = True)
async def kick(ctx, member:discord.Member, *, reason = None):
if reason == None:
reason = "None"
await ctx.guild.kick(member)
await ctx.send(f'{member.mention} has been kicked for {reason}.')
#mute command
@client.command()
@commands.has_permissions(manage_messages = True)
async def mute(ctx, member:discord.Member, time:str, *, reason = None):
end_time = time[-1:]
mutetime = int(time[:-1])
muteembedserver = discord.Embed(title = 'Mute information.', color = discord.Color.red())
muteembedserver.add_field(name = 'Moderator', value = ctx.author.mention, inline = True)
muteembedserver.add_field(name = 'Muted user', value = member.mention, inline = True)
muteembedserver.add_field(name = '⠀', value = '⠀', inline = True)
muteembedserver.add_field(name = 'Reason', value = reason, inline = True)
muteembedserver.add_field(name = 'Muted Time', value = time, inline = True)
muteembedserver.add_field(name = '⠀', value = '⠀', inline = True)
muteembedmember = discord.Embed(title = 'Mute information.', description = 'You were muted in the server ' + str(ctx.guild.name) + str('!'), color = discord.Color.red())
muteembedmember.add_field(name = 'Moderator', value = ctx.author.mention, inline = True)
muteembedmember.add_field(name = 'Reason', value = reason, inline = True)
muteembedmember.add_field(name = 'Time', value = time, inline = True)
guild = ctx.guild
mutedRole = discord.utils.get(guild.roles, name = 'Muted')
if not mutedRole:
mutedRole = await guild.create_role(name = 'Muted')
for channel in guild.channels:
await channel.set_permissions(mutedRole, speak = False, send_messages = False, read_message_history = True, read_messages = False)
if mutetime <= 0:
await ctx.send(f'Time must not be less than or equal to zero!')
else:
if end_time == 's':
await member.add_roles(mutedRole, reason = reason)
await ctx.send(embed = muteembedserver)
await member.send(embed = muteembedmember)
await asyncio.sleep(mutetime)
await member.remove_roles(mutedRole)
elif end_time == 'm':
await member.add_roles(mutedRole, reason = reason)
await ctx.send(embed = muteembedserver)
await member.send(embed = muteembedmember)
await asyncio.sleep(mutetime * 60)
await member.remove_roles(mutedRole)
elif end_time == 'h':
await member.add_roles(mutedRole, reason = reason)
await ctx.send(embed = muteembedserver)
await member.send(embed = muteembedmember)
await asyncio.sleep(mutetime * 60 * 60)
await member.remove_roles(mutedRole)
elif end_time == 'd':
await member.add_roles(mutedRole, reason = reason)
await ctx.send(embed = muteembedserver)
await member.send(embed = muteembedmember)
await asyncio.sleep(mutetime * 60 * 60 * 24)
await member.remove_roles(mutedRole)
else:
await ctx.send(f'**You entered the wrong time!(Example: 10s,10m,10h,10d)**')
#unmate command
@client.command()
@commands.has_permissions(manage_messages = True)
async def unmute(ctx, member:discord.Member):
guild = ctx.guild
mutedRole = discord.utils.get(guild.roles, name = 'Muted')
await member.remove_roles(mutedRole)
await ctx.send(f"{member.mention} Successfully unmuted by {ctx.author.mention}.")
await member.send(f'You were unmuted in the server {guild.name}.')
#ban command
@client.command()
@commands.has_permissions(ban_members = True)
async def ban(ctx, member:discord.Member, *, reason = None):
guild = ctx.guild
if reason == None:
reason = f'None'
else:
reason = reason
if ctx.author.top_role < member.top_role:
await ctx.send(f"You can't ban members with a higher role than you!")
elif ctx.author.top_role > member.top_role:
await ctx.guild.ban(member, reason = reason)
await ctx.send(f'{member.mention} Successfully banned by {ctx.author.mention} for reason {reason}.')
elif ctx.author.guild_permissions.administrator:
await ctx.guild.ban(member, reason = reason)
await ctx.send(f'{member.mention} Successfully banned by {ctx.author.mention} for reason {reason}.')
else:
await ctx.send(f'Error')
#unban command
@client.command()
@commands.has_permissions(ban_members = True)
async def unban(ctx, *, member):
bannedUsers = await ctx.guild.bans()
member = await commands.converter.UserConverter().convert(ctx, member)
for ban_entry in bannedUsers:
user = ban_entry.user
if user.id == member.id:
await ctx.guild.unban(user)
await ctx.send(f'{user.mention} Successfully unbanned by {ctx.author.mention}.')
elif user.id != member.id:
await ctx.send('User not banned!')
else:
await ctx.send(f'Error')
#Connect
client.run( config.TOKEN )
import discord
import asyncio
import aiosqlite
import random
from discord.ext import commands
from discord_components import Button,Select,SelectOption,ComponentsBot
import config
#set prefix
client = commands.Bot(command_prefix = "$")
client.remove_command('help')
#ready check
@client.event
async def on_ready():
print('Bot logged in as {0.user}'.format(client))
setattr(client, 'db', await aiosqlite.connect('level.db'))
await asyncio.sleep(3)
async with client.db.cursor() as cursor:
await cursor.execute('CREATE TABLE IF NOT EXISTS levels (level INTEGER, xp INTEGER, user INTEGER, guild INTEGER)')
@client.event
async def on_message(message):
if message.author.bot:
return
await client.process_commands(message)
author = message.author
guild = message.guild
async with client.db.cursor() as cursor:
await cursor.execute("SELECT xp FROM levels WHERE user = ? AND guild = ?", (author.id, guild.id,))
xp = await cursor.fetchone()
await cursor.execute("SELECT xp FROM levels WHERE user = ? AND guild = ?", (author.id, guild.id,))
level = await cursor.fetchone()
if not xp or not level:
await cursor.execute("INSERT INTO levels (level, xp, user, guild) VALUES (?, ?, ?, ?)", (0, 0, author.id, guild.id,))
await client.db.commit()
try:
xp =xp[0]
level = level[0]
except TypeError:
xp = 0
level = 0
if level <5:
xp += random.randint(1, 3)
await cursor.execute("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (xp, author.id, guild.id,))
else:
rand = random.randint(1, (level//4))
if rand == 1:
xp += random.randint(1, 3)
await cursor.execute("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (xp, author.id, guild.id,))
if xp >= 100:
level += 1
await cursor.execute("UPDATE levels SET level = ? WHERE user = ? and guild = ?", (level, author.id, guild.id,))
await cursor.execute("UPDATE levels SET xp = ? WHERE user = ? AND guild = ?", (0, author.id, guild.id,))
await message.channel.send(f'{author.mention} has leveled up to level **{level}**!')
await client.db.commit()
@client.command(aliases = ['lvl', 'rank', 'r'])
async def level(ctx, member: discord.Member = None):
member = ctx.author
async with client.db.cursor() as cursor:
await cursor.execute("SELECT xp FROM levels WHERE user = ? AND guild = ?", (member.id, ctx.guild.id,))
xp = await cursor.fetchone()
await cursor.execute("SELECT xp FROM levels WHERE user = ? AND guild = ?", (member.id, ctx.guild.id,))
level = await cursor.fetchone()
if not xp or not level:
await cursor.execute("INSERT INTO levels (level, xp, user, guild) VALUES (?, ?, ?, ?)", (0, 0, ctx.author.id, ctx.guild.id,))
await client.db.commit()
try:
xp = xp[0]
level = level[0]
except TypeError:
xp = 0
level = 0
em = discord.Embed(title = f"{member.name}'s level", description = f"level: {level}\nXP: {xp}")
em.set_thumbnail(url = member.avatar_url)
await ctx.send(embed = em)
#help command
@client.command()
async def help(ctx):
page1 = discord.Embed(title = 'Start Page.',
description =
'Bot prefix = `' + str(config.PREFIX) + '`\n'+
'This is a start page, please select a category for more information!',
color = discord.Color.green())
page1.set_thumbnail(url = ctx.guild.icon_url)
page2 = discord.Embed(title = 'Moderation Settings.',
description =
'**clear** `amount` - clears the chat to amount.\n' +
'**mute** `user,time,reason` - disables access to all channels.\n' +
'**unmute** `user` - enables access to all channels.\n' +
'**kick** `user,reason` - kick a member from the server.\n' +
'**ban** `user,reason` - ban a member from the server.\n' +
'**unban** `user` - unban a member from the server.',
color = discord.Color.green())
page2.set_thumbnail(url = ctx.guild.icon_url)
emoji1 = client.get_emoji(973967340003815444)
message = await ctx.send(components = [
Select(
placeholder = 'Select a category!',
options = [
SelectOption(
label = 'Moderation Settings',
emoji = emoji1,
description = 'Setting up server moderation.',
value = '1'
)
],
custom_id = 'HelpSelect'
)
], embed = page1)
while True:
interaction = await client.wait_for('select_option', check = lambda inter: inter.custom_id == 'HelpSelect' and inter.user == ctx.author)
value = interaction.values[0]
if value == '1':
await message.edit(embed = page2)
else:
print('Error')
#clear command
@client.command()
@commands.has_permissions(administrator = True)
async def clear(ctx, amount = 0):
await ctx.channel.purge(limit = amount + 1)
#kick command
@client.command()
@commands.has_permissions(kick_members = True)
async def kick(ctx, member:discord.Member, *, reason = None):
if reason == None:
reason = "None"
await ctx.guild.kick(member)
await ctx.send(f'{member.mention} has been kicked for {reason}.')
#mute command
@client.command()
@commands.has_permissions(manage_messages = True)
async def mute(ctx, member:discord.Member, time:str, *, reason = None):
end_time = time[-1:]
mutetime = int(time[:-1])
muteembedserver = discord.Embed(title = 'Mute information.', color = discord.Color.red())
muteembedserver.add_field(name = 'Moderator', value = ctx.author.mention, inline = True)
muteembedserver.add_field(name = 'Muted user', value = member.mention, inline = True)
muteembedserver.add_field(name = '⠀', value = '⠀', inline = True)
muteembedserver.add_field(name = 'Reason', value = reason, inline = True)
muteembedserver.add_field(name = 'Muted Time', value = time, inline = True)
muteembedserver.add_field(name = '⠀', value = '⠀', inline = True)
muteembedmember = discord.Embed(title = 'Mute information.', description = 'You were muted in the server ' + str(ctx.guild.name) + str('!'), color = discord.Color.red())
muteembedmember.add_field(name = 'Moderator', value = ctx.author.mention, inline = True)
muteembedmember.add_field(name = 'Reason', value = reason, inline = True)
muteembedmember.add_field(name = 'Time', value = time, inline = True)
guild = ctx.guild
mutedRole = discord.utils.get(guild.roles, name = 'Muted')
if not mutedRole:
mutedRole = await guild.create_role(name = 'Muted')
for channel in guild.channels:
await channel.set_permissions(mutedRole, speak = False, send_messages = False, read_message_history = True, read_messages = False)
if mutetime <= 0:
await ctx.send(f'Time must not be less than or equal to zero!')
else:
if end_time == 's':
await member.add_roles(mutedRole, reason = reason)
await ctx.send(embed = muteembedserver)
await member.send(embed = muteembedmember)
await asyncio.sleep(mutetime)
await member.remove_roles(mutedRole)
elif end_time == 'm':
await member.add_roles(mutedRole, reason = reason)
await ctx.send(embed = muteembedserver)
await member.send(embed = muteembedmember)
await asyncio.sleep(mutetime * 60)
await member.remove_roles(mutedRole)
elif end_time == 'h':
await member.add_roles(mutedRole, reason = reason)
await ctx.send(embed = muteembedserver)
await member.send(embed = muteembedmember)
await asyncio.sleep(mutetime * 60 * 60)
await member.remove_roles(mutedRole)
elif end_time == 'd':
await member.add_roles(mutedRole, reason = reason)
await ctx.send(embed = muteembedserver)
await member.send(embed = muteembedmember)
await asyncio.sleep(mutetime * 60 * 60 * 24)
await member.remove_roles(mutedRole)
else:
await ctx.send(f'**You entered the wrong time!(Example: 10s,10m,10h,10d)**')
#unmate command
@client.command()
@commands.has_permissions(manage_messages = True)
async def unmute(ctx, member:discord.Member):
guild = ctx.guild
mutedRole = discord.utils.get(guild.roles, name = 'Muted')
await member.remove_roles(mutedRole)
await ctx.send(f"{member.mention} Successfully unmuted by {ctx.author.mention}.")
await member.send(f'You were unmuted in the server {guild.name}.')
#ban command
@client.command()
@commands.has_permissions(ban_members = True)
async def ban(ctx, member:discord.Member, *, reason = None):
guild = ctx.guild
if reason == None:
reason = f'None'
else:
reason = reason
if ctx.author.top_role < member.top_role:
await ctx.send(f"You can't ban members with a higher role than you!")
elif ctx.author.top_role > member.top_role:
await ctx.guild.ban(member, reason = reason)
await ctx.send(f'{member.mention} Successfully banned by {ctx.author.mention} for reason {reason}.')
elif ctx.author.guild_permissions.administrator:
await ctx.guild.ban(member, reason = reason)
await ctx.send(f'{member.mention} Successfully banned by {ctx.author.mention} for reason {reason}.')
else:
await ctx.send(f'Error')
#unban command
@client.command()
@commands.has_permissions(ban_members = True)
async def unban(ctx, *, member):
bannedUsers = await ctx.guild.bans()
member = await commands.converter.UserConverter().convert(ctx, member)
for ban_entry in bannedUsers:
user = ban_entry.user
if user.id == member.id:
await ctx.guild.unban(user)
await ctx.send(f'{user.mention} Successfully unbanned by {ctx.author.mention}.')
elif user.id != member.id:
await ctx.send('User not banned!')
else:
await ctx.send(f'Error')
#Connect
client.run(config.TOKEN)