-
Notifications
You must be signed in to change notification settings - Fork 11
/
bot.py
107 lines (97 loc) · 3.49 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import discord
from discord.ext import commands
import asyncio
import sys, traceback
import time
import os
import math
import json
import sqlite3
import random
import re
import datetime
from datetime import timedelta
from collections import OrderedDict, deque, Counter
bot = commands.Bot(command_prefix='?', case_insensitive=True)
bot.remove_command('help')
initial_extensions = ['cogs.leveling',
'cogs.public']
if __name__ == '__main__':
for extension in initial_extensions:
try:
bot.load_extension(extension)
except Exception as e:
print(f'Failed to load extension {extension}', file=sys.stderr)
traceback.print_exc()
@bot.event
async def on_ready():
print(f'We have logged in as {bot.user}')
print('Ready!')
return await bot.change_presence(activity=discord.Activity(type=3, name='for Levels'))
@bot.event
async def on_guild_join(guild):
main = sqlite3.connect('Leveling/main.db')
cursor = main.cursor()
cursor.execute(f"SELECT enabled FROM glevel WHERE guild_id = '{guild.id}'")
result = cursor.fetchone()
if result is None:
sql = ("INSERT INTO glevel(guild_id, enabled) VALUES(?,?)")
val = (str(guild.id), 'enabled')
cursor.execute(sql, val)
main.commit()
elif str(result[0]) == 'disabled':
sql = ("UPDATE glevel SET enabled = ? WHERE guild_id = ?")
val = ('enabled', str(guild.id))
cursor.execute(sql, val)
main.commit()
cursor.close()
main.close()
@bot.command(pass_context=True)
async def reload(ctx, *, msg):
"""Load a module."""
await ctx.message.delete()
try:
if os.path.exists("custom_cogs/{}.py".format(msg)):
bot.reload_extension("custom_cogs.{}".format(msg))
elif os.path.exists("cogs/{}.py".format(msg)):
bot.reload_extension("cogs.{}".format(msg))
else:
raise ImportError("No module named '{}'".format(msg))
except Exception as e:
await ctx.send('Failed to reload module: `{}.py`'.format(msg))
await ctx.send('{}: {}'.format(type(e).__name__, e))
else:
await ctx.send('Reloaded module: `{}.py`'.format(msg))
@bot.command(pass_context=True)
async def unload(ctx, *, msg):
"""Unload a module"""
await ctx.message.delete()
try:
if os.path.exists("cogs/{}.py".format(msg)):
bot.unload_extension("cogs.{}".format(msg))
elif os.path.exists("custom_cogs/{}.py".format(msg)):
bot.unload_extension("custom_cogs.{}".format(msg))
else:
raise ImportError("No module named '{}'".format(msg))
except Exception as e:
await ctx.send('Failed to unload module: `{}.py`'.format(msg))
await ctx.send('{}: {}'.format(type(e).__name__, e))
else:
await ctx.send('Unloaded module: `{}.py`'.format(msg))
@bot.command(pass_context=True)
async def load(ctx, *, msg):
"""Load a module"""
await ctx.message.delete()
try:
if os.path.exists("cogs/{}.py".format(msg)):
bot.load_extension("cogs.{}".format(msg))
elif os.path.exists("custom_cogs/{}.py".format(msg)):
bot.load_extension("custom_cogs.{}".format(msg))
else:
raise ImportError("No module named '{}'".format(msg))
except Exception as e:
await ctx.send('Failed to load module: `{}.py`'.format(msg))
await ctx.send('{}: {}'.format(type(e).__name__, e))
else:
await ctx.send('Loaded module: `{}.py`'.format(msg))
bot.run(TOKEN)