Search in sources :

Example 1 with CommandEvent

use of com.jagrosh.jdautilities.command.CommandEvent in project GeyserDiscordBot by GeyserMC.

the class UnmuteCommand method execute.

@Override
protected void execute(CommandEvent event) {
    List<String> args = new ArrayList<>(Arrays.asList(event.getArgs().split(" ")));
    // Fetch the user
    Member member = BotHelpers.getMember(event.getGuild(), args.remove(0));
    // Check user is valid
    if (member == null) {
        event.getMessage().replyEmbeds(new EmbedBuilder().setTitle("Invalid user").setDescription("The user ID specified doesn't link with any valid user in this server.").setColor(BotColors.FAILURE.getColor()).build()).queue();
        return;
    }
    // Check we can target the user
    if (!event.getSelfMember().canInteract(member) || !event.getMember().canInteract(member)) {
        event.getMessage().replyEmbeds(new EmbedBuilder().setTitle("Higher role").setDescription("Either the bot or you cannot target that user.").setColor(BotColors.FAILURE.getColor()).build()).queue();
        return;
    }
    // Get the user from the member
    User user = member.getUser();
    boolean silent = false;
    // We clone the args here to prevent a CME
    for (String arg : args.toArray(new String[0])) {
        if (!arg.startsWith("-") || arg.length() < 2) {
            break;
        }
        if (arg.toCharArray()[1] == 's') {
            // Check for silent flag
            silent = true;
        } else {
            event.getMessage().replyEmbeds(new EmbedBuilder().setTitle("Invalid option").setDescription("The option `" + arg + "` is invalid").setColor(BotColors.FAILURE.getColor()).build()).queue();
        }
        args.remove(0);
    }
    // Get the reason or use None
    String reasonParts = String.join(" ", args);
    String reason;
    if (reasonParts.trim().isEmpty()) {
        reason = "*None*";
    } else {
        reason = reasonParts;
    }
    // Let the user know they're muted if we are not being silent
    if (!silent) {
        user.openPrivateChannel().queue((channel) -> channel.sendMessageEmbeds(new EmbedBuilder().setTitle("You have been unmuted from GeyserMC!").addField("Reason", reason, false).setTimestamp(Instant.now()).setColor(BotColors.FAILURE.getColor()).build()).queue(message -> {
        }, throwable -> {
        }), throwable -> {
        });
    }
    // Find and remove the 'muted' role
    Role muteRole = event.getGuild().getRolesByName("muted", true).get(0);
    event.getGuild().removeRoleFromMember(member, muteRole).queue();
    // UnPersist the role
    GeyserBot.storageManager.removePersistentRole(member, muteRole);
    // Log the change
    int id = GeyserBot.storageManager.addLog(event.getMember(), "unmute", user, reason);
    MessageEmbed mutedEmbed = new EmbedBuilder().setTitle("Unmuted user").addField("User", user.getAsMention(), false).addField("Staff member", event.getAuthor().getAsMention(), false).addField("Reason", reason, false).setFooter("ID: " + id).setTimestamp(Instant.now()).setColor(BotColors.SUCCESS.getColor()).build();
    // Send the embed as a reply and to the log
    ServerSettings.getLogChannel(event.getGuild()).sendMessageEmbeds(mutedEmbed).queue();
    event.getMessage().replyEmbeds(mutedEmbed).queue();
}
Also used : BotHelpers(org.geysermc.discordbot.util.BotHelpers) Arrays(java.util.Arrays) Command(com.jagrosh.jdautilities.command.Command) Permission(net.dv8tion.jda.api.Permission) CommandEvent(com.jagrosh.jdautilities.command.CommandEvent) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Member(net.dv8tion.jda.api.entities.Member) Instant(java.time.Instant) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) List(java.util.List) Role(net.dv8tion.jda.api.entities.Role) ServerSettings(org.geysermc.discordbot.storage.ServerSettings) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) BotColors(org.geysermc.discordbot.util.BotColors) GeyserBot(org.geysermc.discordbot.GeyserBot) Role(net.dv8tion.jda.api.entities.Role) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) User(net.dv8tion.jda.api.entities.User) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) ArrayList(java.util.ArrayList) Member(net.dv8tion.jda.api.entities.Member)

Example 2 with CommandEvent

use of com.jagrosh.jdautilities.command.CommandEvent in project GeyserDiscordBot by GeyserMC.

