I’m trying to make a Discord bot using Python that can track everything happening in a server. I need it to save all chat messages, when people edit or delete stuff, and when new invite links get made. The bot should write all this info to a file right away when it happens.
I found something called .on_message() but I’m not sure how to grab the actual message content from it. Has anyone done something like this before?
This is for our school club’s Discord server. The teachers want to be able to monitor conversations between staff and students, so we need a way to keep records of everything that gets posted.
Any help would be great!
discord.py makes this pretty straightforward. Use message.content like the other person mentioned, but you need to enable message content intent in your bot settings first - otherwise it won’t work. Just a heads up though, logging everything could violate Discord’s ToS so be careful about that.
Built something like this for a gaming server last year. The hard part isn’t catching the events - it’s writing files without blocking your bot. Use threading or asyncio for that. Message edits are handy since on_message_edit gives you both the old and new versions. Learned this the hard way: add proper error handling because Discord’s API flakes out sometimes. Also make sure your bot has admin perms or at least channel-specific permissions, otherwise you’ll get crashes from permission errors.
To create a Discord bot that logs server activity, start by utilizing the .on_message() event in your bot’s code to capture incoming messages. You can access the message content with message.content. To log edits and deletions, incorporate the .on_message_edit() and .on_message_delete() events respectively. For tracking new invite links, use .on_invite_create() and .on_invite_delete() methods. Ensure your bot has the necessary permissions, such as ‘Read Message History’ and ‘Manage Server’. For effective logging, consider using a queuing mechanism when writing to files to mitigate rate limits. Always ensure compliance with both your school’s policies and Discord’s guidelines concerning data logging.