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
|
|
|
---
|
2020-01-06 23:27:48 -05:00
|
|
|
Copyright (C) 2019-2020 Abigail Gold, 0x5c
|
2019-10-03 22:17:36 -04:00
|
|
|
|
2020-02-15 06:27:48 -05:00
|
|
|
This file is part of qrm2 and is released under the terms of
|
2020-01-31 06:50:50 -05:00
|
|
|
the GNU General Public License, version 2.
|
2019-10-03 22:17:36 -04:00
|
|
|
"""
|
|
|
|
|
2020-01-03 23:03:17 -05:00
|
|
|
|
2020-01-07 05:36:09 -05:00
|
|
|
import asyncio
|
2019-12-23 14:06:42 -05:00
|
|
|
import random
|
2020-02-05 07:09:08 -05:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
from datetime import datetime, time
|
2019-12-23 17:54:20 -05:00
|
|
|
from types import SimpleNamespace
|
2019-12-23 13:29:04 -05:00
|
|
|
|
2019-12-23 15:57:57 -05:00
|
|
|
import pytz
|
|
|
|
|
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
|
|
|
|
2020-02-05 07:09:08 -05:00
|
|
|
import info
|
2019-12-07 11:38:34 -05:00
|
|
|
import common as cmn
|
2020-02-05 07:09:08 -05:00
|
|
|
import utils.connector as conn
|
2020-01-07 05:36:09 -05:00
|
|
|
|
2019-12-07 17:13:06 -05:00
|
|
|
import data.keys as keys
|
2020-02-05 07:09:08 -05:00
|
|
|
import data.options as opt
|
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 ---
|
|
|
|
|
2020-01-07 05:36:09 -05:00
|
|
|
# Loop/aiohttp stuff
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
connector = loop.run_until_complete(conn.new_connector())
|
|
|
|
|
2019-10-08 18:39:05 -04:00
|
|
|
bot = commands.Bot(command_prefix=opt.prefix,
|
|
|
|
description=info.description,
|
2020-01-07 05:36:09 -05:00
|
|
|
help_command=commands.MinimalHelpCommand(),
|
|
|
|
loop=loop,
|
|
|
|
connector=connector)
|
2019-10-04 08:53:05 -04:00
|
|
|
|
2020-01-07 05:36:09 -05:00
|
|
|
# Simple way to access bot-wide stuff in extensions.
|
2019-12-23 17:54:20 -05:00
|
|
|
bot.qrm = SimpleNamespace()
|
2019-10-18 08:27:05 -04:00
|
|
|
|
2020-01-07 05:36:09 -05:00
|
|
|
# Let's store stuff here.
|
|
|
|
bot.qrm.connector = connector
|
2020-01-03 23:03:17 -05:00
|
|
|
bot.qrm.debug_mode = debug_mode
|
|
|
|
|
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
|
|
|
|
2020-01-28 01:23:51 -05:00
|
|
|
@bot.command(name="restart", aliases=["rs"], category=cmn.cat.admin)
|
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
|
2020-01-03 23:03:17 -05:00
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.check_mark)
|
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
|
|
|
|
2020-01-28 01:23:51 -05:00
|
|
|
@bot.command(name="shutdown", aliases=["shut"], category=cmn.cat.admin)
|
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
|
2020-01-03 23:03:17 -05:00
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.check_mark)
|
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
|
|
|
|
2020-01-21 22:34:59 -05:00
|
|
|
@bot.group(name="extctl", aliases=["ex"], category=cmn.cat.admin)
|
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)
|
|
|
|
|
|
|
|
|
2020-01-08 03:47:00 -05:00
|
|
|
@_extctl.command(name="list", aliases=["ls"])
|
2019-12-07 11:38:34 -05:00
|
|
|
async def _extctl_list(ctx: commands.Context):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Lists loaded 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)
|
|
|
|
|
|
|
|
|
2020-01-08 03:47:00 -05:00
|
|
|
@_extctl.command(name="load", aliases=["ld"])
|
2019-12-07 11:38:34 -05:00
|
|
|
async def _extctl_load(ctx: commands.Context, extension: str):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Loads an extension."""
|
2020-01-27 00:37:52 -05:00
|
|
|
bot.load_extension(ext_dir + "." + extension)
|
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.check_mark)
|
2019-12-07 11:38:34 -05:00
|
|
|
|
|
|
|
|
2020-01-08 03:47:00 -05:00
|
|
|
@_extctl.command(name="reload", aliases=["rl", "r", "relaod"])
|
2019-12-07 11:38:34 -05:00
|
|
|
async def _extctl_reload(ctx: commands.Context, extension: str):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Reloads an extension."""
|
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)
|
2020-01-27 00:37:52 -05:00
|
|
|
bot.reload_extension(ext_dir + "." + extension)
|
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.check_mark)
|
2019-12-07 11:38:34 -05:00
|
|
|
|
|
|
|
|
2020-01-08 03:47:00 -05:00
|
|
|
@_extctl.command(name="unload", aliases=["ul"])
|
2019-12-07 11:38:34 -05:00
|
|
|
async def _extctl_unload(ctx: commands.Context, extension: str):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Unloads an extension."""
|
2020-01-27 00:37:52 -05:00
|
|
|
bot.unload_extension(ext_dir + "." + extension)
|
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.check_mark)
|
2019-12-07 11:38:34 -05:00
|
|
|
|
|
|
|
|
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-12-23 14:06:42 -05:00
|
|
|
if opt.status_mode == "time":
|
|
|
|
_ensure_activity_time.start()
|
|
|
|
elif opt.status_mode == "random":
|
|
|
|
_ensure_activity_random.start()
|
|
|
|
else:
|
|
|
|
_ensure_activity_fixed.start()
|
2019-10-04 14:17:45 -04:00
|
|
|
|
|
|
|
|
2019-12-23 11:32:02 -05:00
|
|
|
@bot.event
|
|
|
|
async def on_message(message):
|
|
|
|
msg = message.content.lower()
|
|
|
|
for emoji, keywords in opt.msg_reacts.items():
|
|
|
|
if any([keyword in msg for keyword in keywords]):
|
|
|
|
await message.add_reaction(discord.utils.find(lambda x: x.id == emoji, bot.emojis))
|
|
|
|
|
|
|
|
await bot.process_commands(message)
|
2019-10-04 14:17:45 -04:00
|
|
|
|
|
|
|
|
2020-01-03 23:03:17 -05:00
|
|
|
@bot.event
|
|
|
|
async def on_command_error(ctx: commands.Context, err: commands.CommandError):
|
|
|
|
if isinstance(err, commands.UserInputError):
|
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.warning)
|
|
|
|
await ctx.send_help(ctx.command)
|
2020-01-20 03:49:03 -05:00
|
|
|
elif isinstance(err, commands.CommandNotFound):
|
|
|
|
if ctx.invoked_with.startswith(("?", "!")):
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.question)
|
2020-01-03 23:03:17 -05:00
|
|
|
elif isinstance(err, commands.CheckFailure):
|
|
|
|
# Add handling of other subclasses of CheckFailure as needed.
|
|
|
|
if isinstance(err, commands.NotOwner):
|
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.no_entry)
|
|
|
|
else:
|
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.x)
|
|
|
|
elif isinstance(err, commands.DisabledCommand):
|
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.bangbang)
|
|
|
|
elif isinstance(err, (commands.CommandInvokeError, commands.ConversionError)):
|
|
|
|
# Emulating discord.py's default beaviour.
|
2020-01-30 06:15:42 -05:00
|
|
|
print("Ignoring exception in command {}:".format(ctx.command), file=sys.stderr)
|
2020-01-03 23:03:17 -05:00
|
|
|
traceback.print_exception(type(err), err, err.__traceback__, file=sys.stderr)
|
|
|
|
|
|
|
|
embed = cmn.error_embed_factory(ctx, err.original, bot.qrm.debug_mode)
|
|
|
|
embed.description += f"\n`{type(err).__name__}`"
|
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.warning)
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
else:
|
|
|
|
# Emulating discord.py's default beaviour. (safest bet)
|
2020-01-30 06:15:42 -05:00
|
|
|
print("Ignoring exception in command {}:".format(ctx.command), file=sys.stderr)
|
2020-01-03 23:03:17 -05:00
|
|
|
traceback.print_exception(type(err), err, err.__traceback__, file=sys.stderr)
|
|
|
|
await cmn.add_react(ctx.message, cmn.emojis.warning)
|
|
|
|
|
|
|
|
|
2019-10-04 14:17:45 -04:00
|
|
|
# --- Tasks ---
|
|
|
|
|
|
|
|
@tasks.loop(minutes=5)
|
2019-12-23 14:06:42 -05:00
|
|
|
async def _ensure_activity_time():
|
|
|
|
status = opt.statuses[0]
|
2019-12-23 13:29:04 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
tz = pytz.timezone(opt.status_tz)
|
|
|
|
except pytz.exceptions.UnknownTimeZoneError:
|
2019-12-23 15:56:25 -05:00
|
|
|
await bot.change_presence(activity=discord.Game(name="with invalid timezones."))
|
2019-12-23 13:29:04 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
now = datetime.now(tz=tz).time()
|
|
|
|
|
2019-12-23 14:06:42 -05:00
|
|
|
for sts in opt.time_statuses:
|
2019-12-23 13:29:04 -05:00
|
|
|
start_time = time(hour=sts[1][0], minute=sts[1][1], tzinfo=tz)
|
|
|
|
end_time = time(hour=sts[2][0], minute=sts[2][1], tzinfo=tz)
|
|
|
|
if start_time < now <= end_time:
|
|
|
|
status = sts[0]
|
|
|
|
|
|
|
|
await bot.change_presence(activity=discord.Game(name=status))
|
2019-10-04 13:10:27 -04:00
|
|
|
|
2019-12-23 14:08:05 -05:00
|
|
|
|
2019-12-23 14:06:42 -05:00
|
|
|
@tasks.loop(minutes=5)
|
|
|
|
async def _ensure_activity_random():
|
|
|
|
status = random.choice(opt.statuses)
|
2019-10-04 13:10:27 -04:00
|
|
|
|
2019-12-23 14:06:42 -05:00
|
|
|
await bot.change_presence(activity=discord.Game(name=status))
|
2019-10-04 13:10:27 -04:00
|
|
|
|
|
|
|
|
2019-12-23 14:06:42 -05:00
|
|
|
@tasks.loop(minutes=5)
|
|
|
|
async def _ensure_activity_fixed():
|
|
|
|
status = opt.statuses[0]
|
|
|
|
|
|
|
|
await bot.change_presence(activity=discord.Game(name=status))
|
2019-10-04 14:17:45 -04:00
|
|
|
|
|
|
|
|
2019-10-04 13:10:27 -04:00
|
|
|
# --- Run ---
|
|
|
|
|
2019-12-07 17:26:55 -05:00
|
|
|
for ext in opt.exts:
|
2020-01-30 06:15:42 -05:00
|
|
|
bot.load_extension(ext_dir + "." + ext)
|
2019-10-03 22:17:36 -04:00
|
|
|
|
2019-10-04 14:17:45 -04:00
|
|
|
|
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
|
2020-01-03 23:03:17 -05:00
|
|
|
if bot.qrm.debug_mode:
|
2019-10-03 23:08:08 -04:00
|
|
|
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
|
2020-01-03 23:03:17 -05:00
|
|
|
if bot.qrm.debug_mode:
|
2019-10-03 23:08:08 -04:00
|
|
|
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
|
2020-01-03 23:03:17 -05:00
|
|
|
if bot.qrm.debug_mode:
|
2019-10-03 23:08:08 -04:00
|
|
|
raise
|
|
|
|
raise SystemExit("ConnectionResetError: {}".format(ex))
|
2019-10-06 22:42:44 -04:00
|
|
|
|
2020-01-07 05:36:09 -05:00
|
|
|
|
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)
|