gpt4 book ai didi

python - 如何使用twisted每隔几秒发送IRC消息?

转载 作者:行者123 更新时间:2023-12-01 05:52:39 24 4
gpt4 key购买 nike

扭曲的文档提供了 how to create an IRC bot 的示例

这是我当前拥有的代码(来自上面的示例):

from twisted.words.protocols import irc
from twisted.internet import protocol
from twisted.internet import reactor

class Bot(irc.IRCClient):
def _get_nickname(self):
return self.factory.nickname
nickname = property(_get_nickname)

def signedOn(self):
self.join(self.factory.channel)
print "Signed on as %s." % (self.nickname,)

def joined(self, channel):
print "Joined %s." % (channel,)

def privmsg(self, user, channel, msg):
print msg

class BotFactory(protocol.ClientFactory):
protocol = Bot

def __init__(self, channel, nickname='test-nick-name'):
self.channel = channel
self.nickname = nickname

def clientConnectionLost(self, connector, reason):
print "Lost connection (%s), reconnecting." % (reason,)
connector.connect()

def clientConnectionFailed(self, connector, reason):
print "Could not connect: %s" % (reason,)


if __name__ == "__main__":

channel = '#test-channel-123'
reactor.connectTCP('irc.freenode.net', 6667, BotFactory(channel))
reactor.run()

现在我想添加每 5 秒向 channel 发送一条消息的功能。我该如何去做呢?如何从外部获取 Bot.msg 方法的句柄?

最佳答案

sending a message say every 5 seconds to the channel

看看LoopingCall ,您可以使用它每n秒调用一个方法。

from twisted.internet import task

task.LoopingCall(yourSendingMethodHere).start(5.0)
<小时/>

How do I get the handle to the Bot.msg method from outside?

这取决于你。您创建 BotFactory 的实例,每个 Bot 都有对其工厂的引用。

关于python - 如何使用twisted每隔几秒发送IRC消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13622457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com