[当有人提到它时,我如何使漫游器响应? discord.py

问题描述 投票:1回答:1

这是我尝试过的代码:

@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.")

但是它似乎不起作用。

discord.py discord.py-rewrite
1个回答
0
投票

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

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")

而且,我注意到您向频道发送消息的方法不太正确。

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

并且此对象有一个称为send()的方法,该方法使您可以发送内容。您会发现一些常见的情况;使用命令修饰符时,将ctx.send()作为第一个参数,将Context作为第一个参数;当您像使用“ C0”事件时,将其作为message.channel.send()。它也会出现在其他一些地方,但这将是最常见的。

您对它是协程有了正确的想法,因此需要将其on_message()。在文档中,它会指出某些东西是否是协程。


参考:

© www.soinside.com 2019 - 2024. All rights reserved.