gpt4 book ai didi

discord.py - 当有人提到它时,我如何让机器人做出回应?不和谐.py

转载 作者:行者123 更新时间:2023-12-04 16:26:51 35 4
gpt4 key购买 nike

这是我试过的代码:

@client.event
async def on_message(message):
if client.user.mention in message.content.split():
await client.say("You can type `!vx help` for more info.")

但它似乎不起作用。

最佳答案

使用命令装饰器时,您可以执行以下操作:

from discord.ext import commands # necessary for this task

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

或者使用 on_message()事件,这是您可以检查提及的多种方式之一:

@client.event
async def on_message(message):
if client.user.mentioned_in(message):
await message.channel.send("You can type `!vx help` for more info")

另外,我注意到您向 channel 发送消息的方法不太正确。

在 d.py rewrite (v1.x) 中,您有一个 abc.Messageable对象,顾名思义,它类似于服务器的文本 channel 、DM 或群聊。

而这个对象有一个方法叫做 send()它允许您发送内容。您会发现这种情况的一些常见情况是; ctx.send()当你使用命令装饰器时 - 他们有 Context作为第一个参数 - 和 message.channel.send()当您使用 on_message() 时像你这样的事件。它也会出现在其他一些地方,但这些将是最常见的。

你对它是一个协程有正确的想法,因此需要 await它。在文档中,它会说明某些东西是否是协程。

引用文献:
  • commands.when_mentioned_or()
  • ClientUser.mentioned_in()
  • abc.Messageable - 看看你能做什么send()消息到。
  • commands.Context - 这继承自 abc.Messageable .
  • Messageable.send()
  • 关于discord.py - 当有人提到它时,我如何让机器人做出回应?不和谐.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62239816/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com