Finished game roles

This commit is contained in:
Norbi Peti 2017-04-15 13:22:05 +02:00
parent e7963e4f9a
commit 355ba01929
2 changed files with 55 additions and 30 deletions

View file

@ -45,7 +45,6 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
lastannouncementtime = getConfig().getLong("lastannouncementtime");
lastseentime = getConfig().getLong("lastseentime");
GameRoles = (List<String>) getConfig().getList("gameroles", new ArrayList<String>());
saveConfig();
ClientBuilder cb = new ClientBuilder();
cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8));
dc = cb.login();

View file

@ -24,23 +24,35 @@ public class RoleCommand extends DiscordCommandBase {
}
String[] argsa = args.split(" ");
if (argsa[0].equalsIgnoreCase("add")) {
if (argsa.length < 2) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"This command adds a game role to your account.\nUsage: add <rolename>");
final IRole role = checkAndGetRole(message, argsa, "This command adds a game role to your account.");
if (role == null)
return;
try {
DiscordPlugin.perform(() -> message.getAuthor().addRole(role));
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Added game role.");
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while adding role!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while adding the role.");
}
if (!DiscordPlugin.GameRoles.contains(argsa[1].toLowerCase())) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"That game role cannot be found.\nList of game roles:\n"
+ DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n")));
} else if (argsa[0].equalsIgnoreCase("remove")) {
final IRole role = checkAndGetRole(message, argsa, "This command removes a game role from your account.");
if (role == null)
return;
try {
DiscordPlugin.perform(() -> message.getAuthor().removeRole(role));
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Removed game role.");
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while removing role!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while removing the role.");
}
} else if (argsa[0].equalsIgnoreCase("list")) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"List of game roles:\n" + DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n")));
} else if (argsa[0].equalsIgnoreCase("addrole")) {
final List<IRole> roles = (TBMCCoreAPI.IsTestServer() ? DiscordPlugin.devServer : DiscordPlugin.mainServer)
.getRolesByName(argsa[1]);
if (roles.size() == 0) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"The specified role cannot be found on Discord! Removing from the list.");
DiscordPlugin.GameRoles.remove(argsa[1].toLowerCase());
DiscordPlugin.sendMessageToChannel(message.getChannel(), "That role cannot be found on Discord.");
return;
}
if (roles.size() > 1) {
@ -48,30 +60,44 @@ public class RoleCommand extends DiscordCommandBase {
"There are more roles with this name. Why are there more roles with this name?");
return;
}
try {
DiscordPlugin.perform(() -> message.getAuthor().addRole(roles.get(0)));
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while adding role!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while adding the role.");
DiscordPlugin.GameRoles.add(roles.get(0).getName());
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Game role added.");
}
}
} else if (argsa[0].equalsIgnoreCase("remove"))
{
private IRole checkAndGetRole(IMessage message, String[] argsa, String usage) {
if (argsa.length < 2) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"This command removes a game role from your account.\nUsage: remove <rolename>");
return;
DiscordPlugin.sendMessageToChannel(message.getChannel(), usage + "\nUsage: " + argsa[0] + " <rolename>");
return null;
}
} else if (argsa[0].equalsIgnoreCase("list")) {
if (!DiscordPlugin.GameRoles.contains(argsa[1])) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"List of game roles:\n" + DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n")));
"That game role cannot be found.\nList of game roles:\n"
+ DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n")));
return null;
}
final List<IRole> roles = (TBMCCoreAPI.IsTestServer() ? DiscordPlugin.devServer : DiscordPlugin.mainServer)
.getRolesByName(argsa[1]);
if (roles.size() == 0) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"The specified role cannot be found on Discord! Removing from the list.");
DiscordPlugin.GameRoles.remove(argsa[1].toLowerCase());
return null;
}
if (roles.size() > 1) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"There are more roles with this name. Why are there more roles with this name?");
return null;
}
return roles.get(0);
}
@Override
public String[] getHelpText() {
// TODO Auto-generated method stub
return null;
return new String[] { //
"Add or remove game roles from yourself.", //
"Usage: role add|remove <name> or role list", //
"Mods can use role addrole <name> to add a role as a game role" };
}
}