-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
194 lines (149 loc) · 5.66 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import configparser
import ssl
import socket
import threading
from MusicPlayer import MusicPlayer, YoutubeQueueItem, SpotifyQueueItem
class IrcClient(object):
def __init__(self, options):
self.server = options.get('server')
self.port = options.get('port')
self.ident = options.get('ident')
self.nick = options.get('nick')
self.realname = options.get('realname')
self.password = options.get('password')
self.ssl = options.get('ssl', False)
self.connection_thread = None
self.disconnect = threading.Event()
self.event_listeners = {}
def connect(self):
self.connection_thread = threading.Thread(target=self.__connect)
self.connection_thread.start()
def disconnect(self):
self.disconnect.set()
def __connect(self):
read_buffer = ""
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.ssl:
self.s = ssl.wrap_socket(self.s)
self.s.connect((self.server, self.port))
self.__send_string("PASS {}\r\n".format(self.password))
self.__send_string("NICK {}\r\n".format(self.nick))
self.__send_string("USER {} {} bla :{}\r\n".format(self.ident, self.server, self.realname))
while 1:
read_buffer = read_buffer+self.s.recv(1024).decode()
temp = read_buffer.split("\n")
read_buffer = temp.pop()
for line in temp:
print(line)
line = line.rstrip()
line = line.split()
print(line)
if line[0] == "PING":
self.__send_string("PONG %s\r\n" % line[1])
# we assume it is safe to send data after receiving the welcome message
if len(line) > 1 and line[1] == '001':
self.on_connect()
if len(line) > 1 and line[1] == 'PRIVMSG':
identString = line[0][1:]
nick = identString.split("!")[0]
realname = identString.split("!")[1].split("@")[0]
ip = identString.split("@")[1]
ident = Ident(nick, realname, ip)
channel = line[2]
message = " ".join(line[3:])[1:]
self.on_message(ident, channel, message)
def __send_string(self, string):
self.s.send(string.encode())
def __emit(self, eventname, data):
event_listeners = self.event_listeners
if eventname in event_listeners:
for listener in event_listeners[eventname]:
listener(self, data)
def send_message(self, dest, message):
lines = message.split("\n")
for line in lines:
line = line.strip()
if len(line) > 0:
self.__send_string("PRIVMSG {} {}\r\n".format(dest, line))
def on_message(self, ident, channel, message):
self.__emit('PRIVMSG', (ident, channel, message))
def on_connect(self):
self.__emit('connected', None)
def join_channel(self, channel):
self.__send_string("JOIN {}\r\n".format(channel))
def on(self, eventname, callback):
if eventname not in self.event_listeners:
self.event_listeners[eventname] = []
self.event_listeners[eventname].append(callback)
class Ident(object):
def __init__(self, nick, ident, ip):
self.nick = nick
self.ident = ident
self.ip = ip
def get_nick(self):
return self.nick
def get_ident(self):
return self.ident
def get_ip(self):
return self.ip
config = configparser.ConfigParser()
config.read('bot.cfg')
spotify_user = config.get('main', 'spotify_user')
spotify_pass = config.get('main', 'spotify_pass')
options = {
'server': config.get('main', 'server'),
'port': config.getint('main', 'port'),
'channel': config.get('main', 'channel'),
'ident': config.get('main', 'ident'),
'nick': config.get('main', 'nick'),
'realname': config.get('main', 'realname'),
'password': config.get('main', 'password'),
'ssl': True
}
wait_for_connect = threading.Event()
def on_connect(bot, data):
wait_for_connect.set()
def on_message(bot, data):
"""
:type bot: IrcClient
:type data: list
"""
def print_help(bot, dest):
bot.send_message(dest, "MusicBot")
bot.send_message(dest, "!music yt <link> plays a youtube link")
bot.send_message(dest, "!music sp <link> plays a spotify link")
(ident, channel, message) = data
words = message.split(" ")
args = words[1:]
first_word = words[0]
if first_word == "!music":
if len(args) == 2 and args[0] == 'yt':
url = args[1]
item = YoutubeQueueItem(url)
player.add_to_queue(item)
elif len(args) == 2 and args[0] == 'sp':
url = args[1]
item = SpotifyQueueItem(url)
player.add_to_queue(item)
elif len(args) == 1 and args[0] == 'next':
player.skip_song()
elif len(args) == 1 and args[0] == 'list':
queue = "Items in queue:\r\n"
queue += player.get_queue_string()
bot.send_message(channel, queue)
else:
print_help(bot, ident.get_nick())
player_options = {}
if len(spotify_user) > 0:
player_options['spotify_username'] = spotify_user
player_options['spotify_password'] = spotify_pass
else:
print("No spotify user found, disabling support")
player = MusicPlayer(player_options)
player.start()
bot = IrcClient(options)
bot.on('connected', on_connect)
bot.on('PRIVMSG', on_message)
bot.connect()
wait_for_connect.wait()
bot.join_channel(config.get('main', 'channel'))