Reputation: 43
I am making a discord bot, and I want it to clear the messages in a server. I have the code , but when I run it, it asks me for permissions. How to give the bot permissions to delete channel messages?
Upvotes: 0
Views: 1040
Reputation: 2821
Your bot user account needs to have the MANAGE_MESSAGES
permission on the particular server/guild you're running the command on in order to delete messages. This needs to be set by the server manager when they install your bot (usually, it's done with a custom role that the bot uses). You can check to make sure you have the role for it as follows:
# get your bot's guild member object first (that's outside the scope of this post)
# we'll call the guild member object "member" here...
# MANAGE_MESSAGES permission is 0x00002000, we'll use a bitwise AND to see if the
# bot has a role with the MANAGE_MESSAGES permission.
if not filter(lambda role: role.permissions & 0x00002000 != 0, member.roles):
# error handling, possibly send a message saying the bot needs permissions
else:
# delete messages using your method
Upvotes: 2