kuuwang
kuuwang

Reputation: 1

How to join multiple channels using twitchdev python code

I'm trying to make translate bot using twitchdev python code: https://github.com/twitchdev/chat-samples/blob/master/python/chatbot.py

I was able to get messages from channels and send translated text, but I cannot join multiple channels.

What I did below is making list of channels and call using for loop, but it only join to the last channel.

I tried to make another list of channels in def on_welcome(self, c, e) but it also works on the last channel (when I print self.channels in def on_welcome(self, c, e) it printed blank list, and when I print self.channel it only printed last channel)

Any suggestions are welcome.

import sys
import irc.bot
import requests
import config

class TwitchBot(irc.bot.SingleServerIRCBot):
    def __init__(self, username, client_id, token, channels):
        for channel in channels
            self.client_id = client_id
            self.token = token
            self.channel = '#' + channel

            # Get the channel id, we will need this for v5 API calls
            url = 'https://api.twitch.tv/kraken/users?login=' + channel
            headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
            r = requests.get(url, headers=headers).json()
            self.channel_id = r['users'][0]['_id']

            # Create IRC bot connection
            server = 'irc.chat.twitch.tv'
            port = 6667
            print 'Connecting to ' + server + ' on port ' + str(port) + '...'
            irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+token)], username, username)


    def on_welcome(self, c, e):
        print 'Joining ' + self.channel

        # You must request specific capabilities before you can use them
        c.cap('REQ', ':twitch.tv/membership')
        c.cap('REQ', ':twitch.tv/tags')
        c.cap('REQ', ':twitch.tv/commands')
        c.join(self.channel)

    def on_pubmsg(self, c, e):

        # If a chat message starts with an exclamation point, try to run it as a command
        if e.arguments[0][:1] == '!':
            cmd = e.arguments[0].split(' ')[0][1:]
            print 'Received command: ' + cmd
            self.do_command(e, cmd)
        return

    def do_command(self, e, cmd):
        c = self.connection

        # Provide basic information to viewers for specific commands
        elif cmd == "raffle":
            message = "This is an example bot, replace this text with your raffle text."
            c.privmsg(self.channel, message)
        elif cmd == "schedule":
            message = "This is an example bot, replace this text with your schedule text."            
            c.privmsg(self.channel, message)


def main():
    if len(sys.argv) != 5:
        print("Usage: twitchbot <username> <client id> <token> <channel>")
        sys.exit(1)

    username  = config.twitch['botname']
    client_id = config.twitch['cliendID']
    token     = config.twitch['oauth']
    channels   = ["channel1", "channel2"]

    bot = TwitchBot(username, client_id, token, channels)
    bot.start()

if __name__ == "__main__":
    main()

Upvotes: 0

Views: 1546

Answers (1)

Xinthral
Xinthral

Reputation: 437

In your main() function, you are establishing an object of the bot:

bot = TwitchBot(username, client_id, token, channels)

Instead of passing multiple channels to the same bot object, create multiple bot objects with single channels. I haven't tested this yet because Twitch updated their OAuth protocol and I haven't been through the docs.

You may need to thread them, and depending on what functions you implement it may not be thread-safe (just test before going into production). That'd probably look something like:

import threading
t1 = threading.Thread(target=TwitchBot, args=(username, client_id, token, channel1))
t2 = threading.Thread(target=TwitchBot, args=(username, client_id, token, channel2))

t1.setDaemon(True)
t2.setDaemon(True)

t1.start()
t2.start()

Once again, I haven't tested any of this but will likely have time in a few weeks to patch this in my own code after the holiday.

Upvotes: 2

Related Questions