TwitchIO:如何发送聊天消息? [英] TwitchIO: How to send a chat message?
问题描述
是否可以通过TwitchIO库发送消息?例如,如果时间是10:43 PM,发送一条消息?这既不是事件,也不是来自抽动聊天的消息,而是从应用程序内部完全调用的if/else.我通过阅读一些源代码尝试了下面的代码,但是没有用!也没有错误.
Is there a way to send a message via the TwitchIO library? for example, send a message if the time is 10:43 PM? This is not an event nor a message from twitch chat, that's an if/else totally invoked from within the application. I tried the code below, by reading a bit of its source code, but it didn't work! no errors either.
bot = commands.Bot(...)
bot._ws.send_privmsg(bot.get_channel("some_channel_that_is_connected_to_right_now"),"Time is: "+time.time())
再次
,该机器人可以处理事件,但是我在此问题上找不到其他任何东西!该机器人的文档到处都是
again, the bot works with events, but I wasn't able to find anything else on this matter! the bot's documentation is a little bit all over the place
推荐答案
从TwitchIO不和谐聊天日志中获取一些指针,这些指针曾被问过几次.
Got some pointers from the TwitchIO discord chat logs where this has been asked few times.
由于异步特性,您必须从外部代码向正在运行的事件循环添加一个任务,并引用该机器人,如下所示:
Due to async nature you have to add a task to the running event loop from external code having reference to the bot like this:
chan = bot.get_channel("channelname")
loop = asyncio.get_event_loop()
loop.create_task(chan.send("Send this message"))
这篇关于TwitchIO:如何发送聊天消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!