【问题标题】:How do you send a message in TwitchIO that's not a response to a command?如何在 TwitchIO 中发送不是对命令的响应的消息?
【发布时间】:2021-07-24 15:22:11
【问题描述】:

我正在设置一个 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 内部的频道

【问题讨论】:

    标签: python twitch twitch-api


    【解决方案1】:

    这是我想出的答案:

    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)
    

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

    【讨论】:

    • websocket 是不是必须与 TwitchIO 一起加载的依赖项?
    【解决方案2】:

    我找到了替代方案。我用我为此目的发明的命令替换了该消息。命令的名称是“z”,但它可以是任何东西

    async def event_message(self, message):
        if message.echo:
            return
    
        message.content = f"#z {message.content}" # add command name before message
    
        await self.handle_commands(message)
    
    @commands.command()
    async def z(self, ctx: commands.Context):
        print(f"Original message is {ctx.message.content[3:]}\n")
    

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 2022-07-11
      • 2016-01-16
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-15
      相关资源
      最近更新 更多