This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
105 lines (77 loc) · 2.42 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
"""
bot.py
basic initialization and configuration of vidio
- loads external files - .env, .json
- loads cogs and prefixes
- creates bot instance
"""
import os
import json
import dotenv
import asyncio
import asyncpg
import discord
import logging
from discord.ext import commands
# loads environment variables
dotenv.load_dotenv()
TOKEN = os.getenv('discord_token')
DBL_TOKEN = os.getenv('dbl_token')
AUTH = os.getenv('dbl_auth')
HOSTNAME = os.getenv('database_hostname')
NAME = os.getenv('database_name')
USER = os.getenv('database_user')
PASSWORD = os.getenv('database_password')
async def get_prefix(_bot: commands.Bot, message: discord.Message) -> str:
"""Fetches the custom prefix for the provided server"""
if not message.guild: # if the command is initiated in direct messages
return '-'
guild_id = message.guild.id
prefix = await _bot.db.fetchrow(
"select prefix from guilds where guild_id = $1",
guild_id
)
if not prefix:
async with _bot.db.acquire() as conn:
await conn.execute("insert into guilds (guild_id, prefix) values ($1, $2)",
guild_id, '-')
return '-'
return prefix
bot = commands.AutoShardedBot(
command_prefix=get_prefix,
case_insensitive=True
)
bot.embed = 0x2f3136
async def database_setup() -> None:
"""Sets up the database pool connection"""
bot.db = await asyncpg.create_pool(
user=USER,
password=PASSWORD,
database=NAME,
host=HOSTNAME
)
asyncio.get_event_loop().run_until_complete(database_setup())
bot.logger = logging.getLogger('events')
logging.basicConfig(level=logging.INFO)
handler = logging.FileHandler(filename='events.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
bot.logger.addHandler(handler)
with open('data/config.json', encoding="utf8") as CONFIG:
bot.CONFIG = json.load(CONFIG)
with open('data/emojis.json', encoding="utf8") as EMOJIS:
bot.EMOJIS = json.load(EMOJIS)
with open('data/comments.json', encoding="utf8") as COMMENTS:
bot.COMMENTS = json.load(COMMENTS)
bot.DBL_TOKEN = DBL_TOKEN
bot.AUTH = AUTH
bot.cog_list = [
'cogs.core.database',
'cogs.core.settings',
'cogs.core.topgg',
'cogs.commands.simulation',
'cogs.commands.owner',
'cogs.commands.utility'
]
for cog in bot.cog_list:
bot.load_extension(cog)
bot.run(TOKEN)