-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
161 lines (139 loc) · 5.12 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import sys
import json
import platform
import random
import discord
from discord import Intents, Bot, Embed
from discord.ext import tasks, commands
import lib.exceptions as exceptions
from lib.handlers.wk_item import load_item_db, load_item_db
VERSION = '0.2.3'
UPDATE_WK_ITEMS = False
# Load configuration and system files
if not os.path.isfile("config.json"):
sys.exit("'config.json' not found! Please add it and try again.")
else:
with open("config.json") as file:
config = json.load(file)
# Setup Discord bot intents and initialize client
intents = Intents.default()
intents.message_content = True
bot = Bot(
intents=intents,
debug_guilds=[int(config["guild_id"])]
)
bot.config = config
bot.version = VERSION
@bot.event
async def on_ready() -> None:
"""Called when the connection to Discord has been established."""
print("\n-------------------")
print(f"Logged in as '{bot.user}' v{bot.version}")
print(f"PyCord API version: {discord.__version__}")
print(f"Python version: {platform.python_version()}")
print(f"Running on: {platform.system()} {platform.release()} ({os.name})")
print("-------------------\n")
bot_presence.start()
embed = Embed(
description="I'm online! 🤟🏼",
color=0x2ca02c # tab:green matplotlib
)
channel_announce_id = int(config["channel_ids"]["announce"])
await bot.get_channel(channel_announce_id).send(embed=embed)
@tasks.loop(minutes=1.0)
async def bot_presence() -> None:
"""Bot discord presence status updated every minute."""
statuses = [
"Learning ALL the Kanji!",
"Doing my reviews.",
"WaniKani Simulator.",
"Crabigator Dance Party!",
"Simulating Weeb-speak.",
"Writing a WaniKani anime.",
"Using ALL the vocab!",
"Inventing new mnemonics.",
"Sleeping on the job...",
"On a crabigator date.",
"Yell 助けて for help!",
]
await bot.change_presence(activity=discord.Game(random.choice(statuses)))
@bot.event
async def on_application_command(ctx) -> None:
"""Executed every time a slash command has been successfully executed."""
message = (
f"Executed '/{ctx.command.name}' command in "
f"{ctx.guild.name} (ID: {ctx.guild.id}) by "
f"{ctx.author} (ID: {ctx.author.id})"
)
print(message)
@bot.event
async def on_application_command_error(ctx, exception) -> None:
"""Executed every time a valid slash command catches an error."""
if isinstance(exception, commands.CommandOnCooldown):
minutes, seconds = divmod(exception.retry_after, 60)
hours, minutes = divmod(minutes, 60)
hours = hours % 24
message = (
f"You can use this command again in "
f"{f'{round(hours)} hours' if round(hours) > 0 else ''} "
f"{f'{round(minutes)} minutes' if round(minutes) > 0 else ''} "
f"{f'{round(seconds)} seconds' if round(seconds) > 0 else ''}."
)
embed = Embed(
title="Hey, please slow down!",
description=message,
color=0xFF0000
)
return await ctx.send(embed=embed, ephemeral=True)
elif isinstance(exception, exceptions.UserBlacklisted):
embed = Embed(
title="Error!",
description="You are blacklisted from using the bot.",
color=0xFF0000
)
return await ctx.send(embed=embed, ephemeral=True)
elif isinstance(exception, exceptions.UserNotDeveloper):
embed = Embed(
title="Error!",
description="You are not the developer of the bot!",
color=0xFF0000
)
return await ctx.send(embed=embed, ephemeral=True)
elif isinstance(exception, commands.MissingPermissions):
embed = Embed(
title="Error!",
description="You are missing the permission(s) `" + ", ".join(
exception.missing_permissions) + "` to execute this command!",
color=0xFF0000
)
return await ctx.send(embed=embed, ephemeral=True)
elif isinstance(exception, commands.MissingRequiredArgument):
embed = Embed(
title="Error!",
# Command arguments have no capital letter in the code.
description=str(exception).capitalize(),
color=0xFF0000
)
await ctx.send(embed=embed, ephemeral=True)
raise exception
if __name__ == "__main__":
# Loading bot cogs
print("\nLoading Crabigator cogs:")
for file in os.listdir("lib/cogs"):
if file.endswith(".py"):
cog = file[:-3]
try:
bot.load_extension(f"lib.cogs.{cog}")
print(f"- {cog} cog loaded")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
print(f"! {cog} cog failed to load:\n {exception}")
print("Finished loading the cogs.")
# Loading WaniKani items
print("\nLoading WaniKani items:")
bot.item_data = {}
load_item_db(config["wk_token"], bot.item_data, UPDATE_WK_ITEMS)
print("Finished loading all items.")
# Finally run the bot
bot.run(config["bot_token"])