【问题标题】:Why doesn't my discord bot kick command work?为什么我的 discord bot kick 命令不起作用?
【发布时间】:2021-07-18 11:28:06
【问题描述】:

当我在 Discord 中键入命令“.kick @user”时,我一直试图让我的机器人踢出成员,我参考了许多 YouTube 视频,它们都指向相同的代码,但事实并非如此为我工作。那么,我该如何踢会员呢?

client = commands.Bot(command_prefix = ".")

# To kick user
@client.command()
@commands.has_permissions(kick_members = True)
async def kick(ctx, member : discord.Member = None, *, reason = None):
  await member.kick(reason = reason)

【问题讨论】:

标签: python discord.py


【解决方案1】:

看起来 Intent 丢失了,并且您的 on_message 事件使命令停止工作。

意图:

确保在您的应用程序的Discord Developer Portal 中打开它们。

您可以在这里找到它们: 导航到您的应用程序 -> 机器人 -> 向下滚动 -> 打勾

要将它们实现到您的代码中,您可以使用以下代码:

intents = discord.Intents.all() # Imports all the Intents
client = commands.Bot(command_prefix="YourPrefix", intents=intents)

或者只是成员 意图找到/踢成员:

intents = discord.Intents.default() # Imports the default intents
intents.members = True
client = commands.Bot(command_prefix="YourPrefix", intents=intents) 

您也可以查看docs 了解更多信息

对于on_message 事件:

您正在覆盖默认的on_message 事件。要解决此问题,只需将以下内容添加到您的代码中:

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    greeting1 = re.compile(f"hello <@!?{client.user.id}>")
    if greeting1.match(message.content.lower()) is not None:
        await message.channel.send("Hello {}".format(message.author.mention) + "!")
    if greeting1.match(message.content.upper()) is not None:
        await message.channel.send("Hello {}".format(message.author.mention) + "!")

    greeting2 = re.compile(f"hey <@!?{client.user.id}>")
    if greeting2.match(message.content.lower()) is not None:
        await message.channel.send("Hey {}".format(message.author.mention) + "!")
    if greeting2.match(message.content.upper()) is not None:
        await message.channel.send("Hey {}".format(message.author.mention) + "!")

    await client.process_commands(message) # Allows commands to work

确保以正确的方式缩进代码!

【讨论】:

【解决方案2】:

这可能无法正常工作的三个原因,首先是您没有启用意图,您可以通过这种方式启用它们:

intents = discord.Intents.default()
intents.members = True

client = commands.Bot(intents=intents, command_prefix=".")

另一个是可能bot没有足够的权限踢用户,第三个是简单的用户角色高于bot角色。该命令做得很好,所以不必担心更改此代码中的内容。

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 2020-08-07
    • 2021-01-05
    • 2018-03-04
    • 2021-03-01
    • 2019-07-31
    • 1970-01-01
    • 2020-08-16
    • 2021-03-23
    • 2021-01-02
    相关资源
    最近更新 更多