Discord.py 当有人提到机器人时,我如何让它做出反应?不和谐
以下是我尝试过的代码:Discord.py 当有人提到机器人时,我如何让它做出反应?不和谐,discord.py,discord.py-rewrite,Discord.py,Discord.py Rewrite,以下是我尝试过的代码: @client.event 异步def on_消息(消息): 如果message.content.split()中的client.user.notice: 等待客户。说(“您可以键入`!vx help`了解更多信息。”) 但它似乎不起作用。使用命令装饰器时,您可以执行以下操作: 来自discord.ext导入命令#此任务所需 client=commands.Bot(command\u前缀=commands.when\u提到\u或(“!”)) 或者使用on_messag
@client.event
异步def on_消息(消息):
如果message.content.split()中的client.user.notice:
等待客户。说(“您可以键入`!vx help`了解更多信息。”)
但它似乎不起作用。使用命令装饰器时,您可以执行以下操作:
来自discord.ext导入命令#此任务所需
client=commands.Bot(command\u前缀=commands.when\u提到\u或(“!”))
或者使用on_message()
事件,这是检查提及内容的多种方法之一:
@client.event
异步def on_消息(消息):
如果(消息)中提到client.user.\u:
wait message.channel.send(“您可以键入`!vx help`了解更多信息”)
另外,我注意到你向频道发送消息的方法不太正确
在d.py rewrite(v1.x)中,有一个abc.Messageable
对象,顾名思义,它类似于服务器的文本频道,或者DM或群组聊天
这个对象有一个名为send()
的方法,允许您发送内容。一些常见的情况下,你会发现这将是ctx.send()
当您使用命令装饰器时-它们将Context
作为第一个参数-并且message.channel.send()
当您像您一样使用on_message()
事件时。它也会出现在其他一些地方,但这将是最常见的
您对它是一个协同程序的想法是正确的,因此需要等待它。在文档中,它将声明某个东西是否是协同程序
参考文献:
- -查看您可以
发送的消息send()
- -这继承自abc.Messageable
@client.event
async def on_message(message):
if client.user.mention in message.content:
await message.channel.send("You can type `!vx help` for more info.")
下面是一个使用类的类似解决方案。我在具体情况上遇到了一些问题,最终设法解决了,但找不到这样的例子。关键是消息中的
if self.user。提到:
行
消息。提及
指的是用户列表,您可以查看自己或其他人
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
# Don't respond to ourselves
if message.author == self.user:
return
# If bot is mentioned, reply with a message
if self.user in message.mentions:
await message.channel.send("You can type `!vx help` for more info.")
return
def main():
client = MyClient()
client.run(DISCORD_TOKEN)
if __name__ == "__main__":
main()
你能详细说明一下它怎么不适合你吗?你有错误吗?你学过什么教程吗?
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
# Don't respond to ourselves
if message.author == self.user:
return
# If bot is mentioned, reply with a message
if self.user in message.mentions:
await message.channel.send("You can type `!vx help` for more info.")
return
def main():
client = MyClient()
client.run(DISCORD_TOKEN)
if __name__ == "__main__":
main()