【问题标题】:kick is undefined in discord bot在不和谐机器人中未定义踢
【发布时间】:2021-08-08 00:35:30
【问题描述】:

我制作了一个机器人,在不和谐上 ping @everyone 时会踢人,但问题是我收到了这个错误

NameError: name 'kick' is not defined

代码如下:

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

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

    if message.content.startswith('@everyone'):
        await kick(message.author,reason = "spam")

client.run('censored for obvious reasons')

【问题讨论】:

  • 这能回答你的问题吗? How to kick users on command
  • 不应该包含在discord库中吗?
  • 我不确定为什么它会在全局命名空间中,因为您试图在此处引用它。你读过the documentation吗? kickGuild 对象上可用的方法 - 这是您要使用的方法吗?
  • 哦,谢谢,我是新手
  • @JohnDoe 请检查下面的答案。这是最好的解释。

标签: python discord bots


【解决方案1】:

我认为 cmets 已经提供了很多信息,但无论如何这里有一个可能的解决方案,以防其他人偶然发现这个“错误”。

您的信息不正确,但您已经提供了我们需要的所有信息,只有这些必须在安排中更改。

看看下面的代码:

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

    if message.content.startswith('@everyone'):
        await message.author.kick(reason="Spam")
  • 在这里我们得到message.authorkick 他的reason="Spam"
  • await kick 在这里不起作用,因为机器人不知道要踢谁,即使您已经指定 message.author 作为要踢的人。只是安排错误。

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 2017-04-26
    • 2020-04-29
    • 2020-08-20
    • 2019-08-07
    • 2021-06-19
    • 2021-01-24
    • 2018-02-04
    • 1970-01-01
    相关资源
    最近更新 更多