python - 如何检查我的 Python Discord 机器人是否具有必要的权限?

标签 python discord.py

我制作了一个实用机器人来帮助记录消息并帮助将 channel 从一台服务器迁移到另一台服务器。我需要检查它是否具有执行命令所需的权限,但以下代码不起作用(我使用 @client.event 并且不希望使用 @bot.command )这是我的权限检查代码:

if not client.user.server_permissions.text.read_messages: await send_message(message.channel,"Please give me permission to read messages!")

if not client.user.server_permissions.text.manage_messages: await send_message(message.channel, "Please give me permission to manage messages!")

if not client.user.server_permissions.text.read_message_history: await send_message(message.channel, "Please give me permission to read message history!")

if not client.user.server_permissions.text.attach_files: await send_message(message.channel, "Please give me permission to attach files!")

上面的方法不起作用,当我在适当的条件下测试它时,出现以下错误:

AttributeError: 'User' object has no attribute 'server_permissions'

我哪里出错了?

最佳答案

使用 Python Rewrite (v1.0) 时,命名方案已从 server 更改为 guild。这会将正确的代码更改为:

message.channel.guild.me.guild_permissions

(@patrick-haugh 所示的 UserMember 之间的区别仍然正确)

关于python - 如何检查我的 Python Discord 机器人是否具有必要的权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54279986/

相关文章:

python - Discord.py:希望将 message.content 读为小写,.lower() 不起作用

python - 在 Python 中读取文件并转换为 float

python - 如何根据变量更改不和谐命令

python - 优化生成变量的拒绝方法

python - 使机器人对最后一条消息使用react或对带有消息 ID 的消息作出 react

python - 如何为列表中的每个项目多次调用 discord.py 函数?

python - 如何给一个命令多个名称?

python - Python 中的简单 "if"语句

python - 使用 type() 定义类对象 : Why can't I use the name of an attribute meant to be deepcopied?

python - 查找消息的作者