From 853f9569eac222b8e04d0c5f3c54eb19c4b783c3 Mon Sep 17 00:00:00 2001 From: RikiRC <47928112+RikiRC@users.noreply.github.com> Date: Sun, 10 Jan 2021 11:38:38 +0100 Subject: [PATCH 1/5] Update to Python 3.x and new API requests --- chatbot.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/chatbot.py b/chatbot.py index 1742e90..40c7b99 100755 --- a/chatbot.py +++ b/chatbot.py @@ -6,11 +6,10 @@ http://aws.amazon.com/apache2.0/ or 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. +Modifications 2021 by RikiRC ''' -import sys -import irc.bot -import requests +import sys, irc.bot, requests class TwitchBot(irc.bot.SingleServerIRCBot): def __init__(self, username, client_id, token, channel): @@ -20,32 +19,34 @@ def __init__(self, username, client_id, token, channel): # Get the channel id, we will need this for v5 API calls url = 'https://api.twitch.tv/kraken/users?login=' + channel - headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json'} + headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json', 'Authorization': 'oauth:'+token} r = requests.get(url, headers=headers).json() self.channel_id = r['users'][0]['_id'] # Create IRC bot connection server = 'irc.chat.twitch.tv' port = 6667 - print 'Connecting to ' + server + ' on port ' + str(port) + '...' + print('Connecting to ' + server + ' on port ' + str(port) + '...') irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+token)], username, username) def on_welcome(self, c, e): - print 'Joining ' + self.channel + print('Joining ' + self.channel) # You must request specific capabilities before you can use them c.cap('REQ', ':twitch.tv/membership') c.cap('REQ', ':twitch.tv/tags') c.cap('REQ', ':twitch.tv/commands') c.join(self.channel) + print('Joined ' + self.channel) + c.privmsg(self.channel, "Connected!") def on_pubmsg(self, c, e): # If a chat message starts with an exclamation point, try to run it as a command if e.arguments[0][:1] == '!': cmd = e.arguments[0].split(' ')[0][1:] - print 'Received command: ' + cmd + print('Received command: ' + cmd) self.do_command(e, cmd) return @@ -57,14 +58,14 @@ def do_command(self, e, cmd): url = 'https://api.twitch.tv/kraken/channels/' + self.channel_id headers = {'Client-ID': self.client_id, 'Accept': 'application/vnd.twitchtv.v5+json'} r = requests.get(url, headers=headers).json() - c.privmsg(self.channel, r['display_name'] + ' is currently playing ' + r['game']) + c.privmsg(self.channel, str(r['display_name']) + ' is currently playing ' + str(r['game'])) # Poll the API the get the current status of the stream elif cmd == "title": url = 'https://api.twitch.tv/kraken/channels/' + self.channel_id headers = {'Client-ID': self.client_id, 'Accept': 'application/vnd.twitchtv.v5+json'} r = requests.get(url, headers=headers).json() - c.privmsg(self.channel, r['display_name'] + ' channel title is currently ' + r['status']) + c.privmsg(self.channel, str(r['display_name']) + ' channel title is currently ' + str(r['status'])) # Provide basic information to viewers for specific commands elif cmd == "raffle": @@ -80,7 +81,7 @@ def do_command(self, e, cmd): def main(): if len(sys.argv) != 5: - print("Usage: twitchbot ") + print('Usage: twitchbot ') sys.exit(1) username = sys.argv[1] From 829c04f2fa09689caf27ae4a09df0781348bc1ca Mon Sep 17 00:00:00 2001 From: RikiRC <47928112+RikiRC@users.noreply.github.com> Date: Sun, 10 Jan 2021 11:39:02 +0100 Subject: [PATCH 2/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a5960a2..43281c7 100755 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ $ pip install irc To run the chatbot, you will need to provide an OAuth access token with the chat_login scope. You can reference an authentication sample to accomplish this, or simply use the [Twitch Chat OAuth Password Generator](http://twitchapps.com/tmi/). ```sh -$ python chatbot.py +$ python3 chatbot.py ``` * Username - The username of the chatbot * Client ID - Your registered application's Client ID to allow API calls by the bot From 7e7805de3453a8bc636a2d3fabfd19403221d5da Mon Sep 17 00:00:00 2001 From: RikiRC <47928112+RikiRC@users.noreply.github.com> Date: Tue, 16 Feb 2021 16:37:59 +0100 Subject: [PATCH 3/5] Update chatbot.py --- chatbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatbot.py b/chatbot.py index 40c7b99..88130e4 100755 --- a/chatbot.py +++ b/chatbot.py @@ -15,7 +15,7 @@ class TwitchBot(irc.bot.SingleServerIRCBot): def __init__(self, username, client_id, token, channel): self.client_id = client_id self.token = token - self.channel = '#' + channel + self.channel = '#' + channel.lower() # Get the channel id, we will need this for v5 API calls url = 'https://api.twitch.tv/kraken/users?login=' + channel From d4f78770905714ccf93750c25f3e0a815d9d2301 Mon Sep 17 00:00:00 2001 From: RikiRC <47928112+RikiRC@users.noreply.github.com> Date: Tue, 16 Feb 2021 18:37:29 +0100 Subject: [PATCH 4/5] Update chatbot.py --- chatbot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/chatbot.py b/chatbot.py index 88130e4..5a3e98e 100755 --- a/chatbot.py +++ b/chatbot.py @@ -14,12 +14,12 @@ class TwitchBot(irc.bot.SingleServerIRCBot): def __init__(self, username, client_id, token, channel): self.client_id = client_id - self.token = token + self.token = token.removeprefix("oauth:") self.channel = '#' + channel.lower() # Get the channel id, we will need this for v5 API calls url = 'https://api.twitch.tv/kraken/users?login=' + channel - headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json', 'Authorization': 'oauth:'+token} + headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json', 'Authorization': 'oauth:'+self.token} r = requests.get(url, headers=headers).json() self.channel_id = r['users'][0]['_id'] @@ -27,7 +27,7 @@ def __init__(self, username, client_id, token, channel): server = 'irc.chat.twitch.tv' port = 6667 print('Connecting to ' + server + ' on port ' + str(port) + '...') - irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+token)], username, username) + irc.bot.SingleServerIRCBot.__init__(self, [(server, port, 'oauth:'+self.token)], username, username) def on_welcome(self, c, e): From a79adc9626a439155cfa1d5556c8cbd40e90f277 Mon Sep 17 00:00:00 2001 From: RikiRC <47928112+RikiRC@users.noreply.github.com> Date: Tue, 16 Feb 2021 22:19:43 +0100 Subject: [PATCH 5/5] Update chatbot.py --- chatbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatbot.py b/chatbot.py index 5a3e98e..fc771f0 100755 --- a/chatbot.py +++ b/chatbot.py @@ -19,7 +19,7 @@ def __init__(self, username, client_id, token, channel): # Get the channel id, we will need this for v5 API calls url = 'https://api.twitch.tv/kraken/users?login=' + channel - headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json', 'Authorization': 'oauth:'+self.token} + headers = {'Client-ID': client_id, 'Accept': 'application/vnd.twitchtv.v5+json'} r = requests.get(url, headers=headers).json() self.channel_id = r['users'][0]['_id']