the class UnbanCommand method execute.

@Override
protected void execute(CommandEvent event) {
    List<String> args = new ArrayList<>(Arrays.asList(event.getArgs().split(" ")));
    // Fetch the user
    User user = BotHelpers.getUser(args.remove(0));
    // Check user is valid
    if (user == null) {
        event.getMessage().replyEmbeds(new EmbedBuilder().setTitle("Invalid user").setDescription("The user ID specified doesn't link with any valid user in this server.").setColor(BotColors.FAILURE.getColor()).build()).queue();
        return;
    }
    // Check if the user is banned
    try {
        event.getGuild().retrieveBan(user).complete();
    } catch (ErrorResponseException ignored) {
        event.getMessage().replyEmbeds(new EmbedBuilder().setTitle("User not banned").setDescription("The user ID specified doesn't have a ban on this server.").setColor(BotColors.FAILURE.getColor()).build()).queue();
        return;
    }
    boolean silent = false;
    // We clone the args here to prevent a CME
    for (String arg : args.toArray(new String[0])) {
        if (!arg.startsWith("-") || arg.length() < 2) {
            break;
        }
        if (arg.toCharArray()[1] == 's') {
            // Check for silent flag
            silent = true;
        } else {
            event.getMessage().replyEmbeds(new EmbedBuilder().setTitle("Invalid option").setDescription("The option `" + arg + "` is invalid").setColor(BotColors.FAILURE.getColor()).build()).queue();
        }
        args.remove(0);
    }
    // Get the reason or use None
    String reasonParts = String.join(" ", args);
    String reason;
    if (reasonParts.trim().isEmpty()) {
        reason = "*None*";
    } else {
        reason = reasonParts;
    }
    // Let the user know they're unbanned if we are not being silent
    if (!silent) {
        user.openPrivateChannel().queue((channel) -> channel.sendMessageEmbeds(new EmbedBuilder().setTitle("You have been unbanned from GeyserMC!").addField("Reason", reason, false).setTimestamp(Instant.now()).setColor(BotColors.SUCCESS.getColor()).build()).queue(message -> {
        }, throwable -> {
        }), throwable -> {
        });
    }
    // Unban user
    event.getGuild().unban(user).queue();
    // Log the change
    int id = GeyserBot.storageManager.addLog(event.getMember(), "unban", user, reason);
    MessageEmbed unbannedEmbed = new EmbedBuilder().setTitle("Unbanned user").addField("User", user.getAsMention(), false).addField("Staff member", event.getAuthor().getAsMention(), false).addField("Reason", reason, false).setFooter("ID: " + id).setTimestamp(Instant.now()).setColor(BotColors.SUCCESS.getColor()).build();
    // Send the embed as a reply and to the log
    ServerSettings.getLogChannel(event.getGuild()).sendMessageEmbeds(unbannedEmbed).queue();
    event.getMessage().replyEmbeds(unbannedEmbed).queue();
}
Also used : BotHelpers(org.geysermc.discordbot.util.BotHelpers) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) Arrays(java.util.Arrays) Command(com.jagrosh.jdautilities.command.Command) Permission(net.dv8tion.jda.api.Permission) CommandEvent(com.jagrosh.jdautilities.command.CommandEvent) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Instant(java.time.Instant) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) List(java.util.List) ServerSettings(org.geysermc.discordbot.storage.ServerSettings) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) BotColors(org.geysermc.discordbot.util.BotColors) GeyserBot(org.geysermc.discordbot.GeyserBot) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) User(net.dv8tion.jda.api.entities.User) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) ArrayList(java.util.ArrayList) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException)

Example 3 with CommandEvent

use of com.jagrosh.jdautilities.command.CommandEvent in project GeyserDiscordBot by GeyserMC.

the class TagAliasCommand method execute.

