This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
executable file
·110 lines (90 loc) · 2.83 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
108
109
110
#!/usr/bin/env python3
from dotenv.main import load_dotenv
import aiohttp
import aiopath
import aiosqlite
import asyncio
import discord
import os
import sys
import time
DB_PATH = aiopath.AsyncPath('Data/FDRBot.db')
async def main():
if sys.version_info[:2] < (3, 9):
sys.exit('[ERROR] FDRBot requires Python 3.9 or higher. Exiting.')
load_dotenv()
if 'FDRBOT_TOKEN' not in os.environ.keys():
sys.exit(
"[ERROR] Bot token not set in 'FDRBOT_TOKEN' environment variable. Exiting."
)
if 'FDRBOT_GUILD' not in os.environ.keys():
sys.exit(
"[ERROR] Guild ID not set in 'FDRBOT_GUILD' environment variable. Exiting."
)
if 'FDRBOT_OWNER' not in os.environ.keys():
sys.exit(
"[ERROR] Owner ID(s) not set in 'FDRBOT_OWNER' environment variable. Exiting."
)
try:
debug_guild = [int(os.environ['FDRBOT_GUILD'])]
except TypeError:
sys.exit(
"[ERROR] Invalid guild ID set in 'FDRBOT_GUILD' environment variable. Exiting."
)
try:
owner = int(os.environ['FDRBOT_OWNER'])
except TypeError:
sys.exit(
"[ERROR] Invalid owner ID set in 'FDRBOT_OWNER' environment variable. Exiting."
)
mentions = discord.AllowedMentions(everyone=False, roles=False)
bot = discord.Bot(
help_command=None,
intents=discord.Intents.default(),
allowed_mentions=mentions,
debug_guilds=debug_guild,
owner_id=owner,
)
bot.load_extension('cogs.utils') # Load utils cog first
cogs = aiopath.AsyncPath('cogs')
async for cog in cogs.glob('*.py'):
if cog.stem == 'utils':
continue
bot.load_extension(f'cogs.{cog.stem}')
await DB_PATH.parent.mkdir(exist_ok=True)
async with aiosqlite.connect(DB_PATH) as db, aiohttp.ClientSession() as session:
await db.execute(
'''
CREATE TABLE IF NOT EXISTS tags(
name STRING,
content STRING,
uses INTEGER,
creator INTEGER
)
'''
)
await db.commit()
await db.execute(
'''
CREATE TABLE IF NOT EXISTS logs(
guild INTEGER,
channel INTEGER
)
'''
)
await db.commit()
# Setup bot attributes
bot.db = db
bot.session = session
bot.start_time = await asyncio.to_thread(time.time)
try:
await bot.start(os.environ['FDRBOT_TOKEN'])
except discord.LoginFailure:
sys.exit(
"[ERROR] Token invalid, make sure the 'FDRBOT_TOKEN' environment variable is set to your bot token. Exiting."
)
if __name__ == '__main__':
try:
asyncio.run(main())
except KeyboardInterrupt:
pass