Search in sources :

Example 6 with PermissionLevel

use of de.nikos410.discordbot.framework.PermissionLevel in project de-DiscordBot by DACH-Discord.

the class DiscordBot method discoverCommands.

private List<CommandWrapper> discoverCommands(final ModuleWrapper moduleWrapper) {
    LOG.debug("Registering command(s) for module '{}'.", moduleWrapper.getName());
    final List<CommandWrapper> commands = new LinkedList<>();
    final Method[] allMethods = moduleWrapper.getModuleClass().getMethods();
    final List<Method> commandMethods = Arrays.stream(allMethods).filter(module -> module.isAnnotationPresent(CommandSubscriber.class)).collect(Collectors.toList());
    for (final Method method : commandMethods) {
        // Register methods with the @CommandSubscriber as commands
        // All annotations of type CommandSubscriber declared for that Method. Should be exactly 1
        final CommandSubscriber annotation = method.getDeclaredAnnotationsByType(CommandSubscriber.class)[0];
        // Get command properties from annotation
        final String commandName = annotation.command();
        final String commandHelp = annotation.help();
        final boolean pmAllowed = annotation.pmAllowed();
        final PermissionLevel permissionLevel = annotation.permissionLevel();
        final boolean passContext = annotation.passContext();
        final boolean ignoreParameterCount = annotation.ignoreParameterCount();
        final int parameterCount = method.getParameterCount() - 1;
        if ((parameterCount >= 0 && parameterCount <= 5) || ignoreParameterCount) {
            final CommandWrapper commandWrapper = new CommandWrapper(commandName, commandHelp, moduleWrapper, method, pmAllowed, permissionLevel, parameterCount, passContext, ignoreParameterCount);
            commands.add(commandWrapper);
            LOG.debug("Saved command '{}'.", commandName);
        } else {
            LOG.warn("Method '{}' has an invalid number of arguments. Skipping", commandName);
        }
    }
    return commands;
}
Also used : java.util(java.util) Authorization(de.nikos410.discordbot.util.discord.Authorization) LoggerFactory(org.slf4j.LoggerFactory) MessageUpdateEvent(sx.blah.discord.handle.impl.events.guild.channel.message.MessageUpdateEvent) Reflections(org.reflections.Reflections) DiscordIO(de.nikos410.discordbot.util.discord.DiscordIO) ModuleWrapper(de.nikos410.discordbot.framework.ModuleWrapper) ReadyEvent(sx.blah.discord.handle.impl.events.ReadyEvent) MessageReceivedEvent(sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent) JSONObject(org.json.JSONObject) ModuleStatus(de.nikos410.discordbot.framework.ModuleWrapper.ModuleStatus) InitializationException(de.nikos410.discordbot.exception.InitializationException) CommandWrapper(de.nikos410.discordbot.framework.CommandWrapper) Method(java.lang.reflect.Method) Path(java.nio.file.Path) Logger(org.slf4j.Logger) PermissionLevel(de.nikos410.discordbot.framework.PermissionLevel) Files(java.nio.file.Files) CommandModule(de.nikos410.discordbot.framework.CommandModule) EventSubscriber(sx.blah.discord.api.events.EventSubscriber) Instant(java.time.Instant) UserUtils(de.nikos410.discordbot.util.discord.UserUtils) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) TimeUnit(java.util.concurrent.TimeUnit) IDiscordClient(sx.blah.discord.api.IDiscordClient) sx.blah.discord.handle.obj(sx.blah.discord.handle.obj) ChronoUnit(java.time.temporal.ChronoUnit) BotSetup(de.nikos410.discordbot.modules.BotSetup) IOUtil(de.nikos410.discordbot.util.io.IOUtil) Paths(java.nio.file.Paths) DiscordException(sx.blah.discord.util.DiscordException) EventDispatcher(sx.blah.discord.api.events.EventDispatcher) CommandSubscriber(de.nikos410.discordbot.framework.annotations.CommandSubscriber) JSONArray(org.json.JSONArray) CommandWrapper(de.nikos410.discordbot.framework.CommandWrapper) Method(java.lang.reflect.Method) PermissionLevel(de.nikos410.discordbot.framework.PermissionLevel) CommandSubscriber(de.nikos410.discordbot.framework.annotations.CommandSubscriber)

Example 7 with PermissionLevel

use of de.nikos410.discordbot.framework.PermissionLevel in project de-DiscordBot by DACH-Discord.

the class Rules method command_setRegeln.

@CommandSubscriber(command = "setRegeln", help = "Regeln (deutsch) ändern", permissionLevel = PermissionLevel.ADMIN, pmAllowed = false)
public void command_setRegeln(final IMessage message, final String rulesDE) {
    final IGuild guild = message.getGuild();
    final JSONObject guildJSON = getJSONForGuild(guild);
    guildJSON.put("rulesDE", rulesDE);
    saveJSON();
    DiscordIO.sendMessage(message.getChannel(), ":white_check_mark: Regeln (DE) geändert:");
    DiscordIO.sendMessage(message.getChannel(), rulesDE);
    LOG.info(String.format("%s changed rules. (DE)", UserUtils.makeUserString(message.getAuthor(), message.getGuild())));
}
Also used : JSONObject(org.json.JSONObject) IGuild(sx.blah.discord.handle.obj.IGuild) CommandSubscriber(de.nikos410.discordbot.framework.annotations.CommandSubscriber)

