如何在 TwitchIO 中发送不是对命令的响应的消息?

问题描述

我正在设置一个 python TwitchIO 聊天机器人。入门示例是这样的:

async def event_message(self,message):
    print(message.content)
    await self.handle_commands(message)

@commands.command(name='test')
async def my_command(self,ctx):
    await ctx.send(f'Hello {ctx.author.name}!')

如果传入的消息是命令(例如 !test),则会将消息发送回带有 ctx.send() 的通道。

我想要在收到任何消息时(而不仅仅是命令)将消息发送回通道的选项(基于来标准)。例如:

async def event_message(self,message):
    print(message.content)
    if SOMETHING:
        SEND_MESSAGE_HERE
    await self.handle_commands(message)

我不知道如何做某种类型的 .send 来实现这一点。这个答案:TwitchIO: How to send a chat message? 显示这个:

chan = bot.get_channel("channelname")
loop = asyncio.get_event_loop()
loop.create_task(chan.send("Send this message"))

我试过了,机器人立即发送了大量消息并超时了一个小时。

那么,问题是:如何将消息发送回 event_message

内的通道

解决方法

这是我想出的答案:

bot_account_name = "BOT_ACCOUNT_NAME"


async def event_message(self,message):
    print(message.content)

    if message.author.name.lower() != bot_account_name.lower():
        ws = self._ws
        await ws.send_privmsg('CHANNEL_NAME','Message To Send')

    await self.handle_commands(message)

您可以进行其他检查,但根据机器人帐户用户名进行检查很重要。如果您不这样做,机器人将看到自己的消息并回复它们,从而创建一个循环。这是我遇到的问题之一,它立即发送了一堆并因抽搐而超时