Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Logging #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions cogs/logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import nextcord
from nextcord.ext import commands
import aiosqlite
import asyncio
from datetime import datetime

class Logs(commands.Cog):
def __init__(self, client):
self.client = client

@commands.Cog.listener()
async def on_ready(self):
print("Online!")
setattr(self.client, "db", await aiosqlite.connect("main.db"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, typehint self.client.db

await asyncio.sleep(1)
async with self.client.db.cursor() as cursor:
await cursor.execute("CREATE TABLE IF NOT EXISTS logging (channel INTEGER, guild INTEGER)")
await self.client.db.commit()

@commands.Cog.listener()
async def on_message_delete(self, message):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc, this is dependent on the message being cached, if its not this would probably raise an error since message.author does not exist.

guild = message.guild
async with self.client.db.cursor() as cursor:
await cursor.execute("SELECT channel FROM logging WHERE guild = ?", (guild.id,))
channel = await cursor.fetchone()
msgDelete = self.client.get_channel(channel[0])
embed = nextcord.Embed(title = f"{message.author}'s Message was Deleted", description = f"Deleted Message: {message.content}\nAuthor: {message.author.mention}", timestamp = datetime.now(), color = nextcord.Colour.red())
embed.set_author(name = message.author.name, icon_url = message.author.display_avatar)
embed.set_footer(text=str(message.author.id))
await msgDelete.send(embed=embed)

@commands.Cog.listener()
async def on_message_edit(self, before, after):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick thing, there is a possibility of before being None on any update or edit event.

guild = before.guild
async with self.client.db.cursor() as cursor:
await cursor.execute("SELECT channel FROM logging WHERE guild = ?", (guild.id,))
channel = await cursor.fetchone()
msgEdit = self.client.get_channel(channel[0])
embed = nextcord.Embed(title = f"{before.author} Edited Their Message", description = f"Before: {before.content}\nAfter: {after.content}\nAuthor: {before.author.mention}\n", timestamp = datetime.now(), color = nextcord.Colour.blue())
embed.set_author(name = after.author.name, icon_url = after.author.display_avatar)
embed.set_footer(text=str(before.author.id))
await msgEdit.send(embed=embed)

@commands.Cog.listener()
async def on_member_update(self, before, after):
guild = before.guild
async with self.client.db.cursor() as cursor:
await cursor.execute("SELECT channel FROM logging WHERE guild = ?", (guild.id,))
channel = await cursor.fetchone()
memUpdate = self.client.get_channel(channel[0])
if len(before.roles) > len(after.roles):
role = next(role for role in before.roles if role not in after.roles)
embed = nextcord.Embed(title = f"{before}'s Role has Been Removed", description = f"{role.mention} was removed from {before.mention}.", timestamp = datetime.now(), color = nextcord.Colour.red())
elif len(after.roles) > len(before.roles):
role = next(role for role in after.roles if role not in before.roles)
embed = nextcord.Embed(title = f"{before} Got a New Role", description = f"{role.mention} was added to {before.mention}.", timestamp = datetime.now(), color = nextcord.Colour.green())
elif before.nick != after.nick:
embed = nextcord.Embed(title = f"{before}'s Nickname Changed", description = f"Before: {before.nick}\nAfter: {after.nick}", timestamp = datetime.now(), color = nextcord.Colour.blue())
else:
return
embed.set_author(name = after.name, icon_url = after.display_avatar)
await memUpdate.send(embed=embed)

@commands.Cog.listener()
async def on_guild_channel_create(self, channel):
guild = channel.guild
async with self.client.db.cursor() as cursor:
await cursor.execute("SELECT channel FROM logging WHERE guild = ?", (guild.id,))
channelID = await cursor.fetchone()
chanCreate = self.client.get_channel(channelID[0])
embed = nextcord.Embed(title = f"{channel.name} was Created", description = channel.mention, timestamp = datetime.now(), color = nextcord.Colour.green())
await chanCreate.send(embed=embed)

@commands.Cog.listener()
async def on_guild_channel_delete(self, channel):
guild = channel.guild
async with self.client.db.cursor() as cursor:
await cursor.execute("SELECT channel FROM logging WHERE guild = ?", (guild.id,))
channelID = await cursor.fetchone()
chanDelete = self.client.get_channel(channelID[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wtf camelcase? this should be snakecase iirc

embed = nextcord.Embed(title = f"{channel.name} was Deleted", timestamp = datetime.now(), color = nextcord.Colour.red())
await chanDelete.send(embed=embed)

@commands.group()
async def logs(ctx):
return

@logs.command()
@commands.has_permissions(manage_guild=True)
async def channel(self, ctx, channel: nextcord.TextChannel):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like there is no way to disable logging?
also, the name for this--channel--seems improper.

async with self.client.db.cursor() as cursor:
await cursor.execute("SELECT channel FROM logging WHERE guild = ?", (ctx.guild.id,))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you do know this gives back a list of channel_ids, right?

the filter should include the channel_id, if you really want to be particular.

channelData = await cursor.fetchone()
if channelData:
channelData = channelData[0]
if channelData == channel.id:
return await ctx.send("That's the same channel bro.")
await cursor.execute("UPDATE logging SET channel = ? WHERE guild = ?", (channel.id, ctx.guild.id,))
await ctx.send(f"{channel.mention} is now the logging channel.")
else:
await cursor.execute("INSERT INTO logging VALUES (?, ?)", (channel.id, ctx.guild.id,))
await ctx.send(f"{channel.mention} is now the logging channel.")
await self.client.db.commit()

def setup(client):
client.add_cog(Logs(client))
Empty file added main.db
Empty file.