· 6 years ago · Mar 16, 2019, 05:58 PM
1'''
2Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
5
6 http://aws.amazon.com/apache2.0/
7
8or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
9'''
10
11import sys
12import irc.bot
13import requests
14
15queue = []
16
17class TwitchBot(irc.bot.SingleServerIRCBot):
18 def __init__(self, username, client_id, token, channel):
19 self.client_id = client_id
20 self.token = token
21 self.channel = '#' + channel
22
23 # Get the channel id, we will need this for v5 API calls
24 url = 'https://api.twitch.tv/kraken/users?login=' + channel
25 headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
26 r = requests.get(url, headers=headers).json()
27 print(r)
28 self.channel_id = r['users'][0]['_id']
29
30 # Create IRC bot connection
31 server = 'irc.chat.twitch.tv'
32 port = 6667
33 print('Connecting to ' + server + ' on port ' + str(port) + '...')
34 irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+token)], username, username)
35
36
37 def on_welcome(self, c, e):
38 print('Joining ' + self.channel)
39
40 # You must request specific capabilities before you can use them
41 c.cap('REQ', ':twitch.tv/membership')
42 c.cap('REQ', ':twitch.tv/tags')
43 c.cap('REQ', ':twitch.tv/commands')
44 c.join(self.channel)
45
46 def on_pubmsg(self, c, e):
47
48 # If a chat message starts with an exclamation point, try to run it as a command
49 if e.arguments[0][:1] == '!':
50 cmd = e.arguments[0].split(' ')[0][1:]
51 self.do_command(e, cmd)
52 for tag in e.tags:
53 if tag['key'] == 'bits':
54 print(e.source + ' donated ' + tag['value'] + ' bits')
55 if int(tag['value']) > 100:
56 queue.append("choice 1")
57
58 return
59
60 def do_command(self, e, cmd):
61 c = self.connection
62
63 # Poll the API to get current game.
64 if cmd == "nextchallenge":
65 url = 'https://api.twitch.tv/kraken/channels/' + self.channel_id
66 headers = {'Client-ID': self.client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
67 r = requests.get(url, headers=headers).json()
68 if "vellhart" in r['display_name'].lower():
69 c.privmsg(self.channel, 'You should turn on ' + queue[0])
70 queue.pop(0)
71
72def main():
73 username = "" # CHANGE
74 client_id = "" # CHANGE
75 token = "" # CHANGE
76 channel = "vellhart"
77
78 bot = TwitchBot(username, client_id, token, channel)
79 bot.start()
80
81if __name__ == "__main__":
82 main()