Files
botpy/c/log.py
2025-11-26 19:34:08 +05:00

65 lines
2.5 KiB
Python

import discord
from discord.ext import commands
from discord.ext.commands import Context
import random
class Logger(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.last_member = None
@commands.Cog.listener()
async def on_message_delete(self, context = Context):
if context.author.bot:
return
try:
res = await self.bot.ch(server_id=context.guild.id)
print("------------------------------------\ndeleted message: ", f'{context.content}\n------------------------------------')
embed = discord.Embed(
color=random.randint(0, 0xFFFFFF),
title='Message deleted'
)
embed.add_field(name=f'', value=f'[link]({context.jump_url})', inline=False)
embed.set_author(name=context.author.name, icon_url = context.author.avatar.url)
embed.add_field(name='content:', value=f'{context.content}')
if context.attachments:
a = 0
for attachment in context.attachments:
a = a + 1
embed.add_field(name=f"attachment #{a}", value=attachment, inline = False)
embed.add_field(name='', value=f'#{context.channel}', inline = False)
channel = self.bot.get_channel(int(res))
await channel.send(embed=embed)
except:
pass
@commands.Cog.listener()
async def on_message_edit(self, message_before, message_after):
if message_before.author.bot:
return
try:
res = await self.bot.ch(server_id=message_before.guild.id)
embed = discord.Embed(
color=random.randint(0, 0xFFFFFF),
title='Message edited'
)
embed.add_field(name=f'', value=f'[link]({message_before.jump_url})', inline=False)
embed.set_author(name=message_before.author.name, icon_url = message_before.author.avatar.url)
embed.add_field(name='before:', value=f'{message_before.content}')
embed.add_field(name='after:', value=f'{message_after.content}')
embed.add_field(name='', value=f'#{message_before.channel}', inline = False)
channel = self.bot.get_channel(int(res))
await channel.send(embed=embed)
except:
pass
@commands.Cog.listener()
async def on_ready(self):
print('Logger cog has been loaded')
async def setup(bot):
await bot.add_cog(Logger(bot))