@Override
protected void execute(CommandEvent event) {
    if (SwearHandler.filteredMessages.contains(event.getMessage().getIdLong())) {
        return;
    }
    EmbedBuilder embed = new EmbedBuilder();
    String[] args = event.getArgs().split(" ");
    if (args.length == 0) {
        embed.setTitle("Invalid usage");
        embed.setDescription("Missing tag name. `" + PropertiesManager.getPrefix() + "tag alias <name>`");
        embed.setColor(BotColors.FAILURE.getColor());
        event.getMessage().replyEmbeds(embed.build()).queue();
        return;
    }
    Command foundTag = null;
    for (Command tag : TagsManager.getTags()) {
        if (tag.getName().equals(args[0]) || Arrays.stream(tag.getAliases()).anyMatch(s -> s.equals(args[0]))) {
            foundTag = tag;
            break;
        }
    }
    if (foundTag == null) {
        embed.setTitle("Missing tag");
        embed.setDescription("No tag with the name `" + args[0] + "`, do `" + PropertiesManager.getPrefix() + "tags` for the full list.");
        embed.setColor(BotColors.FAILURE.getColor());
        event.getMessage().replyEmbeds(embed.build()).queue();
        return;
    }
    if (foundTag.getAliases().length > 0) {
        embed.setTitle("Aliases for " + foundTag.getName() + " (" + foundTag.getAliases().length + ")");
        embed.setDescription("`" + String.join("`, `", foundTag.getAliases()) + "`");
        embed.setFooter("Use `" + PropertiesManager.getPrefix() + "tag <name>` to show a tag");
        embed.setColor(BotColors.SUCCESS.getColor());
    } else {
        embed.setTitle("No aliases for " + foundTag.getName());
        embed.setDescription("No aliases where found for the tag with the name `" + foundTag.getName() + "`.");
        embed.setColor(BotColors.FAILURE.getColor());
    }
    event.getMessage().replyEmbeds(embed.build()).queue();
}
Also used : Arrays(java.util.Arrays) SwearHandler(org.geysermc.discordbot.listeners.SwearHandler) Command(com.jagrosh.jdautilities.command.Command) PropertiesManager(org.geysermc.discordbot.util.PropertiesManager) CommandEvent(com.jagrosh.jdautilities.command.CommandEvent) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) BotColors(org.geysermc.discordbot.util.BotColors) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Command(com.jagrosh.jdautilities.command.Command)

Example 4 with CommandEvent

use of com.jagrosh.jdautilities.command.CommandEvent in project Vinny by kikkia.

the class FormattingUtils method cleanSayCommand.

public static String cleanSayCommand(CommandEvent commandEvent) {
    StringBuilder sb = new StringBuilder();
    String[] words = commandEvent.getArgs().split(" ");
    Pattern inviteRegx = Pattern.compile("discord(?:app\\.com\\/invite|\\.gg)\\/([a-z0-9]{1,16})", Pattern.CASE_INSENSITIVE);
    Pattern userMentionRegx = Pattern.compile("\\<\\@([0-9]+)\\>");
    Pattern roleMentionRegx = Pattern.compile("\\<\\@\\&([0-9]+)\\>");
    for (String word : words) {
        // Escape everyone
        if (word.equalsIgnoreCase("@everyone")) {
            sb.append("(at)everyone ");
            continue;
        }
        // Escape here
        if (word.equalsIgnoreCase("@here")) {
            sb.append("(at)here ");
            continue;
        }
        // Escape all user mentions
        Matcher mentionMatcher = userMentionRegx.matcher(word);
        if (mentionMatcher.find()) {
            String userId = mentionMatcher.group(1);
            // If any users match the id
            List<User> mentionedUser = commandEvent.getMessage().getMentionedUsers().stream().filter(user -> user.getId().equals(userId)).collect(Collectors.toList());
            // Check if user was mentioned
            if (!mentionedUser.isEmpty()) {
                sb.append(mentionedUser.get(0).getName()).append(" ");
                continue;
            }
        }
        // Escape all role mentions
        Matcher roleMatcher = roleMentionRegx.matcher(word);
        if (roleMatcher.find()) {
            String roleId = roleMatcher.group(1);
            List<Role> mentionedRole = commandEvent.getMessage().getMentionedRoles().stream().filter(role -> role.getId().equals(roleId)).collect(Collectors.toList());
            if (!mentionedRole.isEmpty()) {
                sb.append(mentionedRole.get(0).getName()).append(" ");
                continue;
            }
        }
        // Get rid of invites
        Matcher matcher = inviteRegx.matcher(word);
        if (matcher.find()) {
            word = "`invite`";
        }
        sb.append(word).append(" ");
    }
    return sb.toString();
}
Also used : QueuedAudioTrack(com.bot.voice.QueuedAudioTrack) Sauce(com.kikkia.jsauce.models.Sauce) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) CommandEvent(com.jagrosh.jdautilities.command.CommandEvent) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Member(net.dv8tion.jda.api.entities.Member) Collectors(java.util.stream.Collectors) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) java.awt(java.awt) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Matcher(java.util.regex.Matcher) Role(net.dv8tion.jda.api.entities.Role) Map(java.util.Map) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Pattern(java.util.regex.Pattern) DateFormat(java.text.DateFormat) IntervalFormatException(com.bot.exceptions.IntervalFormatException) Role(net.dv8tion.jda.api.entities.Role) Pattern(java.util.regex.Pattern) User(net.dv8tion.jda.api.entities.User) Matcher(java.util.regex.Matcher)

