discord - “成员”对象没有属性“hasPermission”
问题描述
我想制作一个机器人来轻松删除消息。它在 3 个月前有效,但现在我收到一个错误“'Member' 对象没有属性 'hasPermission'”。感谢大家分享意见。祝你今天过得愉快。
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix = 'botcuk')
@bot.event
async def on_ready():
await bot.change_presence(activity=discord.Streaming(name="admin biseyler deniyor", url='https://www.youtube.com/watch?v=fw7L7ZO4z_A'))
if message.content.startswith('botcuksil'):
if (message.author.hasPermission('MANAGE_MESSAGES')):
args = message.content.split(' ')
if len(args) == 2:
if args[1].isdigit():
count = int(args[1]) + 1
deleted = await message.channel.purge(limit = count)
await message.channel.send('{} mesaj silindi'.format(len(deleted)-1))
解决方案
它抛出'Member' object has no attribute 'hasPermission'
此错误是因为“成员”没有名为“hasPermission”的属性。为了解决您的问题,我重新编写了您的代码,并在代码下方给出了一些解释:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix = 'botcuk')
@bot.event
async def on_ready():
await bot.change_presence(activity=discord.Streaming(name="admin biseyler deniyor", url='https://www.youtube.com/watch?v=fw7L7ZO4z_A'))
@client.event # we need to add and check for a client event
async def on_message(): # we need to execute the command when we recieve an message so we define a function called 'async def on_message():'
if message.content.startswith('botcuksil'):
if message.author.guild_permissions.manage_messages: # we need 'author.guild_permissions.(permission)' intead of 'if (message.author.hasPermission('MANAGE_MESSAGES'))'
args = message.content.split(' ')
if len(args) == 2:
if args[1].isdigit():
count = int(args[1]) + 1
deleted = await message.channel.purge(limit = count)
await message.channel.send('{} mesaj silindi'.format(len(deleted)-1))
我尚未对此进行测试,但据我所知,您的问题:'Member' object has no attribute 'hasPermission'
应该解决。
您所做的是message.author.hasPermission('MANAGE_MESSAGES')
但是 message.author 没有像hasPermission
.
我所做的总结:
我添加了一个客户端事件 ( @client.event
) 和一个异步函数on_message()
。
它的作用是检查是否收到消息。如果它在受邀访问的任何服务器中检测到新消息,则会触发on_message()
.
正如我所说:你所做的是message.author.hasPermission('MANAGE_MESSAGES')
但message.author
没有像hasPermission
.
我们可以使用message.author.guild_permissions.manage_messages
. 它检查消息作者的服务器的权限是否包括“manage_messages”。希望我的解决方案能解决您的问题。
总是乐于助人!-sqd山
推荐阅读
- python-2.7 - Python按唯一ID转置行
- amazon-web-services - 所有 IAM 服务上的 AWS iam:ListRolePolicies 权限错误
- python - 数据科学:定量和定性变量之间的相关性(在 python 中)
- html - 从文件选择对话框中选择文件时使用我自己的产品图标而不是浏览器图标
- elixir - 为什么 (true || false && false) 计算结果为 true?
- postgresql - 可以更改 Postgresql 的 SSL 证书的默认位置吗?
- r - R igraph 关联矩阵到图对象
- android - 如何使 GCM / FCM 通知类型的消息不可折叠
- java - 日志显示 TLSv1 而不是 SSLv3
- java - 在 Java RabbitMQ 中动态创建 RabbitListner