-
Notifications
You must be signed in to change notification settings - Fork 35
/
permission.py
100 lines (80 loc) · 3.6 KB
/
permission.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
# 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/>.
"""Database agnostic way of getting and setting a player's permissions.
It assumes the database driver interprets integers as SteamID64s and
being able to handle minqlx.Player instances.
"""
import minqlx
class permission(minqlx.Plugin):
def __init__(self):
self.add_command("setperm", self.cmd_setperm, 5, usage="<id> <level>")
self.add_command("getperm", self.cmd_getperm, 5, usage="<id>")
# myperm can only be used in-game.
self.add_command("myperm", self.cmd_myperm,
channels=("chat", "red_team_chat", "blue_team_chat", "spectator_chat", "free_chat", "client_command"))
def cmd_setperm(self, player, msg, channel):
if len(msg) < 3:
return minqlx.RET_USAGE
try:
ident = int(msg[1])
target_player = None
if 0 <= ident < 64:
target_player = self.player(ident)
ident = target_player.steam_id
except ValueError:
channel.reply("Invalid ID. Use either a client ID or a SteamID64.")
return
except minqlx.NonexistentPlayerError:
channel.reply("Invalid client ID. Use either a client ID or a SteamID64.")
return
try:
level = int(msg[2])
if level < 0 or level > 5:
raise ValueError
except ValueError:
channel.reply("Invalid permission level. Use a level between 0 and 5.")
return
self.db.set_permission(ident, level)
name = target_player.name if target_player else str(ident)
channel.reply("^6{}^7 was given permission level ^6{}^7.".format(name, level))
def cmd_getperm(self, player, msg, channel):
if len(msg) < 2:
return minqlx.RET_USAGE
try:
ident = int(msg[1])
target_player = None
if 0 <= ident < 64:
target_player = self.player(ident)
ident = target_player.steam_id
if ident == minqlx.owner():
channel.reply("That's my master.")
return
except ValueError:
channel.reply("Invalid ID. Use either a client ID or a SteamID64.".format(msg[1]))
return
perm = self.db.get_permission(ident)
if perm is None:
channel.reply("I do not know ^6{}^7.".format(msg[1]))
else:
name = target_player.name if target_player else str(ident)
channel.reply("^6{}^7 has permission level ^6{}^7.".format(name, perm))
def cmd_myperm(self, player, msg, channel):
if player.steam_id == minqlx.owner():
channel.reply("You can do anything to me, master.")
return
perm = self.db.get_permission(player)
if perm is None:
channel.reply("I do not know you.")
else:
channel.reply("You have permission level ^6{}^7.".format(perm))