thegamecracks / theticketbot

A ticket bot built on Discord's native threads.
MIT License
2 stars 0 forks source link

Non-existent members and roles can become permanently listed as inbox staff #4

Closed thegamecracks closed 4 months ago

thegamecracks commented 4 months ago

Describe the bug

When a member leaves the server or a role is deleted, any inboxes that have the member/role listed as staff become stuck in that inbox. The admin cannot remove members until they re-join, and deleted roles are impossible to remove from staff. The inbox must be deleted and re-created in order to clear the staff list.

To Reproduce

Steps to reproduce the behavior:

  1. Add a member or role to an inbox
  2. Kick the member or delete the role
  3. Create a ticket with that inbox

Expected behavior

When a member leaves or a role is deleted, they should be removed from any inboxes that list them.

Version Information

thegamecracks commented 4 months ago

For real-time updates, we can check for members that leave and roles that are deleted, on_member_remove and on_guild_role_delete respectively. In these events, we will only know which guild the member/role was in, and the member/role's ID. As such, the database queries to remove them from inbox staff might look like:

SELECT inbox_id, mention FROM inbox_staff
    JOIN inbox   ON inbox.id   = inbox_id
    JOIN message ON message.id = inbox.id
    JOIN channel ON channel.id = channel_id
    JOIN guild   ON guild.id   = guild_id
WHERE guild_id = ? AND mention = ?;

DELETE FROM inbox_staff WHERE inbox_id = ? AND mention = ?;

However, like the other cleanup events, this is not a perfect solution as staff can be removed while the bot is offline. Instead, we could filter out inbox staff at the moment of its usage, including when a new ticket is created and when the admin uses the /inbox staff list command. For this to work, it needs to know which members and roles are still in the guild.

For roles, this is simple because discord.py already caches them. However, I specifically disabled the member cache as it takes up a significant amount of memory/bandwidth with data that we aren't interested in. Instead, we may have to fetch each member on demand and manually cache them, along with tracking member events.

thegamecracks commented 4 months ago

This issue now has a new workaround thanks to the new staff command. Discord's select menu filters out deleted members/roles for us, meaning they will be cleared out the next time an admin makes a change to the inbox's staff. It still requires manual intervention and can be inconvenient when there are multiple inboxes, but this at least means their presence won't be permanent.

Probably still worth filtering out roles at usage, so I'll keep the issue open.