· 6 years ago · Nov 01, 2018, 11:16 PM
1__author__ = "tremor"
2
3import requests
4import sys
5import irc.bot
6
7class Twitchbot(irc.bot.SingleServerIRCBot):
8 def __init__(self, username, client_id, token, channel):
9 self.client_id = client_id
10 self.token = token
11 self.channel = '#' + channel
12
13 url = 'https://api.twitch.tv/kraken/users?login=' + channel
14 headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
15 r = requests.get(url, headers=headers).json()
16 self.channel_id = r['users'][0]['_id']
17
18 server = 'irc.chat.twitch.tv'
19 port = 6667
20 print(f"Connecting to {server} on port {str(port)} ...")
21 super().__init__(self, [{server, port, 'oauth:'+token}], username, username)
22
23 def on_pubmsg(self, c, e):
24
25 if e.arguments[0][:1] == "!":
26 cmd = e.arguments[0].split(' ')[0][1:]
27 print(f"Received command: {cmd}")
28 self.do_command(e, cmd)
29
30 def do_command(self, e, cmd):
31 c = self.connection
32
33 if cmd == "title":
34 url = 'https://api.twitch.tv/kraken/channels/' + self.channel_id
35 headers = {'Client-ID': self.client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
36 r = requests.get(url, headers=headers).json()
37 c.privmsg(self.channel, r['display_name'] + ' channel title is currently ' + r['status'])
38
39
40def main():
41 return
42
43
44if __name__ == '__main__':
45 main()