Python 3 - Discord 机器人

Python 3 - Discord Bot

我正在制作一个 discord 机器人,我希望它能够清除服务器中的消息。我有代码,但是当我 运行 它时,它会询问我的权限。如何授予机器人删除频道消息的权限?

您的机器人用户帐户需要 MANAGE_MESSAGES permission 在特定的 server/guild 上 运行 命令才能删除消息。这需要由服务器管理员在安装您的机器人时设置(通常,它是通过机器人使用的自定义角色完成的)。您可以检查以确保您拥有它的角色,如下所示:

# 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