Skip to content

Commit

Permalink
add logging decorator tools
Browse files Browse the repository at this point in the history
  • Loading branch information
1ntegrale9 committed Dec 3, 2023
1 parent ae05fa9 commit 762281d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
30 changes: 26 additions & 4 deletions daug/utils/dpyexcept.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import datetime
from functools import wraps
import traceback
import discord
from discord.ext import commands
from dotenv import load_dotenv
from os import getenv

def excepter(func):
load_dotenv()
CHANNEL_TRACEBACK_ID = getenv('CHANNEL_TRACEBACK_ID')


def excepter(func, *, _channel_id=None):
@wraps(func)
async def wrapped(self, *args, **kwargs):
try:
return await func(self, *args, **kwargs)
except Exception as e:
orig_error = getattr(e, 'original', e)
message = ''.join(traceback.TracebackException.from_exception(orig_error).format())
appinfo = await self.bot.application_info()
await appinfo.owner.send(f'```python\n{message}\n```')
error_msg = ''.join(traceback.TracebackException.from_exception(orig_error).format())

channel_id = _channel_id or CHANNEL_TRACEBACK_ID
if len(args) >= 1 and isinstance(args[0], discord.Interaction):
interaction: discord.Interaction = args[0]
channel = interaction.client.get_channel(channel_id)
else:
bot: commands.Bot = self.bot
channel = bot.get_channel(channel_id)

if channel:
embed = discord.Embed()
embed.add_field(name='class', value=self.__class__.__name__)
embed.add_field(name='function', value=func.__name__)
embed.add_field(name='datetime', value=datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S.%f'))
await channel.send(f'```python\n{error_msg}\n```', embed=embed)
return wrapped
27 changes: 27 additions & 0 deletions daug/utils/dpylog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from functools import wraps
from datetime import datetime
import discord
from discord.ext import commands
from dotenv import load_dotenv
from os import getenv

load_dotenv()
CHANNEL_LOG_ID = getenv('CHANNEL_LOG_ID')


def dpylogger(func, *, _channel_id=None):
@wraps(func)
async def wrapped(self, *args, **kwargs):
channel_id = _channel_id or CHANNEL_LOG_ID
if len(args) >= 1 and isinstance(args[0], discord.Interaction):
interaction: discord.Interaction = args[0]
channel = interaction.client.get_channel(channel_id)
else:
bot: commands.Bot = self.bot
channel = bot.get_channel(channel_id)

if channel:
content = ' '.join((self.__class__.__name__, func.__name__, datetime.now().strftime('%Y/%m/%d %H:%M:%S.%f')))
await channel.send(content)
await func(self, *args, **kwargs)
return wrapped
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='Daug',
version='2023.2.28.1',
version='2023.12.3.1',
author='Discord Bot Portal JP',
author_email='[email protected]',
description='discord.py を利用した Discord Bot 向け機能拡張ライブラリ',
Expand All @@ -18,10 +18,11 @@
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
install_requires=[
'discord.py >= 2.1.1',
'discord.py >= 2.3.2',
],
)

0 comments on commit 762281d

Please sign in to comment.