首页 > 解决方案 > “成员”对象没有属性“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))

标签: discord

解决方案


它抛出'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山


推荐阅读