-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundbox.py
121 lines (103 loc) · 3.69 KB
/
soundbox.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
from errbot import BotPlugin, botcmd
import subprocess
from pathlib import Path
class Sound():
def __init__(self, filename, aliases):
self.filename = filename
self.aliases = aliases
self.counter = 0
def play(self, sounds_path):
proc = subprocess.Popen(['mpv', '--really-quiet', sounds_path+"/"+self.filename])
self.counter += 1
return proc
def addAliases(self, aliases):
self.aliases = list(set(self.aliases + aliases))
def getAliases(self):
return self.aliases
def getFilename(self):
return self.filename
def __str__(self):
return "{}\t{}".format(self.filename, ", ".join(self.aliases))
def __eq__(self, other):
if not isinstance(other, Sound):
return False
return self.filename == other.getFilename
class Soundbox(BotPlugin):
"""Plugin to play funny sounds"""
def activate(self):
super().activate()
self.mute = False
self.proc = None
if "SOUNDS" not in self:
self["SOUNDS"]=[]
def get_configuration_template(self):
return {'SOUNDS_PATH': '/srv/sounds', 'CHECK_IF_EXISTS': True}
@botcmd
def soundbox_list(self, msg, args):
"""List sounds"""
for sound in self['SOUNDS']:
yield str(sound)
if len(self['SOUNDS']) == 0:
return "No sounds yet"
@botcmd(split_args_with=None)
def soundbox_add(self, msg, args):
"""Add a sound"""
if self.config["CHECK_IF_EXISTS"]:
with Path("{}/{}".format(self.config["SOUNDS_PATH"],args[0])):
if not file.is_file():
return "No file {} was found in {}".format(args[0],self.config["SOUNDS_PATH"])
sound = Sound(args[0],args[1:])
with self.mutable('SOUNDS') as sounds_list:
sounds_list.append(sound)
return "Sound was successfully added."
def findSound(self, alias):
for sound in self['SOUNDS']:
if alias in sound.aliases:
return sound
return None
@botcmd(split_args_with=None)
def soundbox_addaliases(self, msg, args):
"""Add a list of aliases to a sound"""
sound = self.findSound(args[0])
if sound == None:
return "Sound {} not found.".format(args[0])
sound.addAliases(args[:1])
return "Aliases were successfully added."
@botcmd
def soundbox_del(self, msg, args):
"""Remove a sound"""
sound = self.findSound(args)
if sound == None:
return "Sound {} not found.".format(args)
with self.mutable('SOUNDS') as sounds_list:
for sound in sounds_list:
if args in sound.aliases:
sounds_list.remove(sound)
break
return "Sound was successfully removed."
@botcmd
def sb(self, msg, args):
"""Play a sound"""
if self.mute:
return "Mmmh mmh mmmmh! (My lips are sealed!)"
elif self.proc and self.proc.poll() is None:
return "I'm already playing something!"
else:
sound = self.findSound(args)
if sound == None:
return "Sound {} not found.".format(args)
self.proc = sound.play(self.config["SOUNDS_PATH"])
@botcmd
def kill(self, msg, args):
"""Kill current sound"""
self.proc.terminate()
return "OK, sorry if it was too loud..."
@botcmd
def mute(self, msg, args):
"""Disables temporarly making noise"""
if self.mute:
self.mute=False
return "Yay, I can make noise again!"
else:
self.mute=True
return "OK, I'll shut up now!"