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

标签 python twitch twitch-api

我正在设置一个 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() 将消息发送回 channel 。

我希望可以选择在收到任何消息时将消息发送回 channel (基于 come 标准),而不仅仅是命令。例如:

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 中的 channel

最佳答案

命令向您传递一个Context(Messageable 的一个实例,它定义了send())。 Channel 也是一个Messageable,因此它有一个send()。因此,如果您的 Bot 实例是 bot:

await bot.connected_channels[0].send('...')

将向机器人连接的第一个 channel 发送消息。

关于python - 如何在 TwitchIO 中发送不是对命令的响应的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67348181/

相关文章:

python - 在 Python 中将函数传递给 re.sub

python - Tkinter:如何获取特定单词的索引?

http-live-streaming - 谁能解释一下 MSE DASH 和 HLS 之间的相关性?

javascript - 使用 Twitch API 显示离线/在线流媒体

vue.js - Axios 响应数据替换

python - 如果文件与另一个文件夹匹配,如何返回 1 个文件夹中的所有文件名?

python - "surrogateescape"无法转义某些字符

Python - 需要像对象这样的字节,而不是 str

api - 抽搐螺旋 API : Invalid OAuth Token Error

python-3.x - 如何在discord.py中循环任务