-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathirc_bot.py
More file actions
63 lines (51 loc) · 1.68 KB
/
irc_bot.py
File metadata and controls
63 lines (51 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from irc.bot import SingleServerIRCBot
import requests
import logging
logger = logging.getLogger("Queue_Bot")
class Context:
def __init__(self, **kwargs):
vars(self).update(kwargs)
class Bot(SingleServerIRCBot):
def __init__(self, name, client_id, oauth, channel):
self.HOST = "irc.chat.twitch.tv"
self.PORT = 6667
self.USERNAME = name.lower()
self.CLIENT_ID = client_id
self.TOKEN = oauth
self.CHANNEL = f"#{channel}"
self.handler = None
# Login bot
url = f"https://api.twitch.tv/kraken/users?login={self.USERNAME}"
headers = {"Client-ID": self.CLIENT_ID,
"Accept": "application/vnd.twitchtv.v5+json"}
logger.info("Logging in twitch bot...")
resp = requests.get(url, headers=headers).json()
logger.debug(resp)
super().__init__(
[
(self.HOST, self.PORT, self.TOKEN)
], self.USERNAME, self.USERNAME
)
def on_welcome(self, cxn, event):
"""Request capabilities on joining the channel
"""
for req in ("membership", "tags", "commands"):
cxn.cap("REQ", f":twitch.tv/{req}")
cxn.join(self.CHANNEL)
def on_pubmsg(self, cxn, event):
"""Override for public message event handler
"""
tags = {kvpair["key"]: kvpair["value"] for kvpair in event.tags}
tags['message'] = event.arguments[0]
ctx = Context(event=tags, bot=self)
if self.handler:
# To generalize have your handler func be (*args, **kwargs)
self.handler(ctx)
def send_message(self, message):
"""Send a message
"""
self.connection.privmsg(self.CHANNEL, message)
def add_handler(self, handler):
"""A wrapper for incoming message handling
"""
self.handler = handler