Twisted Connection Bot Class的参考功能

时间:2015-12-18 05:53:02

标签: python-2.7 twisted irc twitch

我目前正在开发一个Twitch.tv聊天和审核机器人(完整的代码可以在github上找到:https://github.com/DarkElement75/tecsbot;可能没有完全更新以匹配我描述的问题),并且这样做我需要有许多不同的Twisted TCP连接到各种渠道。我有(因为Twitch's Whisper system工作的方式)一个用于发送/接收耳语的连接,并且需要能够连接到任何通道,可以通过TwitchWhisperBot的write()引用此耳语连接并在此连接上发送数据功能。但是,我还没有找到一种方法,允许我当前的全局函数引用这个write()函数。这就是我现在所拥有的:

#global functions
def send_whisper(self, user, msg):
    whisper_str = "/w %s %s" % (user, msg)
    print dir(whisper_bot)
    print dir(whisper_bot.transport)
    whisper_bot.write("asdf")

def whisper(self, user, msg):
    '''global whisper_user, whisper_msg
    if "/mods" in msg:
        thread.start_new_thread(get_whisper_mods_msg, (self, user, msg))
    else:
        whisper_user = user
        whisper_msg = msg'''
    if "/mods" in msg:
        thread.start_new_thread(get_whisper_mods_msg, (self, user, msg))
    else:
        send_whisper(self, user, msg)
#Example usage of these (inside a channel connection):
send_str = "Usage: !permit add <user> message/time/<time> <message count/time duration/time unit>/permanent" 
whisper(self, user, send_str)

#Whisper classes
class TwitchWhisperBot(irc.IRCClient, object):
    def write(self, msg):
        self.msg(self.channel, msg.encode("utf-8"))
        logging.info("{}: {}".format(self.nickname, msg))

class WhisperBotFactory(protocol.ClientFactory, object):
    wait_time = 1

    def __init__(self, channel):
        global whisper_bot

        self.channel = channel
        whisper_bot = TwitchWhisperBot(self.channel)

    def buildProtocol(self, addr):
        return TwitchWhisperBot(self.channel)

    def clientConnectionLost(self, connector, reason):
        # Reconnect when disconnected
        logging.error("Lost connection, reconnecting")
        self.protocol = TwitchWhisperBot
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        # Keep retrying when connection fails
        msg = "Could not connect, retrying in {}s"
        logging.warning(msg.format(self.wait_time))
        time.sleep(self.wait_time)
        self.wait_time = min(512, self.wait_time * 2)
        connector.connect()
#Execution begins here:
#main whisper bot where other threads with processes will be started
#sets variables for connection to twitch chat

whisper_channel = '#_tecsbot_1444071429976'
whisper_channel_parsed = whisper_channel.replace("#", "")

server_json = get_json_servers()
server_arr = (server_json["servers"][0]).split(":")
server = server_arr[0]
port = int(server_arr[1])

#try:
# we are using this to make more connections, better than threading
# Make logging format prettier
logging.basicConfig(format="[%(asctime)s] %(message)s",
                    datefmt="%H:%M:%S",
                    level=logging.INFO)

# Connect to Twitch IRC server, make more instances for more connections
#Whisper connection
whisper_bot = ''
reactor.connectTCP(server, port, WhisperBotFactory(whisper_channel))

#Channel connections
reactor.connectTCP('irc.twitch.tv', 6667, BotFactory("#darkelement75"))

0 个答案:

没有答案