-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
100 lines (82 loc) · 2.84 KB
/
main.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
import glob
import json
import os
import re
import time
from configparser import *
from discord.ext import commands, timers
from loguru import logger
from cogs.utils.checks import is_bot_owner_check
from cogs.utils.database import Database
#initiate logger test
logger.add(f"file_{str(time.strftime('%Y%m%d-%H%M%S'))}.log", rotation="500 MB")
auth = ConfigParser()
auth.read('auth.ini') # All my usernames and passwords for the api
database = Database(path='cogs/peribot.db')
def load_cogs(folder):
os.chdir(folder)
files = []
for file in glob.glob("*.py"):
file = re.search('^([A-Za-z1-9]{1,})(?:.py)$', file).group(1)
files.append(file)
return files
def config():
with open('config.json', 'r') as f:
config = json.load(f)
return config
async def get_prefix(bot, message):
if not message.guild:
return commands.when_mentioned_or('!')(bot, message)
settings = await database.get_server_settings(message.guild.id)
if settings:
prefix = settings.prefix
return commands.when_mentioned_or(str(prefix))(bot, message)
else:
return commands.when_mentioned_or("!")(bot, message)
bot = commands.Bot(command_prefix=get_prefix)
@bot.event
async def on_ready():
"""
When bot is ready and online it prints that its online
:return:
"""
bot.timer_manager = timers.TimerManager(bot)
logger.debug("Bot is ready!")
@bot.command()
@is_bot_owner_check()
async def load(ctx, extension):
try:
bot.load_extension('cogs.' + extension)
logger.debug(f'Loaded {extension}')
await ctx.send(f'Loaded {extension}')
except Exception as error:
logger.exception(f"Extension {extension} could not be loaded. [{error}]")
@bot.command()
@is_bot_owner_check()
async def reload(ctx, extension):
try:
bot.unload_extension('cogs.' + extension)
bot.load_extension('cogs.' + extension)
logger.debug(f'Reloaded {extension}')
await ctx.send(f'Reloaded {extension}')
except Exception as error:
logger.exception(f"Extension {extension} could not be reloaded. [{error}]")
@bot.command()
@is_bot_owner_check()
async def unload(ctx, extension):
try:
bot.unload_extension('cogs.' + extension)
logger.debug(f'Unloaded {extension}')
await ctx.send(f'{extension} successfully unloaded')
except Exception as error:
logger.exception(f"Extension {extension} could not be unloaded. [{error}]")
if __name__ == "__main__":
bot.remove_command('help')
extensions = load_cogs('cogs')
for extension in extensions:
try:
bot.load_extension('cogs.'+extension)
logger.debug(f'Loaded {extension} cog.')
except Exception as error:
logger.exception(f"Extension {extension} could not be loaded. [{error}]")
bot.run(auth.get('discord', 'TOKEN'))