2019-10-03 22:17:36 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
2019-10-04 11:39:12 -04:00
|
|
|
qrm, a bot for Discord
|
2019-10-03 22:17:36 -04:00
|
|
|
---
|
2019-10-05 02:23:11 -04:00
|
|
|
Copyright (C) 2019 Abigail Gold, 0x5c
|
2019-10-03 22:17:36 -04:00
|
|
|
|
2019-12-08 15:35:58 -05:00
|
|
|
This file is part of discord-qrm2 and is released under the terms of the GNU
|
2019-10-05 02:23:11 -04:00
|
|
|
General Public License, version 2.
|
2019-10-03 22:17:36 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import discord
|
2019-10-04 14:17:45 -04:00
|
|
|
from discord.ext import commands, tasks
|
2019-10-03 22:17:36 -04:00
|
|
|
|
2019-12-07 11:38:34 -05:00
|
|
|
import common as cmn
|
2019-10-03 22:17:36 -04:00
|
|
|
import info
|
|
|
|
|
2019-12-07 17:13:06 -05:00
|
|
|
import data.options as opt
|
|
|
|
import data.keys as keys
|
2019-10-03 22:17:36 -04:00
|
|
|
|
|
|
|
|
2019-10-06 22:42:44 -04:00
|
|
|
# --- Settings ---
|
|
|
|
|
|
|
|
exit_code = 1 # The default exit code. ?shutdown and ?restart will change it accordingly (fail-safe)
|
2019-10-04 08:53:05 -04:00
|
|
|
|
2019-12-07 17:26:55 -05:00
|
|
|
ext_dir = "exts" # The name of the directory where extensions are located.
|
2019-12-07 11:38:34 -05:00
|
|
|
|
2019-10-04 08:53:05 -04:00
|
|
|
debug_mode = opt.debug # Separate assignement in-case we define an override (ternary operator goes here)
|
|
|
|
|
|
|
|
|
|
|
|
# --- Bot setup ---
|
|
|
|
|
2019-10-08 18:39:05 -04:00
|
|
|
bot = commands.Bot(command_prefix=opt.prefix,
|
|
|
|
description=info.description,
|
|
|
|
help_command=commands.MinimalHelpCommand())
|
2019-10-04 08:53:05 -04:00
|
|
|
|
2019-10-18 08:27:05 -04:00
|
|
|
|
2019-10-04 08:53:05 -04:00
|
|
|
# --- Commands ---
|
2019-10-03 22:17:36 -04:00
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
@bot.command(name="restart", hidden=True)
|
2019-12-08 04:10:19 -05:00
|
|
|
@commands.check(cmn.check_if_owner)
|
2019-10-18 08:27:05 -04:00
|
|
|
async def _restart_bot(ctx: commands.Context):
|
2019-10-06 22:42:44 -04:00
|
|
|
"""Restarts the bot."""
|
|
|
|
global exit_code
|
2019-12-08 04:10:19 -05:00
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.good)
|
2019-12-08 16:02:09 -05:00
|
|
|
print(f"[**] Restarting! Requested by {ctx.author}.")
|
2019-10-07 09:11:03 -04:00
|
|
|
exit_code = 42 # Signals to the wrapper script that the bot needs to be restarted.
|
|
|
|
await bot.logout()
|
|
|
|
|
2019-10-06 22:42:44 -04:00
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
@bot.command(name="shutdown", hidden=True)
|
2019-12-08 04:10:19 -05:00
|
|
|
@commands.check(cmn.check_if_owner)
|
2019-10-18 08:27:05 -04:00
|
|
|
async def _shutdown_bot(ctx: commands.Context):
|
2019-10-06 22:42:44 -04:00
|
|
|
"""Shuts down the bot."""
|
|
|
|
global exit_code
|
2019-12-08 04:10:19 -05:00
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.good)
|
2019-12-08 16:02:09 -05:00
|
|
|
print(f"[**] Shutting down! Requested by {ctx.author}.")
|
2019-10-07 09:11:03 -04:00
|
|
|
exit_code = 0 # Signals to the wrapper script that the bot should not be restarted.
|
|
|
|
await bot.logout()
|
2019-10-06 22:42:44 -04:00
|
|
|
|
2019-10-04 13:10:27 -04:00
|
|
|
|
2019-12-07 11:38:34 -05:00
|
|
|
@bot.group(name="extctl", hidden=True)
|
2019-12-08 04:10:19 -05:00
|
|
|
@commands.check(cmn.check_if_owner)
|
2019-12-07 11:38:34 -05:00
|
|
|
async def _extctl(ctx: commands.Context):
|
|
|
|
"""Extension control commands.
|
|
|
|
Defaults to `list` if no subcommand specified"""
|
|
|
|
if ctx.invoked_subcommand is None:
|
|
|
|
cmd = bot.get_command("extctl list")
|
|
|
|
await ctx.invoke(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
@_extctl.command(name="list")
|
|
|
|
async def _extctl_list(ctx: commands.Context):
|
|
|
|
"""Lists Extensions."""
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = "Loaded Extensions"
|
2019-12-07 11:38:34 -05:00
|
|
|
embed.description = "\n".join(["‣ " + x.split(".")[1] for x in bot.extensions.keys()])
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
|
|
|
|
|
|
|
@_extctl.command(name="load")
|
|
|
|
async def _extctl_load(ctx: commands.Context, extension: str):
|
|
|
|
try:
|
|
|
|
bot.load_extension(ext_dir + "." + extension)
|
2019-12-08 04:10:19 -05:00
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.good)
|
2019-12-07 11:38:34 -05:00
|
|
|
except commands.ExtensionError as ex:
|
|
|
|
embed = cmn.error_embed_factory(ctx, ex, debug_mode)
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
|
|
|
|
2019-12-23 20:51:31 -05:00
|
|
|
@_extctl.command(name="reload", aliases=["relaod"])
|
2019-12-07 11:38:34 -05:00
|
|
|
async def _extctl_reload(ctx: commands.Context, extension: str):
|
2019-12-23 20:51:31 -05:00
|
|
|
if ctx.invoked_with == "relaod":
|
|
|
|
pika = bot.get_emoji(opt.pika)
|
|
|
|
if pika:
|
|
|
|
await cmn.add_react(ctx.message, pika)
|
2019-12-07 11:38:34 -05:00
|
|
|
try:
|
|
|
|
bot.reload_extension(ext_dir + "." + extension)
|
2019-12-08 04:10:19 -05:00
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.good)
|
2019-12-07 11:38:34 -05:00
|
|
|
except commands.ExtensionError as ex:
|
|
|
|
embed = cmn.error_embed_factory(ctx, ex, debug_mode)
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
|
|
|
|
|
|
|
@_extctl.command(name="unload")
|
|
|
|
async def _extctl_unload(ctx: commands.Context, extension: str):
|
|
|
|
try:
|
|
|
|
bot.unload_extension(ext_dir + "." + extension)
|
2019-12-08 04:10:19 -05:00
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.good)
|
2019-12-07 11:38:34 -05:00
|
|
|
except commands.ExtensionError as ex:
|
|
|
|
embed = cmn.error_embed_factory(ctx, ex, debug_mode)
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
|
|
|
|
2019-10-04 13:10:27 -04:00
|
|
|
# --- Events ---
|
|
|
|
|
|
|
|
@bot.event
|
|
|
|
async def on_ready():
|
|
|
|
print(f"Logged in as: {bot.user} - {bot.user.id}")
|
|
|
|
print("------")
|
2019-10-04 14:17:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
# --- Tasks ---
|
|
|
|
|
|
|
|
@tasks.loop(minutes=5)
|
|
|
|
async def _ensure_activity():
|
2019-10-08 23:02:57 -04:00
|
|
|
await bot.change_presence(activity=discord.Game(name=opt.game))
|
2019-10-04 13:10:27 -04:00
|
|
|
|
|
|
|
|
2019-10-04 14:17:45 -04:00
|
|
|
@_ensure_activity.before_loop
|
|
|
|
async def _before_ensure_activity():
|
|
|
|
await bot.wait_until_ready()
|
|
|
|
|
|
|
|
|
2019-10-04 13:10:27 -04:00
|
|
|
# --- Run ---
|
|
|
|
|
2019-12-07 17:26:55 -05:00
|
|
|
for ext in opt.exts:
|
2019-12-08 03:26:04 -05:00
|
|
|
bot.load_extension(ext_dir + '.' + ext)
|
2019-10-03 22:17:36 -04:00
|
|
|
|
2019-10-04 14:17:45 -04:00
|
|
|
_ensure_activity.start()
|
|
|
|
|
|
|
|
|
2019-10-03 23:08:08 -04:00
|
|
|
try:
|
|
|
|
bot.run(keys.discord_token)
|
|
|
|
|
2019-10-08 18:39:05 -04:00
|
|
|
except discord.LoginFailure as ex:
|
|
|
|
# Miscellaneous authentications errors: borked token and co
|
2019-10-03 23:08:08 -04:00
|
|
|
if debug_mode:
|
|
|
|
raise
|
|
|
|
raise SystemExit("Error: Failed to authenticate: {}".format(ex))
|
|
|
|
|
2019-10-08 18:39:05 -04:00
|
|
|
except discord.ConnectionClosed as ex:
|
|
|
|
# When the connection to the gateway (websocket) is closed
|
2019-10-03 23:08:08 -04:00
|
|
|
if debug_mode:
|
|
|
|
raise
|
|
|
|
raise SystemExit("Error: Discord gateway connection closed: [Code {}] {}".format(ex.code, ex.reason))
|
|
|
|
|
2019-10-08 18:39:05 -04:00
|
|
|
except ConnectionResetError as ex:
|
|
|
|
# More generic connection reset error
|
2019-10-03 23:08:08 -04:00
|
|
|
if debug_mode:
|
|
|
|
raise
|
|
|
|
raise SystemExit("ConnectionResetError: {}".format(ex))
|
2019-10-06 22:42:44 -04:00
|
|
|
|
|
|
|
# --- Exit ---
|
|
|
|
# Codes for the wrapper shell script:
|
|
|
|
# 0 - Clean exit, don't restart
|
|
|
|
# 1 - Error exit, [restarting is up to the shell script]
|
|
|
|
# 42 - Clean exit, do restart
|
|
|
|
|
|
|
|
raise SystemExit(exit_code)
|