-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.py
187 lines (166 loc) · 6.03 KB
/
runner.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import asyncio
import sys
import os
import aiohttp
import discord
from pepebot import PepeBot
import logging
from handler.logger import Logger
from discord.ext import commands
from handler.context import Context
_log = logging.getLogger("pepebot")
@commands.group(name='cog', invoke_without_command=True)
@commands.is_owner()
async def cog(ctx: Context):
embed = discord.Embed(title="Cogs")
embed.description = ""
for index, ext in enumerate(ctx.bot.extensions, start=1):
embed.description += f"{index}{ext} \n"
await ctx.send(embed=embed)
@cog.command()
@commands.is_owner()
async def load(ctx: Context, Cog: str):
failed_extensions = []
loaded_extensions = []
embed = discord.Embed(title="``cog loaded``")
if Cog == '*':
for ext in ctx.bot.COGS:
try:
await ctx.bot.load_extension(name="cogs." + ext)
except (commands.ExtensionFailed, commands.ExtensionNotFound):
failed_extensions.append(ext)
except commands.ExtensionAlreadyLoaded:
loaded_extensions.append(ext)
else:
loaded_extensions.append(ext)
embed.description = (
f"loaded extension: {loaded_extensions} "
f"{len(loaded_extensions)}/{len(ctx.bot.COGS)} \n"
f"failed extensions: {failed_extensions}"
)
await ctx.send(embed=embed)
return
try:
embed.description = f"{Cog} has been successfully loaded"
except commands.ExtensionAlreadyLoaded:
await ctx.error_embed(description="cog is already loaded")
except commands.ExtensionNotLoaded:
await ctx.error_embed(description=f"{Cog} is not a valid cog")
except commands.ExtensionFailed:
await ctx.error_embed(description="cog failed to load")
await ctx.send(embed=embed)
@cog.command()
@commands.is_owner()
async def remove(ctx: Context, Cog: str):
removed_extensions = []
failed_extensions = []
embed = discord.Embed(title="``removed cog``")
if Cog == "*":
for ext in ctx.bot.COGS:
try:
await ctx.bot.unload_extension(name="cogs." + ext)
except (commands.ExtensionNotLoaded, commands.ExtensionNotFound):
failed_extensions.append(ext)
else:
removed_extensions.append(ext)
embed.description = (
f"removed extensions: {removed_extensions} "
f"{len(removed_extensions)}/{len(ctx.bot.COGS)} \n"
f"failed extensions: {failed_extensions}"
)
await ctx.send(embed=embed)
return
try:
await ctx.bot.unload_extension(name="cogs." + Cog)
except commands.ExtensionNotFound:
await ctx.error_embed("given cog is not valid")
except commands.ExtensionNotLoaded:
await ctx.error_embed("the cog is already not loaded")
embed.description = f"{Cog} cog has been removed successfully"
await ctx.send(embed=embed)
@cog.command()
@commands.is_owner()
async def reload(ctx: Context, Cog: str):
failed_extensions = []
reloaded_extensions = []
embed = discord.Embed(title="``reloaded cogs``")
if Cog == "*":
for cogs in ctx.bot.COGS:
try:
await ctx.bot.reload_extension(name="cogs." + cogs)
except (commands.ExtensionFailed, commands.ExtensionNotLoaded,
commands.ExtensionNotFound):
failed_extensions.append(cogs)
else:
reloaded_extensions.append(cogs)
embed.description = (
f"reloaded cogs: {reloaded_extensions} "
f"``{len(reloaded_extensions)}/{len(ctx.bot.COGS)}``\n"
f"failed to reload: {failed_extensions}"
)
await ctx.send(embed=embed)
return
else:
try:
await ctx.bot.reload_extension(name="cogs." + Cog)
except commands.ExtensionFailed:
await ctx.error_embed(description="failed to load cog")
except commands.ExtensionNotFound:
await ctx.error_embed(description="the extension is not valid")
except commands.ExtensionNotLoaded:
await ctx.error_embed(description="extension is already not loaded")
except commands.ExtensionError:
await ctx.error_embed(description="something went wrong")
embed.title = "``loaded cog``"
embed.description = f"{Cog} successfully loaded!"
await ctx.send(embed=embed)
@commands.is_owner()
@commands.command(name="sync")
async def sync_commands(ctx: Context):
await ctx.bot.tree.sync()
await ctx.send("Done")
async def main():
client = PepeBot()
levels = {
"pepebot": logging.INFO,
"youtube": logging.INFO,
"discord": logging.INFO,
"discord.http": logging.WARNING,
'discord.state': logging.WARNING,
'discord.gateway': logging.WARNING,
"discord.bot": logging.WARNING,
"discord.client": logging.WARNING,
"aiohttp": logging.DEBUG,
"asyncio": logging.DEBUG,
"asyncpg": logging.DEBUG,
"asyncpg.pool": logging.DEBUG
}
# logger, log into file
client.logger = Logger(
base_handler=f"pepebot",
fileName='botconfig/logs/pepebot.log',
is_console_handler=True,
output=sys.stderr,
level=logging.INFO,
loggers=levels
)
client.add_command(cog)
client.add_command(sync_commands)
async with client, client.logger:
try:
_log.info("starting up the bot")
await client.start()
except aiohttp.ClientConnectorError:
_log.info("unable to connect to gateway, maybe offline")
except discord.ConnectionClosed as error:
_log.info(
f"connection closed reason:{error.reason} code: {error.code}")
finally:
_log.warning("terminating bot tasks")
_log.warning("closing down the bot")
await client.close()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
_log.warning(f"task shutdown")