Search code examples
python-2.7twistedirctwitch

Reference function of Twisted Connection Bot Class


I am currently working on developing a Twitch.tv chat and moderation bot(full code can be found on github here: https://github.com/DarkElement75/tecsbot; might not be fully updated to match the problem I describe), and in doing this I need to have many different Twisted TCP connections to various channels. I have (because of the way Twitch's Whisper system works) one connection for sending / receiving whispers, and need the ability for any connection to any channel can reference this whisper connection and send data on this connection, via the TwitchWhisperBot's write() function. However, I have yet to find a method that allows my current global function to reference this write() function. Here is what I have right now:

#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"))

Solution

  • The simplest solution here would be using the Singleton pattern, since you're guaranteed to only have a single connection of each type at any given time. Personally, with Twisted, I think the simplest solution is to use the reactor to store your instance (since reactor itself is a singleton).

    So what you want to do is, inside TwitchWhisperBot, at sign in:

    def signedOn(self):
        reactor.whisper_instance = self
    

    And then, anywhere else in the code, you can access that instance:

    reactor.whisper_instance = self
    

    Just for sanity's sake, you should also check if it's been set or not:

    if getattr(reactor, 'whisper_instance'):
        reactor.whisper_instance.write("test_user", "message")
    else:
        logging.warning("whisper instance not set")