-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbot.py
89 lines (68 loc) · 2.81 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from typing import List, Dict
import json
import socket
import asyncio
import traceback
from chat_thief.config.log import logger
from chat_thief.config.twitch import TwitchConfig
from chat_thief.command_router import CommandRouter
BEGINBOTBOT = ":beginbotbot!beginbotbot@beginbotbot.tmi.twitch.tv"
CONNECTION_DATA = ("irc.chat.twitch.tv", 6667)
ENCODING = "utf-8"
CHAT_MSG = "PRIVMSG"
ARE_YOU_ALIVE = "PING"
I_AM_ALIVE = "PONG"
config = TwitchConfig()
async def pong(server: socket.socket) -> None:
server.sendall(bytes(I_AM_ALIVE + "\r\n", ENCODING))
def simple_send_msg(server: socket.socket, msg: str) -> None:
if msg:
server.sendall(bytes(f"{CHAT_MSG} #{config.channel} :{msg}\n", ENCODING))
async def send_msg(server: socket.socket, msg: str) -> None:
if msg:
server.sendall(bytes(f"{CHAT_MSG} #{config.channel} :{msg}\n", ENCODING))
def irc_handshake(server: socket.socket) -> None:
logger.debug(
json.dumps({"message": f"Connecting to #{config.channel} as {config.bot}"})
)
server.sendall(bytes("PASS " + config.token + "\r\n", ENCODING))
server.sendall(bytes("NICK " + config.bot + "\r\n", ENCODING))
server.sendall(bytes("JOIN " + f"#{config.channel}" + "\r\n", ENCODING))
async def chat_response(server: socket.socket):
return server.recv(2048).decode(ENCODING)
async def run_bot(server: socket.socket) -> None:
chat_buffer = ""
while True:
raw_irc_response = await chat_response(server)
chat_buffer = chat_buffer + raw_irc_response
messages = chat_buffer.split("\r\n")
chat_buffer = messages.pop()
for message in messages:
if message == "PING :tmi.twitch.tv":
await pong(server)
elif len(message.split()) < 2:
continue
elif CHAT_MSG in message:
try:
if response := CommandRouter(message, logger).build_response():
MESSAGE_LIMIT = 500
if isinstance(response, List):
for r in response:
await send_msg(server, f"{r}")
elif len(response) > MESSAGE_LIMIT:
# This is dumb!
await send_msg(server, f"{response[:500]}")
await send_msg(server, f"{response[500:]}")
else:
await send_msg(server, f"{response}")
except:
print("\033[91m")
traceback.print_exc()
print("\033[0m")
async def main():
with socket.socket() as server:
server.connect(CONNECTION_DATA)
irc_handshake(server)
await asyncio.gather(run_bot(server))
if __name__ == "__main__":
asyncio.run(main())