-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
100 lines (67 loc) · 2.46 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
# Dear programmer:
# When I wrote this code, only god, and I knew how it worked
# Now, only god knows it!
#
# Therefore, if you are trying to optimize this,
# and it fails (most surely)
# please increase this counter as a warning for the next person:
#
# total_hours_wasted_here = 254
import os
import logging
from dotenv import load_dotenv
import discord
from discord.ext import commands
from Database import database
import asyncio
import platform
from Database.Models.node import Node
from Database.Models.user import User
from Setup.content import cleanup_all_content
from Setup.user import spawn_user, move_to, whoami_embed
# Extra Cases ---------------------------------------------------------------
# prevent event loop is closed error
# https://stackoverflow.com/questions/45600579/asyncio-event-loop-is-closed-when-getting-loop
if platform.system() == "Windows":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# prevent voice client warning
discord.VoiceClient.warn_nacl = False
# ---------------------------------------------------------------------------
discord.utils.setup_logging()
load_dotenv()
is_dev = os.getenv("IS_DEV") == "True"
class MyBot(commands.Bot):
async def setup_hook(self):
await database.init()
await bot.load_extension("Cogs.main_cog")
await bot.load_extension("Cogs.user_cog")
await bot.load_extension("Cogs.generator_cog")
if is_dev:
await bot.load_extension("Cogs.test_cog")
await cleanup_all_content()
async def on_member_join(self, member):
# TODO: generate image, name, and background story
await spawn_user(member)
node = await Node.get(name="root")
await move_to(member, node)
channel = self.get_channel(node.channel_id)
# greeting msg
welcome_message = f"Welcome to the {node.name}, {member.mention}!"
await channel.send(welcome_message)
user = await User.get(discord_id=member.id)
embed = whoami_embed(user)
await channel.send(embed=embed)
async def close(self):
logging.info("Closing discord bot...")
await database.Tortoise.close_connections()
await super().close()
intents = discord.Intents.all()
bot = MyBot(
command_prefix=">",
intents=intents,
help_command=None,
)
@bot.event
async def on_ready():
logging.info(f"{bot.user} has connected to Discord!")
bot.run(os.getenv("DISCORD_TOKEN"), log_handler=None)