generated from jarp0l/discord.py-bot-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·117 lines (93 loc) · 3.2 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
#!/bin/env python3
import logging
import os
import discord
from discord.ext import commands
from keep_alive import keep_alive
import libs.config as config
####################
# Config variables #
####################
c_prefix = config.get_config("prefix")
c_extensions = config.get_config("cogs")
c_disabled_extensions = config.get_config("disabled_cogs")
####################
# String variables #
####################
s_status = config.get_string("status")
# Prefix
bot = commands.Bot(command_prefix=c_prefix)
# Log to a file
logger = logging.getLogger("discord")
logger.setLevel(logging.ERROR)
handler = logging.FileHandler(filename="discord.log", encoding="utf-8", mode="a")
handler.setFormatter(
logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s")
)
logger.addHandler(handler)
# Logging the starting point of bot into the console
@bot.event
async def on_ready():
print(f"\n### Logged in as {bot.user}\n")
await bot.change_presence(
status=discord.Status.online, activity=discord.Game(name=f"{s_status}")
)
# Removes the "command not found" error from the console
@bot.event
async def on_command_error(ctx, error):
errors_to_skip = [commands.CommandNotFound, commands.MissingRequiredArgument]
for error_type in errors_to_skip:
if isinstance(error, error_type):
return
raise error
# Load cogs
def main():
# Logging the unlodead cogs into the console
if len(c_disabled_extensions) != 0:
print("\nFollowing cogs are disabled:")
for extension in c_disabled_extensions:
print(f"[Disabled]\t{extension} has been disabled.")
# Logging the loaded cogs into the console
if len(c_extensions) != 0:
print("\nLoading the COGS:")
for extension in c_extensions:
try:
bot.load_extension(extension)
print(f"[Success]\t{extension} loaded successfully.")
except Exception as e:
print(
f"[ERROR]\tAn error occurred while loading {extension}\n-->"
+ str(e)
+ "\n"
)
@bot.command(name="reload", aliases=["rl"])
async def _reload(ctx):
"""Reaload the enabled cogs"""
reloaded = []
not_reloaded = []
for extension in c_extensions:
try:
bot.reload_extension(extension)
reloaded.append(extension)
except Exception as e:
not_reloaded.append(extension)
print(
f"[ERROR]\tAn error occurred while reloading {extension}\n-->"
+ str(e)
+ "\n"
)
if not len(not_reloaded):
await ctx.channel.send(f"**[Success]**\tAll cogs reloaded successfully.")
else:
not_reloaded_cogs = "\n".join(not_reloaded)
await ctx.channel.send(
f"**[ERROR]**\t{len(not_reloaded)} cog(s) could not be reloaded:\n{not_reloaded_cogs}"
)
if __name__ == "__main__":
main()
try:
keep_alive()
bot.run(os.environ["BOT_TOKEN"])
except discord.errors.HTTPException as e:
print(f"Rate limited! Try again later.\n\n{e}")
exit(1)