Add error logging to a channel. Closes #11.

This commit is contained in:
Ske 2018-09-07 18:17:30 +02:00
parent a2cbf20add
commit 7b99a89bb9
2 changed files with 26 additions and 26 deletions

View File

@ -79,31 +79,6 @@ class PluralKitBot:
result = await commands.command_dispatch(self.client, message, conn) result = await commands.command_dispatch(self.client, message, conn)
return result return result
"""command_items = commands.command_list.items()
command_items = sorted(command_items, key=lambda x: len(x[0]), reverse=True)
prefix = "pk;"
for command_name, command in command_items:
if message.content.lower().startswith(prefix + command_name):
args_str = message.content[len(prefix + command_name):].strip()
args = args_str.split(" ")
# Splitting on empty string yields one-element array, remove that
if len(args) == 1 and not args[0]:
args = []
async with self.pool.acquire() as conn:
time_before = time.perf_counter()
await command.function(self.client, conn, message, args)
time_after = time.perf_counter()
# Report command time stats
execution_time = time_after - time_before
response_time = (datetime.now() - message.timestamp).total_seconds()
await self.stats.report_command(command_name, execution_time, response_time)
return True"""
async def handle_proxy_dispatch(self, message): async def handle_proxy_dispatch(self, message):
# Try doing proxy parsing # Try doing proxy parsing
async with self.pool.acquire() as conn: async with self.pool.acquire() as conn:

View File

@ -1,6 +1,9 @@
import os
import discord import discord
import logging import logging
import re import re
import traceback
from typing import Tuple, Optional from typing import Tuple, Optional
from pluralkit import db, System, Member from pluralkit import db, System, Member
@ -115,6 +118,26 @@ import pluralkit.bot.commands.switch_commands
import pluralkit.bot.commands.system_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,
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): async def run_command(ctx: CommandContext, func):
try: try:
result = await func(ctx) result = await func(ctx)
@ -122,9 +145,11 @@ async def run_command(ctx: CommandContext, func):
await ctx.reply(embed=result.to_embed()) await ctx.reply(embed=result.to_embed())
except CommandError as e: except CommandError as e:
await ctx.reply(embed=e.to_embed()) await ctx.reply(embed=e.to_embed())
except Exception: except Exception as e:
logger.exception("Exception while dispatching command") logger.exception("Exception while dispatching command")
await log_error_in_channel(ctx)
async def command_dispatch(client: discord.Client, message: discord.Message, conn) -> bool: async def command_dispatch(client: discord.Client, message: discord.Message, conn) -> bool:
prefix = "^pk(;|!)" prefix = "^pk(;|!)"