From f6c4078d09d227e09a46b74e192d316b3e4f0606 Mon Sep 17 00:00:00 2001 From: "M. Hancock" Date: Mon, 5 Feb 2018 15:56:38 -0800 Subject: [PATCH] add class to connect to twitch.tv (#23) * make Message object part of Bot * add TwitchBot class * fix issue with logging not starting * change server name * no loading plugins until functionality works * allow full functionality * add ops * my sanity is wearing thin * finalize TwitchBot * fix formatting --- pinhook/bot.py | 59 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/pinhook/bot.py b/pinhook/bot.py index 821ff66..62297cb 100644 --- a/pinhook/bot.py +++ b/pinhook/bot.py @@ -11,23 +11,6 @@ import irc.bot irc.client.ServerConnection.buffer_class.errors = 'replace' -class Message: - def __init__(self, channel, nick, botnick, ops, logger, cmd=None, arg=None, text=None, nick_list=None): - self.channel = channel - self.nick = nick - self.nick_list = nick_list - self.botnick = botnick - self.ops = ops - self.logger = logger - if cmd: - self.cmd = cmd - self.arg = arg - if text: - self.text = text - if not (cmd or text): - raise TypeError('missing cmd or text parameter') - - class Bot(irc.bot.SingleServerIRCBot): def __init__(self, channels, nickname, server, **kwargs): self.set_kwargs(**kwargs) @@ -40,6 +23,22 @@ class Bot(irc.bot.SingleServerIRCBot): self.bot_nick = nickname self.start_logging(self.log_level) self.load_plugins() + + class Message: + def __init__(self, channel, nick, botnick, ops, logger, cmd=None, arg=None, text=None, nick_list=None): + self.channel = channel + self.nick = nick + self.nick_list = nick_list + self.botnick = botnick + self.ops = ops + self.logger = logger + if cmd: + self.cmd = cmd + self.arg = arg + if text: + self.text = text + if not (cmd or text): + raise TypeError('missing cmd or text parameter') def set_kwargs(self, **kwargs): kwarguments = { @@ -168,7 +167,7 @@ class Bot(irc.bot.SingleServerIRCBot): c.privmsg(chan, 'Plugins reloaded') elif cmd in self.cmds: try: - output = self.cmds[cmd](Message( + output = self.cmds[cmd](self.Message( channel=chan, cmd=cmd, nick_list=list(self.channels[chan].users()), @@ -185,7 +184,7 @@ class Bot(irc.bot.SingleServerIRCBot): else: for lstnr in self.lstnrs: try: - output = self.lstnrs[lstnr](Message( + output = self.lstnrs[lstnr](self.Message( channel=chan, text=text, nick_list=list(self.channels[chan].users()), @@ -208,3 +207,25 @@ class Bot(irc.bot.SingleServerIRCBot): self.logger.debug('output action: {}'.format(msg)) c.action(chan, msg) time.sleep(.5) + + +class TwitchBot(Bot): + def __init__(self, nickname, channel, token, plugin_dir='plugins', log_level='info', ops=[]): + self.bot_nick = nickname + self.start_logging(log_level) + self.channel = channel + self.plugin_dir = plugin_dir + self.ops = ops + server = 'irc.twitch.tv' + port = 6667 + self.logger.info('Joining Twitch Server') + irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+token)], nickname, nickname) + self.load_plugins() + + def on_welcome(self, c, e): + self.logger.info('requesting permissions') + c.cap('REQ', ':twitch.tv/membership') + c.cap('REQ', ':twitch.tv/tags') + c.cap('REQ', ':twitch.tv/commands') + self.logger.info('Joining channel ' + self.channel) + c.join(self.channel)