forked from ManicJamie/HornetBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHornet.py
159 lines (134 loc) · 6.52 KB
/
Hornet.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
import discord
from discord.ext import commands, tasks
from discord.ext.commands import Context, errors
import logging
import time
from datetime import datetime, timedelta
import os
import asyncio
from components import auth, emojiUtil, helpcmd, embeds
import config
import save
# Move old log and timestamp
if not os.path.exists(config.LOG_FOLDER): os.mkdir(f"./{config.LOG_FOLDER}")
if os.path.exists(config.LOG_PATH): os.rename(config.LOG_PATH, f"{config.LOG_FOLDER}/{datetime.utcnow().strftime('%Y-%m-%d_%H-%M-%S_hornet.log')}")
# Setup logging
filehandler = logging.FileHandler(filename=config.LOG_PATH, encoding="utf-8", mode="w")
filehandler.setFormatter(logging.Formatter('[{asctime}] [{levelname:<8}] {name}: {message}', '%Y-%m-%d %H:%M:%S', style='{'))
logging.getLogger().addHandler(filehandler) # file log
discord.utils.setup_logging(level=logging.INFO) # add stream to stderr & set for discord.py
def getParams(cmd: commands.Command):
paramstring = ""
for name, param in cmd.params.items():
if not param.required:
paramstring += f"*<{param.name}>* "
else:
paramstring += f"<{param.name}> "
return cmd.usage if cmd.usage is not None else paramstring
class HornetBot(commands.Bot):
def __init__(self, *args, **kwargs):
emojiUtil.bot = self
# Intents (all)
intents = discord.Intents.default()
intents.message_content = True
intents.presences = True
intents.members = True
self.case_insensitive = True
super().__init__(intents=intents, help_command=helpcmd.HornetHelpCommand(), case_insensitive=True, max_messages=config.cache_size, **kwargs)
async def on_ready(self):
"""Load modules after load"""
modules = []
for file in os.listdir(f"{os.path.dirname(__file__)}/modules/"):
if not file.endswith(".py"): continue
mod_name = file[:-3] # strip .py at the end
modules.append(mod_name)
# Add extensions from /modules/
for ext in modules:
try:
await self.load_extension(f"modules.{ext}")
logging.log(logging.INFO, f"Loaded {ext}")
except commands.ExtensionError as e:
logging.log(logging.ERROR, f"Failed to load {ext}; ignoring")
logging.log(logging.ERROR, e)
save.initModules()
async def on_command_error(self, ctx: Context, error: commands.CommandError):
ectx = embeds.EmbedContext(ctx)
command = ctx.command
cog = ctx.cog
if command and command.has_error_handler(): return
if cog and cog.has_error_handler(): return
if isinstance(error, commands.CommandNotFound):
cmd = ctx.invoked_with
cog = self.get_cog("CustomCommands")
if cog and await cog.try_custom_cmd(ctx, cmd): return
await ectx.embedReply(title=f"Command {ctx.prefix}{cmd} not found")
return
if isinstance(error, commands.CheckFailure):
await ectx.embedReply(title="Not permitted", message="You are not allowed to run this command")
return
cmd = ctx.command
if isinstance(error, commands.UserInputError):
if isinstance(error, commands.MissingRequiredArgument):
await ectx.embedReply(title=f"Command: {ctx.prefix}{cmd} {getParams(cmd)}", \
message=f"Required parameter `{error.param.name}` is missing")
return
typename = type(error).__name__
if typename.endswith("NotFound"):
typename = typename.removesuffix("NotFound")
await ectx.embedReply(title=f"{typename} not found", message=f"{typename} `{error.argument}` could not be found")
return
await ectx.embedReply(title=f"Unhandled exception in Hornet!", \
message=f"```{error.args}```\r\nIf this message doesn't help, consider reporting to <@196347053100630016>")
return await super().on_command_error(ctx, error)
botInstance = HornetBot(command_prefix =';', activity=discord.Game(name="Hollow Knight: Silksong"))
# Base bot commands
@botInstance.command(help="pong!", hidden=True)
async def ping(context: Context):
await context.reply('pong!', mention_author=False)
@botInstance.command(help="pong!", hidden=True)
async def pong(context: Context):
await context.reply('ping!', mention_author=False)
@botInstance.command(help="Time since bot went up", hidden=True)
async def uptime(context: Context):
await embeds.embedReply(context, title="Uptime:", message=f"{timedelta(seconds=int(time.time() - startTime))}")
@botInstance.command(help="Get user's avatar by id")
async def avatar(context: Context, user: discord.User = None):
if user is None: user = context.author
await context.reply(user.display_avatar.url, mention_author=False)
@botInstance.command(help="Add admin role (owner only)")
@commands.check(auth.isOwner)
async def addAdminRole(context: Context, role: discord.Role):
save.getGuildData(context.guild.id)["adminRoles"].append(role.id)
save.save()
await context.message.delete()
@botInstance.command(help="Remove admin role (owner only)")
@commands.check(auth.isOwner)
async def removeAdminRole(context: Context, role: discord.Role):
save.getGuildData(context.guild.id)["adminRoles"].remove(role.id)
save.save()
await context.message.delete()
@botInstance.command(help="Set server nickname in save.json (global admin only)")
@commands.check(auth.isGlobalAdmin)
async def setNick(context: Context, nickname: str):
save.getGuildData(context.guild.id)["nick"] = nickname
save.save()
await context.message.delete()
@botInstance.command(help="Reload modules (global admin only)")
@commands.check(auth.isGlobalAdmin)
async def reloadModules(context: Context):
extensionNames = list(botInstance.extensions.keys())
failed = []
for extension in extensionNames:
try:
await botInstance.reload_extension(extension, package="modules")
logging.log(logging.INFO, f"Loaded {extension}")
except commands.ExtensionError as e:
logging.log(logging.ERROR, f"Failed to load {extension}; ignoring")
logging.log(logging.ERROR, e)
failed.append(extension)
if len(failed) == 0:
await context.reply("Reloaded all modules!", mention_author=False)
else:
await context.reply(f"Modules {', '.join(failed)} failed to reload")
startTime = time.time()
botInstance.run(config.token)