1
Fork 0
mirror of https://github.com/allthingslinux/tux.git synced 2024-10-03 00:53:12 +00:00
tux/.archive/on_member_update.py

89 lines
3.2 KiB
Python
Raw Normal View History

2024-01-09 01:27:09 +00:00
# Import the necessary modules
import discord
from discord.ext import commands
2024-01-09 02:01:17 +00:00
2024-01-09 01:27:09 +00:00
from tux.utils.tux_logger import TuxLogger
2024-01-09 01:27:09 +00:00
# Initialize the logger
logger = TuxLogger(__name__)
2024-01-09 01:36:43 +00:00
# TODO:
# Properly account for all important changes
# Ensure that some changes are not using fields (e.g. roles, permissions, etc.)...
# this is because fields are limited in character count
class OnMemberUpdate(commands.Cog):
2024-01-09 01:27:09 +00:00
# Initialize the class with the bot as an argument
def __init__(self, bot):
self.bot = bot
2024-01-09 01:27:09 +00:00
# Define a listener for the 'on_member_update' event
@commands.Cog.listener("on_member_update")
async def on_member_update(self, before: discord.Member, after: discord.Member):
"""
2024-01-09 02:04:27 +00:00
This function is triggered when a member's profile is updated. It
compares the before and after states of the member, logs any changes,
creates an embed for the changes, and sends the embed to a specified
channel.
2024-01-10 04:05:13 +00:00
""" # noqa E501
2024-01-09 01:27:09 +00:00
try:
# Compare the before and after states of the member
changes = self.compare_member_changes(before, after)
if changes:
# Log the changes
self.log_member_changes(changes)
# Create an embed for the changes
embed = self.create_embed_for_changes(changes)
# Send the embed to a specified channel
await self.send_embed(embed)
except Exception as e:
# Log any errors that occur
logger.error(f"Error handling member update: {e}")
2024-01-09 01:27:09 +00:00
def compare_member_changes(self, before, after):
"""
This function compares the before and after states of a member.
It currently only checks for changes in nickname, but more comparisons can be added as needed.
"""
changes = {}
logger.info(f"Comparing {before} to {after}.")
2024-01-09 01:27:09 +00:00
if before.nick != after.nick:
2024-01-09 01:36:43 +00:00
changes["nickname"] = {"before": before.nick, "after": after.nick}
2024-01-09 01:27:09 +00:00
return changes
2024-01-09 01:27:09 +00:00
def log_member_changes(self, changes):
"""
This function logs any changes that have occurred.
"""
for change, values in changes.items():
logger.info(f"{change} changed from {values['before']} to {values['after']}")
2024-01-09 01:27:09 +00:00
def create_embed_for_changes(self, changes):
"""
This function creates an embed for the changes that have occurred.
"""
2024-01-09 01:36:43 +00:00
embed = discord.Embed(
title="Member Update", description="A member has updated their profile."
)
2024-01-09 01:27:09 +00:00
for change, values in changes.items():
2024-01-09 01:36:43 +00:00
embed.add_field(name=f"Old {change}", value=values["before"], inline=False)
embed.add_field(name=f"New {change}", value=values["after"], inline=False)
logger.info("Embed created.")
2024-01-09 01:27:09 +00:00
return embed
2024-01-09 01:27:09 +00:00
async def send_embed(self, embed):
channel_id = 1191472088695980083
channel = self.bot.get_channel(channel_id)
await channel.send(embed=embed)
logger.info(f"Embed sent to channel {channel_id}.")
2024-01-09 01:36:43 +00:00
2024-01-09 01:27:09 +00:00
# Define an asynchronous setup function that adds the OnMemberUpdate cog to the bot
async def setup(bot):
await bot.add_cog(OnMemberUpdate(bot))