mirror of
https://github.com/miaowware/qrm2.git
synced 2025-08-01 12:52:27 -04:00
main.py: added checks to restart and shutdown (#27)
- Added a check for ownership of the bot - Added a helper function to add reactions (and handle lack of permissions) Fixes #7
This commit is contained in:
parent
53816ffb8e
commit
959a7609fb
38
main.py
38
main.py
@ -43,35 +43,45 @@ class GlobalSettings(commands.Cog):
|
|||||||
bot = commands.Bot(command_prefix=opt.prefix, description=info.description, help_command=commands.MinimalHelpCommand())
|
bot = commands.Bot(command_prefix=opt.prefix, description=info.description, help_command=commands.MinimalHelpCommand())
|
||||||
|
|
||||||
|
|
||||||
|
# --- Helper functions ---
|
||||||
|
|
||||||
|
async def add_react(msg: discord.Message, react: str):
|
||||||
|
try:
|
||||||
|
await msg.add_reaction(react)
|
||||||
|
except discord.Forbidden:
|
||||||
|
print(f"!! Missing permissions to add reaction in '{msg.guild.id}/{msg.channel.id}'!")
|
||||||
|
|
||||||
|
|
||||||
|
# --- Checks ---
|
||||||
|
|
||||||
|
async def check_if_owner(ctx: commands.Context):
|
||||||
|
if ctx.author.id in opt.owners_uids:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
await add_react(ctx.message, "❌")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
# --- Commands ---
|
# --- Commands ---
|
||||||
|
|
||||||
@bot.command(name="restart")
|
@bot.command(name="restart")
|
||||||
|
@commands.check(check_if_owner)
|
||||||
async def _restart_bot(ctx):
|
async def _restart_bot(ctx):
|
||||||
"""Restarts the bot."""
|
"""Restarts the bot."""
|
||||||
global exit_code
|
global exit_code
|
||||||
if ctx.author.id in opt.owners_uids:
|
await add_react(ctx.message, "✅")
|
||||||
await ctx.message.add_reaction("✅")
|
|
||||||
exit_code = 42 # Signals to the wrapper script that the bot needs to be restarted.
|
exit_code = 42 # Signals to the wrapper script that the bot needs to be restarted.
|
||||||
await bot.logout()
|
await bot.logout()
|
||||||
else:
|
|
||||||
try:
|
|
||||||
await ctx.message.add_reaction("❌")
|
|
||||||
except:
|
|
||||||
return
|
|
||||||
|
|
||||||
@bot.command(name="shutdown")
|
@bot.command(name="shutdown")
|
||||||
|
@commands.check(check_if_owner)
|
||||||
async def _shutdown_bot(ctx):
|
async def _shutdown_bot(ctx):
|
||||||
"""Shuts down the bot."""
|
"""Shuts down the bot."""
|
||||||
global exit_code
|
global exit_code
|
||||||
if ctx.author.id in opt.owners_uids:
|
await add_react(ctx.message, "✅")
|
||||||
await ctx.message.add_reaction("✅")
|
|
||||||
exit_code = 0 # Signals to the wrapper script that the bot should not be restarted.
|
exit_code = 0 # Signals to the wrapper script that the bot should not be restarted.
|
||||||
await bot.logout()
|
await bot.logout()
|
||||||
else:
|
|
||||||
try:
|
|
||||||
await ctx.message.add_reaction("❌")
|
|
||||||
except:
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
# --- Events ---
|
# --- Events ---
|
||||||
|
Loading…
x
Reference in New Issue
Block a user