【问题标题】:AttributeError: 'User' object has no attribute 'kick'AttributeError:“用户”对象没有属性“踢”
【发布时间】:2022-01-26 21:08:17
【问题描述】:

所以我做了很多研究,找到了很多答案,但没有一个奏效。我试图让我的机器人按命令踢用户,但我不知道正确的语法。我们将不胜感激,谢谢。

intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix='#', intents=intents)

@bot.event
async def on_message(message):
    if '#kick' in message.content.lower():
        mod=discord.utils.get(message.author.guild.roles, name='Mod')
        if mod in message.author.roles:
            await message.channel.send('Who do you want to kick?')
            try:
                message=await bot.wait_for('message', check=lambda m: m.author==message.author and m.channel==message.channel, timeout=15)
            except asyncio.TimeoutError:
                await message.channel.send('Your victim got away')
            else:
                member2id=message.mentions[0].id
                member2=await bot.fetch_user(member2id)
                await member2.kick()
                await message.channel.send(f'{message.content} was kicked')
        else:
            await message.channel.send('You dont have the permission to use that command')
bot.run('token')

【问题讨论】:

标签: python discord


【解决方案1】:

我相信您正在寻找 Member.kick,而不是 User.kick(不存在)。

我相信message.mentions 返回一个Member 对象列表,所以sn-p 可能是:

intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix='#', intents=intents)

@bot.event
async def on_message(message):
    if '#kick' in message.content.lower():
        mod=discord.utils.get(message.author.guild.roles, name='Mod')
        if mod in message.author.roles:
            await message.channel.send('Who do you want to kick?')
            try:
                message=await bot.wait_for('message', check=lambda m: m.author==message.author and m.channel==message.channel, timeout=15)
            except asyncio.TimeoutError:
                await message.channel.send('Your victim got away')
            else:
                await message.mentions[0].kick()
                await message.channel.send(f'{message.content} was kicked')
        else:
            await message.channel.send('You dont have the permission to use that command')
bot.run('token')

但是,请注意Message.mentions 可以为空,并且列表是随机排序的。

另外,discord.py 支持更多kick 功能,请参阅here

【讨论】:

  • 我正在尝试
  • 现在可以了,谢谢。但这是否意味着我可以输入 await message.mentions[0].ban()?这也有用吗?
  • 我的建议是在 discordpy 文档搜索字段 here 中输入 ban。是的,该链接包含Member.ban,所以它似乎是一个函数。我认为它会起作用。
  • 感谢汤姆的帮助
猜你喜欢
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2021-04-19
  • 2021-11-22
相关资源
最近更新 更多