Example 5 with CommandEvent

use of com.jagrosh.jdautilities.command.CommandEvent in project MusicBot by jagrosh.

the class ForceRemoveCmd method doCommand.

@Override
public void doCommand(CommandEvent event) {
    if (event.getArgs().isEmpty()) {
        event.replyError("You need to mention a user!");
        return;
    }
    AudioHandler handler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler();
    if (handler.getQueue().isEmpty()) {
        event.replyError("There is nothing in the queue!");
        return;
    }
    User target;
    List<Member> found = FinderUtil.findMembers(event.getArgs(), event.getGuild());
    if (found.isEmpty()) {
        event.replyError("Unable to find the user!");
        return;
    } else if (found.size() > 1) {
        OrderedMenu.Builder builder = new OrderedMenu.Builder();
        for (int i = 0; i < found.size() && i < 4; i++) {
            Member member = found.get(i);
            builder.addChoice("**" + member.getUser().getName() + "**#" + member.getUser().getDiscriminator());
        }
        builder.setSelection((msg, i) -> removeAllEntries(found.get(i - 1).getUser(), event)).setText("Found multiple users:").setColor(event.getSelfMember().getColor()).useNumbers().setUsers(event.getAuthor()).useCancelButton(true).setCancel((msg) -> {
        }).setEventWaiter(bot.getWaiter()).setTimeout(1, TimeUnit.MINUTES).build().display(event.getChannel());
        return;
    } else {
        target = found.get(0).getUser();
    }
    removeAllEntries(target, event);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) DJCommand(com.jagrosh.jmusicbot.commands.DJCommand) Bot(com.jagrosh.jmusicbot.Bot) OrderedMenu(com.jagrosh.jdautilities.menu.OrderedMenu) Permission(net.dv8tion.jda.api.Permission) CommandEvent(com.jagrosh.jdautilities.command.CommandEvent) Member(net.dv8tion.jda.api.entities.Member) AudioHandler(com.jagrosh.jmusicbot.audio.AudioHandler) FinderUtil(com.jagrosh.jdautilities.commons.utils.FinderUtil) User(net.dv8tion.jda.api.entities.User) User(net.dv8tion.jda.api.entities.User) OrderedMenu(com.jagrosh.jdautilities.menu.OrderedMenu) AudioHandler(com.jagrosh.jmusicbot.audio.AudioHandler) Member(net.dv8tion.jda.api.entities.Member)

Aggregations

CommandEvent (com.jagrosh.jdautilities.command.CommandEvent)8 List (java.util.List)7 Permission (net.dv8tion.jda.api.Permission)6 TimeUnit (java.util.concurrent.TimeUnit)5 User (net.dv8tion.jda.api.entities.User)5 Bot (com.jagrosh.jmusicbot.Bot)4 AudioHandler (com.jagrosh.jmusicbot.audio.AudioHandler)4 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)4 Member (net.dv8tion.jda.api.entities.Member)4 Command (com.jagrosh.jdautilities.command.Command)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 MessageEmbed (net.dv8tion.jda.api.entities.MessageEmbed)3 FinderUtil (com.jagrosh.jdautilities.commons.utils.FinderUtil)2 OrderedMenu (com.jagrosh.jdautilities.menu.OrderedMenu)2 Paginator (com.jagrosh.jdautilities.menu.Paginator)2 JMusicBot (com.jagrosh.jmusicbot.JMusicBot)2 QueuedTrack (com.jagrosh.jmusicbot.audio.QueuedTrack)2 DJCommand (com.jagrosh.jmusicbot.commands.DJCommand)2 MusicCommand (com.jagrosh.jmusicbot.commands.MusicCommand)2