Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (51 loc) · 1.76 KB
/
Copy pathmain.py
File metadata and controls
67 lines (51 loc) · 1.76 KB
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
import argparse
import logging
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
class DDSBot(commands.Bot):
def __init__(self):
# Every permission needs to be enabled through intents.
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
super().__init__(
command_prefix=">",
case_insensitive=True,
intents=intents,
)
# Remove built-in help command.
self.remove_command("help")
logging.basicConfig(level=logging.INFO)
logging.getLogger("discord")
file_handler = logging.FileHandler("bot_errors.log")
file_handler.setLevel(logging.ERROR)
logging.getLogger().addHandler(file_handler)
def get_token(self, build_mode: bool) -> str | None:
"""Load the environment variables from the .env file."""
load_dotenv()
return os.getenv("DISCORD_TOKEN") if build_mode else os.getenv("DEV_TOKEN")
async def setup_hook(self) -> None:
await self.load_cogs()
async def load_cogs(self):
"""Load every cog by dropping the .py in their path name."""
for file in os.listdir("./cogs"):
if file.endswith(".py"):
await self.load_extension(f"cogs.{file[:-3]}")
async def on_ready(self):
# self.user can be None in some runtime/type-checking scenarios, guard against that.
name = self.user.name if self.user is not None else "Unknown"
print(f"INFO: {name} has connected to Discord.")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-b", "--build", action="store_true", help="Build for the main 'DDS-Bot'. Without this, will start 'DDS-Bot Dev'."
)
args = parser.parse_args()
bot = DDSBot()
token = bot.get_token(args.build)
if token is not None:
bot.run(token)
else:
raise RuntimeError("ERROR: DISCORD TOKEN NOT SET")