-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmotd.py
151 lines (120 loc) · 5.76 KB
/
motd.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
# minqlx - A Quake Live server administrator bot.
# Copyright (C) 2015 Mino <mino@minomino.org>
# This file is part of minqlx.
# minqlx is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# minqlx is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with minqlx. If not, see <http://www.gnu.org/licenses/>.
import minqlx
import minqlx.database
import random
MOTD_SET_KEY = "minqlx:motd"
MOTD_SOUNDS = [
"sound/vo/crash_new/37b_07_alt.wav",
"sound/player/lucy/taunt.wav"
]
class motd(minqlx.Plugin):
database = minqlx.database.Redis
def __init__(self):
super().__init__()
self.add_hook("player_connect, self.handle_player_connect)
self.add_hook("player_disconnect", self.handle_player_disconnect)
self.add_command(("setmotd", "newmotd"), self.cmd_setmotd, 4, usage="<motd>")
self.add_command(("setmotdall", "newmotdall"), self.cmd_setmotdall, 4, usage="<motd>")
self.add_command(("getmotd", "motd"), self.cmd_getmotd)
self.add_command(("clearmotd", "removemotd", "remmmotd"), self.cmd_clearmotd, 4)
self.add_command(("clearmotdall", "removemotdall", "remmmotdall"), self.cmd_clearmotdall, 4)
self.add_command("addmotd", self.cmd_addmotd, 4, usage="<more_motd>")
self.add_command("addmotdall", self.cmd_addmotdall, 4, usage="<more_motd>")
# homepath doesn't change runtime, so we can just save it for the sake of efficiency.
self.home = self.get_cvar("fs_homepath")
self.motd_key = MOTD_SET_KEY + ":{}".format(self.home)
# Add this server to the MOTD set.
self.db.sadd(MOTD_SET_KEY, self.home)
# Cvar to disable/change the welcome sound.
self.set_cvar_once("qlx_motdHeader", "^6======= ^7Message of the Day ^6=======^7")
self.connected_players = []
@minqlx.delay(10)
def handle_player_connect(self, player):
"""Send the message of the day to the player in a tell.
"""
if player not in self.connected_players: #to prevent bug with player_connect firing twice
self.connected_players.append(player)
try:
motd = self.db[self.motd_key]
except KeyError:
return
if not MOTD_SOUNDS: return
# Try to play sound file
try:
if self.db.get_flag(player, "essentials:sounds_enabled", default=True):
super().play_sound(random.choice(MOTD_SOUNDS), player)
except Exception as e:
self.msg("^1Error: ^7{}".format(e))
self.send_motd(player, motd)
def handle_player_disconnect(self, player, reason):
self.connected_players.remove(player)
def cmd_setmotd(self, player, msg, channel):
if len(msg) < 2:
return minqlx.RET_USAGE
self.db.sadd(MOTD_SET_KEY, self.home)
self.db[self.motd_key] = " ".join(msg[1:])
player.tell("The MOTD has been set.")
return minqlx.RET_STOP_EVENT
def cmd_setmotdall(self, player, msg, channel):
motds = self.db.smembers(MOTD_SET_KEY)
db = self.db.pipeline()
for path in motds:
motd_key = MOTD_SET_KEY + ":{}".format(path)
db.set(motd_key, " ".join(msg[1:]))
db.execute()
player.tell("All MOTDs have been set.")
return minqlx.RET_STOP_EVENT
def cmd_getmotd(self, player, msg, channel):
if self.motd_key in self.db:
self.send_motd(player, self.db[self.motd_key])
else:
player.tell("No MOTD has been set.")
return minqlx.RET_STOP_EVENT
def cmd_clearmotd(self, player, msg, channel):
del self.db[self.motd_key]
player.tell("The MOTD has been cleared.")
return minqlx.RET_STOP_EVENT
def cmd_clearmotdall(self, player, msg, channel):
motds = [MOTD_SET_KEY + ":{}".format(m) for m in self.db.smembers(MOTD_SET_KEY)]
self.db.delete(*motds)
player.tell("All MOTDs have been cleared.")
return minqlx.RET_STOP_EVENT
def cmd_addmotd(self, player, msg, channel):
motd = self.db[self.motd_key]
if not motd:
self.db[self.motd_key] = " ".join(msg[1:])
player.tell("No MOTD was set, so a new one was made.")
else:
leading_space = "" if len(motd) > 2 and motd[-2:] == "\\n" else " "
self.db[self.motd_key] = motd + leading_space + " ".join(msg[1:])
player.tell("The MOTD has been updated.")
return minqlx.RET_STOP_EVENT
def cmd_addmotdall(self, player, msg, channel):
motds = self.db.smembers(MOTD_SET_KEY)
for path in motds:
motd_key = MOTD_SET_KEY + ":{}".format(path)
if motd_key not in self.db:
self.db[motd_key] = " ".join(msg[1:])
else:
motd = self.db[motd_key]
leading_space = "" if len(motd) > 2 and motd[-2:] == "\\n" else " "
self.db[motd_key] = motd + leading_space + " ".join(msg[1:])
player.tell("Added to all MOTDs.")
return minqlx.RET_STOP_EVENT
def send_motd(self, player, motd):
for line in self.get_cvar("qlx_motdHeader").split("\\n"):
player.tell(line)
for line in motd.split("\\n"):
player.tell(line)