· 6 years ago · Mar 15, 2019, 07:34 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
15class TwitchBot(irc.bot.SingleServerIRCBot):
16 def __init__(self, username, client_id, token, channel):
17 self.client_id = client_id
18 self.token = token
19 self.channel = '#' + channel
20
21 # Get the channel id, we will need this for v5 API calls
22 url = 'https://api.twitch.tv/kraken/users?login=' + channel
23 headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
24 r = requests.get(url, headers=headers).json()
25 print(r)
26 self.channel_id = r['users'][0]['_id']
27
28 # Create IRC bot connection
29 server = 'irc.chat.twitch.tv'
30 port = 6667
31 print('Connecting to ' + server + ' on port ' + str(port) + '...')
32 irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+token)], username, username)
33
34
35 def on_welcome(self, c, e):
36 print('Joining ' + self.channel)
37
38 # You must request specific capabilities before you can use them
39 c.cap('REQ', ':twitch.tv/membership')
40 c.cap('REQ', ':twitch.tv/tags')
41 c.cap('REQ', ':twitch.tv/commands')
42 c.join(self.channel)
43
44 def on_pubmsg(self, c, e):
45
46 # If a chat message starts with an exclamation point, try to run it as a command
47 if e.arguments[0][:1] == '!':
48 cmd = e.arguments[0].split(' ')[0][1:]
49 print('Received command: ' + cmd)
50 #self.do_command(e, cmd)
51 #if any(['bit' in t.keys() for t in e.tags]):
52 print(e)
53 return
54
55 def do_command(self, e, cmd):
56 c = self.connection
57
58 # Poll the API to get current game.
59 if cmd == "fite":
60 url = 'https://api.twitch.tv/kraken/channels/' + self.channel_id
61 headers = {'Client-ID': self.client_id, 'Accept': 'application/vnd.twitchtv.v5+json'}
62 r = requests.get(url, headers=headers).json()
63 c.privmsg(self.channel, r['display_name'] + ' is currently fighting ' + r['game'])
64
65
66 # The command was not recognized
67 #else:
68 # c.privmsg(self.channel, "Drink water! NOW!")
69
70def main():
71 username = COMPLETE
72 client_id = COMPLETE
73 token = COMPLETE
74 channel = "vellhart"
75
76 bot = TwitchBot(username, client_id, token, channel)
77 bot.start()
78
79if __name__ == "__main__":
80 main()