Catch exceptions in message handler and not command dispatch. Closes #18

This commit is contained in:
Ske 2018-09-15 14:52:24 +02:00
parent 22b206c562
commit fd67945660
3 changed files with 43 additions and 32 deletions

View File

@ -3,15 +3,19 @@ import json
import logging
import os
import time
import traceback
from datetime import datetime
import discord
from pluralkit import db
from pluralkit.bot import channel_logger, commands, proxy
from pluralkit.bot import channel_logger, commands, proxy, embeds
from pluralkit.stats import InfluxStatCollector, NullStatCollector
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s")
# logging.getLogger("pluralkit").setLevel(logging.DEBUG)
class PluralKitBot:
@ -44,11 +48,14 @@ class PluralKitBot:
if message.author.bot:
return
if await self.handle_command_dispatch(message):
return
try:
if await self.handle_command_dispatch(message):
return
if await self.handle_proxy_dispatch(message):
return
if await self.handle_proxy_dispatch(message):
return
except Exception:
await self.log_error_in_channel(message)
async def on_socket_raw_receive(self, msg):
# Since on_reaction_add is buggy (only works for messages the bot's already cached, ie. no old messages)
@ -84,6 +91,22 @@ class PluralKitBot:
async with self.pool.acquire() as conn:
return await self.proxy.try_proxy_message(conn, message)
async def log_error_in_channel(self, message):
channel_id = os.environ["LOG_CHANNEL"]
if not channel_id:
return
channel = self.client.get_channel(channel_id)
embed = embeds.exception_log(
message.content,
message.author.name,
message.author.discriminator,
message.server.id if message.server else None,
message.channel.id
)
await self.client.send_message(channel, "```python\n{}```".format(traceback.format_exc()), embed=embed)
async def periodical_stat_timer(self, pool):
async with pool.acquire() as conn:
while True:

View File

@ -139,26 +139,6 @@ import pluralkit.bot.commands.switch_commands
import pluralkit.bot.commands.system_commands
async def log_error_in_channel(ctx: CommandContext):
channel_id = os.environ["LOG_CHANNEL"]
if not channel_id:
return
channel = ctx.client.get_channel(channel_id)
embed = discord.Embed()
embed.colour = discord.Colour.dark_red()
embed.title = ctx.message.content
embed.set_footer(text="Sender: {}#{} | Server: {} | Channel: {}".format(
ctx.message.author.name, ctx.message.author.discriminator,
ctx.message.server.id if ctx.message.server else "(DMs)",
ctx.message.channel.id
))
await ctx.client.send_message(channel, "```python\n{}```".format(traceback.format_exc()), embed=embed)
async def run_command(ctx: CommandContext, func):
try:
result = await func(ctx)
@ -166,10 +146,6 @@ async def run_command(ctx: CommandContext, func):
await ctx.reply(embed=result.to_embed())
except CommandError as e:
await ctx.reply(embed=e.to_embed())
except Exception as e:
logger.exception("Exception while dispatching command")
await log_error_in_channel(ctx)
async def command_dispatch(client: discord.Client, message: discord.Message, conn) -> bool:

View File

@ -3,14 +3,14 @@ from typing import Tuple
import discord
def success(text: str):
def success(text: str) -> discord.Embed:
embed = discord.Embed()
embed.description = text
embed.colour = discord.Colour.green()
return embed
def error(text: str, help: Tuple[str, str] = None):
def error(text: str, help: Tuple[str, str] = None) -> discord.Embed:
embed = discord.Embed()
embed.description = text
embed.colour = discord.Colour.dark_red()
@ -22,8 +22,20 @@ def error(text: str, help: Tuple[str, str] = None):
return embed
def status(text: str):
def status(text: str) -> discord.Embed:
embed = discord.Embed()
embed.description = text
embed.colour = discord.Colour.blue()
return embed
def exception_log(message_content, author_name, author_discriminator, server_id, channel_id) -> discord.Embed:
embed = discord.Embed()
embed.colour = discord.Colour.dark_red()
embed.title = message_content
embed.set_footer(text="Sender: {}#{} | Server: {} | Channel: {}".format(
author_name, author_discriminator,
server_id if server_id else "(DMs)",
channel_id
))
return embed