use of de.nikos410.discordbot.framework.annotations.CommandSubscriber in project de-DiscordBot by DACH-Discord.
the class DiscordBot method makeCommandMap.
private void makeCommandMap() {
this.commands.clear();
for (final String key : this.loadedModules.keySet()) {
Object module = this.loadedModules.get(key);
for (Method method : module.getClass().getMethods()) {
if (method.isAnnotationPresent(CommandSubscriber.class)) {
final CommandSubscriber[] annotations = method.getDeclaredAnnotationsByType(CommandSubscriber.class);
final String command = annotations[0].command();
final String help = annotations[0].help();
final boolean pmAllowed = annotations[0].pmAllowed();
final int permissionLevel = annotations[0].permissionLevel();
final int parameterCount = method.getParameterCount();
final boolean passContext = annotations[0].passContext();
// Mindestens 1 (message), max 6 (message + 5 parameter)
if (parameterCount > 0 && parameterCount <= 6) {
final Command cmd = new Command(module, method, help, pmAllowed, permissionLevel, parameterCount - 1, passContext);
this.commands.put(command.toLowerCase(), cmd);
} else {
System.err.println("Ungültige Anzahl Parameter bei Befehl " + command);
}
}
}
final CommandModule moduleAnnotation = module.getClass().getDeclaredAnnotationsByType(CommandModule.class)[0];
if (!moduleAnnotation.commandOnly()) {
try {
this.client.getDispatcher().registerListener(module);
} catch (NullPointerException e) {
System.err.println("[Error] Could not get EventDispatcher: ");
Util.error(e);
}
}
}
}
use of de.nikos410.discordbot.framework.annotations.CommandSubscriber in project de-DiscordBot by DACH-Discord.
the class GeneralCommands method command_Uptime.
@CommandSubscriber(command = "uptime", help = "Zeigt seit wann der Bot online ist")
public void command_Uptime(final IMessage message) {
final DateTimeFormatter timeStampFormatter = DateTimeFormatter.ofPattern("dd.MM. | HH:mm");
Util.sendMessage(message.getChannel(), "Online seit: " + startupTimestamp.format(timeStampFormatter));
}
use of de.nikos410.discordbot.framework.annotations.CommandSubscriber in project de-DiscordBot by DACH-Discord.
the class BotSetup method command_help.
@CommandSubscriber(command = "help", help = "Zeigt diese Hilfe an")
public void command_help(final IMessage message) {
final EmbedBuilder helpEmbedBuilder = new EmbedBuilder();
final List<ModuleWrapper> loadedModules = bot.getLoadedModules();
for (ModuleWrapper module : loadedModules) {
final StringBuilder moduleHelpBuilder = new StringBuilder();
for (CommandWrapper command : module.getCommands()) {
// Only list commands that are available to that user
if (bot.getUserPermissionLevel(message.getAuthor(), message.getGuild()).getLevel() >= command.getPermissionLevel().getLevel()) {
moduleHelpBuilder.append(String.format("`%s` - %s%n", command.getName(), command.getHelp()));
}
}
final String helpString = moduleHelpBuilder.toString();
if (moduleHelpBuilder.length() > 0) {
helpEmbedBuilder.appendField(module.getDisplayName(), helpString, false);
}
}
final EmbedObject embedObject = helpEmbedBuilder.build();
DiscordIO.sendEmbed(message.getAuthor().getOrCreatePMChannel(), embedObject);
if (!message.getChannel().isPrivate()) {
DiscordIO.sendMessage(message.getChannel(), ":mailbox_with_mail:");
}
}
use of de.nikos410.discordbot.framework.annotations.CommandSubscriber in project de-DiscordBot by DACH-Discord.
the class BotSetup method command_listModules.
@CommandSubscriber(command = "modules", help = "Alle Module anzeigen")
public void command_listModules(final IMessage message) {
final EmbedBuilder embedBuilder = new EmbedBuilder();
// List loaded modules
final StringBuilder loadedBuilder = new StringBuilder();
for (ModuleWrapper module : bot.getLoadedModules()) {
loadedBuilder.append(module.getName());
loadedBuilder.append('\n');
}
final String loadedModulesString = loadedBuilder.toString().isEmpty() ? "_keine_" : loadedBuilder.toString();
embedBuilder.appendField("Aktivierte Module", loadedModulesString, true);
// List unloaded modules
final StringBuilder unloadedBuilder = new StringBuilder();
for (ModuleWrapper module : bot.getUnloadedModules()) {
unloadedBuilder.append(module.getName());
unloadedBuilder.append('\n');
}
final String unloadedModulesString = unloadedBuilder.toString().isEmpty() ? "_keine_" : unloadedBuilder.toString();
embedBuilder.appendField("Deaktivierte Module", unloadedModulesString, true);
// Add failed modules, if present
final List<ModuleWrapper> failedModules = bot.getFailedModules();
if (!failedModules.isEmpty()) {
final StringBuilder failedBuilder = new StringBuilder();
for (ModuleWrapper module : failedModules) {
failedBuilder.append(module.getName());
failedBuilder.append('\n');
}
embedBuilder.appendField("Folgende Module konnten nicht geladen werden:", failedBuilder.toString(), true);
}
DiscordIO.sendEmbed(message.getChannel(), embedBuilder.build());
}
use of de.nikos410.discordbot.framework.annotations.CommandSubscriber in project de-DiscordBot by DACH-Discord.
the class GameStats method command_playing.
@CommandSubscriber(command = "playing", help = "Zeigt alle Nutzer die das angegebene Spiel spielen", pmAllowed = false)
public void command_playing(final IMessage message, final String game) {
final IGuild guild = message.getGuild();
// Similar games
final List<String> similarGames = findSimilarKeys(game, guild);
// Users who play the game right now
List<IUser> playingNowUsers = new ArrayList<>();
for (IUser user : guild.getUsers()) {
final String currentUserGame = getCurrentGame(user);
if (game.equalsIgnoreCase(currentUserGame)) {
playingNowUsers.add(user);
}
}
// Users who played the game at any point
List<IUser> playingAnyUsers = new ArrayList<>();
if (gameStatsJSON.has(guild.getStringID())) {
final JSONObject guildJSON = gameStatsJSON.getJSONObject(guild.getStringID());
if (guildJSON.has(game.toLowerCase())) {
final JSONArray gameArray = guildJSON.getJSONArray(game.toLowerCase());
for (int i = 0; i < gameArray.length(); i++) {
final IUser user = guild.getUserByID(gameArray.getLong(i));
// Only add if user isn't playing right now
if (user != null && !playingNowUsers.contains(user)) {
playingAnyUsers.add(user);
}
}
}
}
sendPlayingResponse(message, game, playingNowUsers, playingAnyUsers, similarGames);
}
Aggregations