-
Notifications
You must be signed in to change notification settings - Fork 1
/
doge_bot.py
69 lines (52 loc) · 1.96 KB
/
doge_bot.py
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
64
65
66
67
68
69
import json
from irc.bot import SingleServerIRCBot
from requests import get
from lib import db, cmds, react, automod
NAME = "dogebot"
OWNER = "plasmodium_"
filename = "tokens.json"
tokens = []
try:
with open(filename) as f:
tokens = json.load(f)
except FileNotFoundError:
pass
class Bot(SingleServerIRCBot):
def __init__(self):
self.HOST = "irc.chat.twitch.tv"
self.PORT = 6667
self.USERNAME = NAME.lower()
self.CLIENT_ID = tokens[0]
self.TOKEN = tokens[1]
self.CHANNEL = f"#{OWNER}"
url = f"https://api.twitch.tv/kraken/users?login={self.USERNAME}"
headers = {"Client-ID": self.CLIENT_ID, "Accept": "application/vnd.twitchtv.v5+json"}
resp = get(url, headers=headers).json()
self.channel_id = resp["users"][0]["_id"]
super().__init__([(self.HOST, self.PORT, f"oauth:{self.TOKEN}")], self.USERNAME, self.USERNAME)
def on_welcome(self, cxn, event):
"""sends message to chat to confirm bot activation"""
for req in ("membership", "tags", "commands"):
cxn.cap("REQ", f":twitch.tv/{req}")
cxn.join(self.CHANNEL)
db.build()
self.send_message("Now Online.")
@db.with_commit
def on_pubmsg(self, cxn, event):
"""
keeps the bot from reacting to itself
parses chat input and reads for commands
"""
tags = {kvpair["key"]: kvpair["value"] for kvpair in event.tags}
user = {"name": tags["display-name"], "id": tags["user-id"], "type": tags["mod"]}
message = event.arguments[0]
react.add_user(bot, user)
if user["name"] != NAME and automod.clear(bot, user, message):
react.process(bot, user, message)
cmds.process(bot, user, message)
def send_message(self, message):
"""allows bot to post messages to chat"""
self.connection.privmsg(self.CHANNEL, message)
if __name__ == "__main__":
bot = Bot()
bot.start()