Example 8 with PermissionLevel

use of de.nikos410.discordbot.framework.PermissionLevel in project de-DiscordBot by DACH-Discord.

the class Rules method command_setFooter.

@CommandSubscriber(command = "setFooter", help = "Footer der Begüßungsnachricht ändern.", permissionLevel = PermissionLevel.ADMIN, pmAllowed = false)
public void command_setFooter(final IMessage message, final String footer) {
    final IGuild guild = message.getGuild();
    final JSONObject guildJSON = getJSONForGuild(guild);
    guildJSON.put("footer", footer);
    saveJSON();
    DiscordIO.sendMessage(message.getChannel(), ":white_check_mark: Begrüßungs-Footer geändert:");
    DiscordIO.sendMessage(message.getChannel(), footer);
    LOG.info(String.format("%s changed rules. (DE)", UserUtils.makeUserString(message.getAuthor(), message.getGuild())));
}
Also used : JSONObject(org.json.JSONObject) IGuild(sx.blah.discord.handle.obj.IGuild) CommandSubscriber(de.nikos410.discordbot.framework.annotations.CommandSubscriber)

Example 9 with PermissionLevel

use of de.nikos410.discordbot.framework.PermissionLevel in project de-DiscordBot by DACH-Discord.

the class UserLog method command_userlogTest.

@CommandSubscriber(command = "userlogTest", help = "Userlog-Ausgabe testen", permissionLevel = PermissionLevel.ADMIN)
public void command_userlogTest(final IMessage message) {
    final IUser user = message.getAuthor();
    final IGuild guild = message.getGuild();
    final JSONObject guildJSON = getJSONForGuild(guild);
    if (!guildJSON.has("channel")) {
        DiscordIO.sendMessage(message.getChannel(), "Fehler! Kein Kanal hinterlegt!");
        return;
    }
    final long channelID = guildJSON.getLong("channel");
    final IChannel channel = guild.getChannelByID(channelID);
    if (channel != null) {
        userJoinNotify(user, channel);
        userLeaveNotify(user, channel);
        userBanNotify(user, channel);
    }
}
Also used : IChannel(sx.blah.discord.handle.obj.IChannel) JSONObject(org.json.JSONObject) IUser(sx.blah.discord.handle.obj.IUser) IGuild(sx.blah.discord.handle.obj.IGuild) CommandSubscriber(de.nikos410.discordbot.framework.annotations.CommandSubscriber)

Example 10 with PermissionLevel

use of de.nikos410.discordbot.framework.PermissionLevel in project de-DiscordBot by DACH-Discord.

the class UserLog method command_disableUserlog.

@CommandSubscriber(command = "disableUserlog", help = "Userlog deaktivieren", permissionLevel = PermissionLevel.ADMIN)
public void command_disableUserlog(final IMessage message) {
    final IGuild guild = message.getGuild();
    final JSONObject guildJSON = getJSONForGuild(guild);
    guildJSON.put("on", false);
    saveUserLogJSON();
    // :white_check_mark:
    message.addReaction(ReactionEmoji.of("✅"));
}
Also used : JSONObject(org.json.JSONObject) IGuild(sx.blah.discord.handle.obj.IGuild) CommandSubscriber(de.nikos410.discordbot.framework.annotations.CommandSubscriber)

Aggregations

CommandSubscriber (de.nikos410.discordbot.framework.annotations.CommandSubscriber)19 JSONObject (org.json.JSONObject)17 IGuild (sx.blah.discord.handle.obj.IGuild)11 CommandSubscriber (de.nikos410.discordBot.util.modular.annotations.CommandSubscriber)4 ChronoUnit (java.time.temporal.ChronoUnit)4 CommandWrapper (de.nikos410.discordbot.framework.CommandWrapper)2 PermissionLevel (de.nikos410.discordbot.framework.PermissionLevel)2 CommandUtils (de.nikos410.discordbot.util.CommandUtils)2 Method (java.lang.reflect.Method)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 IChannel (sx.blah.discord.handle.obj.IChannel)2 IUser (sx.blah.discord.handle.obj.IUser)2 CommandModule (de.nikos410.discordBot.util.modular.annotations.CommandModule)1 InitializationException (de.nikos410.discordbot.exception.InitializationException)1 CommandModule (de.nikos410.discordbot.framework.CommandModule)1 ModuleWrapper (de.nikos410.discordbot.framework.ModuleWrapper)1 ModuleStatus (de.nikos410.discordbot.framework.ModuleWrapper.ModuleStatus)1 BotSetup (de.nikos410.discordbot.modules.BotSetup)1 Authorization (de.nikos410.discordbot.util.discord.Authorization)1 DiscordIO (de.nikos410.discordbot.util.discord.DiscordIO)1