/
CmdAdmin.java
182 lines (157 loc) 路 7.41 KB
/
CmdAdmin.java
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
package de.halfminer.hmbot.cmd;
import com.github.theholywaffle.teamspeak3.api.exception.TS3CommandFailedException;
import com.github.theholywaffle.teamspeak3.api.wrapper.Client;
import com.github.theholywaffle.teamspeak3.api.wrapper.ClientInfo;
import com.github.theholywaffle.teamspeak3.api.wrapper.DatabaseClient;
import de.halfminer.hmbot.storage.HalfClient;
import de.halfminer.hmbot.util.MessageBuilder;
import de.halfminer.hmbot.util.StringArgumentSeparator;
import java.util.List;
import java.util.Map;
/**
* - Lookup player information via username or client id
* - Supports client, database and unique id as parameters
* - Checks database for offline client with given database or unique id, if no player was found
* - Reload the config file
* - Won't reload if file was not modified or if it is in invalid format
* - Restart (full reconnect) or stop the bot entirely
*/
class CmdAdmin extends Command {
public CmdAdmin(HalfClient client, ClientInfo clientInfo, StringArgumentSeparator command) throws InvalidCommandException {
super(client, clientInfo, command);
if (!command.meetsLength(1)) {
throw new InvalidCommandException(CommandEnum.ADMIN);
}
}
@Override
void run() throws InvalidCommandException {
String argument = command.getArgument(0);
switch (argument.toLowerCase()) {
case "stop":
case "restart":
boolean restart = argument.startsWith("r");
if (restart) {
sendMessage("cmdAdminRestarted");
} else {
sendMessage("cmdAdminStopped");
}
componentHolder.getStateHolder().stop("Bot was " + (restart ? "restarted" : "stopped")
+ " via command by client " + clientInfo.getNickname(), restart);
return;
case "reload":
if (componentHolder.getStateHolder().reloadConfig()) {
sendMessage("cmdAdminConfigReloaded");
} else {
sendMessage("cmdAdminConfigReloadedError");
}
return;
case "lookup":
if (command.meetsLength(2)) {
String lookupArg = command.getArgument(1);
// determine what input we have
LookupType type;
if (lookupArg.length() == 28 && lookupArg.endsWith("=")) {
type = LookupType.STRING_UID;
} else if (command.getArgumentInt(1) > Integer.MIN_VALUE) {
type = LookupType.NUMERIC_ID;
} else {
type = LookupType.NICKNAME;
}
Map<String, String> mapToSend = null;
boolean isOnline = false;
switch (type) {
case NICKNAME:
try {
List<Client> clients = api.getClientsByName(lookupArg);
if (clients.size() == 1) {
mapToSend = getClientInfoMap(clients.get(0));
isOnline = true;
} else {
StringBuilder send = new StringBuilder();
for (Client client : clients) {
send.append(" ID: ")
.append(client.getId())
.append(" - Nickname: ")
.append(client.getNickname())
.append("\n");
}
sendMessage("cmdAdminLookupList", "LIST", send.toString());
return;
}
} catch (TS3CommandFailedException ignored) {}
break;
case NUMERIC_ID:
int id = command.getArgumentInt(1);
for (Client client : api.getClients()) {
if (client.getId() == id || client.getDatabaseId() == id) {
mapToSend = getClientInfoMap(client);
isOnline = true;
break;
}
}
// offline database id lookup
if (mapToSend == null) {
try {
DatabaseClient dbClient = api.getDatabaseClientInfo(id);
mapToSend = dbClient.getMap();
} catch (TS3CommandFailedException ignored) {}
}
break;
case STRING_UID:
try {
Client client = api.getClientByUId(lookupArg);
mapToSend = getClientInfoMap(client);
isOnline = true;
} catch (TS3CommandFailedException e) {
try {
DatabaseClient dbClient = api.getDatabaseClientByUId(lookupArg);
mapToSend = dbClient.getMap();
} catch (TS3CommandFailedException ignored) {}
}
break;
}
if (mapToSend == null) {
sendMessage("cmdAdminLookupNotFound");
return;
}
StringBuilder toSend = new StringBuilder("======= ")
.append(mapToSend.get("client_nickname"))
.append(" (")
.append(isOnline ? "Online" : "Offline")
.append(", DBID: ")
.append(mapToSend.get("client_database_id"))
.append(") =======\n");
for (Map.Entry<String, String> entry : mapToSend.entrySet()) {
if (entry.getValue().length() == 0) continue;
toSend.append(" ")
.append(entry.getKey())
.append("=")
.append(entry.getValue())
.append("\n");
}
MessageBuilder.create(toSend.toString())
.setDirectString()
.sendMessage(clientId);
return;
}
default:
throw new InvalidCommandException(CommandEnum.ADMIN);
}
}
private Map<String, String> getClientInfoMap(Client client) {
if (client instanceof ClientInfo) {
return client.getMap();
} else {
try {
return api.getClientInfo(client.getId()).getMap();
} catch (TS3CommandFailedException e) {
return null;
}
}
}
private enum LookupType {
NUMERIC_ID,
STRING_UID,
NICKNAME
}
}