【问题标题】:How to join multiple channels using twitchdev python code如何使用 twitchdev python 代码加入多个频道
【发布时间】:2020-06-20 21:41:34
【问题描述】:

我正在尝试使用 twitchdev python 代码制作翻译机器人:https://github.com/twitchdev/chat-samples/blob/master/python/chatbot.py

我能够从频道获取消息并发送翻译文本,但我无法加入多个频道。

我在下面所做的是制作频道列表并使用 for 循环调用,但它只加入最后一个频道。

我尝试在def on_welcome(self, c, e) 中创建另一个频道列表,但它也适用于最后一个频道(当我在def on_welcome(self, c, e) 中打印self.channels 时,它会打印空白列表,而当我打印 self.channel 时,它只打印最后一个频道)

欢迎提出任何建议。

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()

【问题讨论】:

    标签: python-3.x bots twitch


    【解决方案1】:

    在您的main() 函数中,您正在建立一个机器人对象:

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

    不要将多个通道传递给同一个机器人对象,而是使用单个通道创建多个机器人对象。我还没有对此进行测试,因为 Twitch 更新了他们的 OAuth 协议,而且我还没有通过文档。

    您可能需要对它们进行线程化,并且根据您实现的功能,它可能不是线程安全的(只需在投入生产之前进行测试)。这可能看起来像:

    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()
    

    再一次,我还没有测试过这些,但可能会在几周后有时间在假期结束后在我自己的代码中修补它。

    【讨论】:

    • 这对你有用吗?
    猜你喜欢
    • 2013-06-27
    • 2014-03-31
    • 2016-03-01
    • 1970-01-01
    • 2016-07-04
    • 2013-04-08
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多