· 6 years ago · Apr 08, 2019, 10:22 AM
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
14from time import time
15
16total_bits = 0
17st_time = time()
18cooldown = time()
19
20def bits2challenge(bits):
21 challenges = {800 : "Invisible dashing", 1000 : "Ice Physics", 1200 : "100% increase game speed"}
22 vals = [800, 1000, 1200]
23
24 if bits < vals[0]:
25 current = "NOTHING YOU CHEAPSKATE"
26 next_c = challenges[vals[0]]
27 left = vals[0] - bits
28 elif bits < vals[1]:
29 current = challenges[vals[0]]
30 next_c = challenges[vals[1]]
31 left = vals[1] - bits
32 elif bits < vals[2]:
33 current = challenges[vals[1]]
34 next_c = challenges[vals[2]]
35 left = vals[2] - bits
36 else:
37 current = challenges[vals[2]]
38 next_c = "Complete"
39 left = -1
40 return current, next_c, left
41
42
43class TwitchBot(irc.bot.SingleServerIRCBot):
44 def __init__(self, username, client_id, token, channel):
45 self.client_id = client_id
46 self.token = token
47 self.channel = '#' + channel
48
49 # Get the channel id, we will need this for v5 API calls
50 url = 'https://api.twitch.tv/kraken/users?login=' + channel
51 headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
52 r = requests.get(url, headers=headers).json()
53 print(r)
54 self.channel_id = r['users'][0]['_id']
55
56 # Create IRC bot connection
57 server = 'irc.chat.twitch.tv'
58 port = 6667
59 print('Connecting to ' + server + ' on port ' + str(port) + '...')
60 irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+token)], username, username)
61
62
63 def on_welcome(self, c, e):
64 print('Joining ' + self.channel)
65
66 # You must request specific capabilities before you can use them
67 c.cap('REQ', ':twitch.tv/membership')
68 c.cap('REQ', ':twitch.tv/tags')
69 c.cap('REQ', ':twitch.tv/commands')
70 c.join(self.channel)
71
72 def on_pubmsg(self, c, e):
73 global st_time, total_bits
74
75 # If a chat message starts with an exclamation point, try to run it as a command
76 if e.arguments[0][:1] == '!':
77 cmd = e.arguments[0].split(' ')[0][1:]
78 self.do_command(e, cmd)
79 for tag in e.tags:
80 if tag['key'] == 'bits':
81 total_bits += int(tag['value'])
82 current, nextc, left = bits2challenge(total_bits)
83 if left > 0:
84 c.privmsg(self.channel, e.source + ' donated ' + str(tag['value']) + ' bits! We have reached ' + current + ' challenge! We only need ' + str(left) + ' to reach the next challenge!')
85 else:
86 c.privmsg(self.channel, e.source + ' donated ' + str(tag['value']) + ' bits! We have reached the ultimate goal!')
87 if (time() - st_time)/60 > 10:
88 current, nextc, left = bits2challenge(total_bits)
89 c.privmsg(self.channel,'Times up!! @vellhart should enable ' + current + '! NOW!!!')
90 st_time = time()
91 total_bits = 0
92 return
93
94 def do_command(self, e, cmd):
95 global st_time, total_bits, cooldown
96
97 c = self.connection
98 allowed = ["vellhart", "verbalkin7", "tardisgater", "randomdoorknob", "periweather", "alecm88", "johnald"]
99 url = 'https://api.twitch.tv/kraken/channels/' + self.channel_id
100 headers = {'Client-ID': self.client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
101 r = requests.get(url, headers=headers).json()
102 nick = r['display_name'].lower().split('@')[0]
103
104 # Poll the API to get current game.
105 if cmd == "nextchallenge":
106 if nick in allowed:
107 current, nextc, left = bits2challenge(total_bits)
108 c.privmsg(self.channel, 'You should turn on ' + current)
109 elif cmd == "start":
110 if nick in allowed:
111 st_time = time()
112 print('starting time')
113 elif cmd == "CC":
114 current, nextc, left = bits2challenge(total_bits)
115 left_time = (time() - st_time)/60
116 if (time() - cooldown)/60 > 2:
117 c.privmsg(self.channel, "we're currently on " + current + " we've raised " + str(total_bits) + " bits towards the next incentive. Get your bits in, there's " + str(left_time) + " till the next block")
118 cooldown = time()
119
120def main():
121 username = "chatfite"
122 client_id = "hi4y8i4z2o0b8163dkl55hxy574quk"
123 token = "pxo7qyc19s1c7fqzqmic8d0g1ooagf"
124 channel = "vellhart"
125
126 bot = TwitchBot(username, client_id, token, channel)
127 bot.start()
128
129if __name__ == "__main__":
130 main()