Search in sources :

Example 1 with SlashCommandEvent

use of com.jagrosh.jdautilities.command.SlashCommandEvent in project MMDBot by MinecraftModDevelopment.

the class CmdCommunityChannel method execute.

/**
 * Execute.
 *
 * @param event The {@link SlashCommandEvent CommandEvent} that triggered this Command.
 */
@Override
protected void execute(final SlashCommandEvent event) {
    if (!Utils.checkCommand(this, event)) {
        return;
    }
    final var guild = event.getGuild();
    final var author = guild.getMember(event.getUser());
    if (author == null) {
        return;
    }
    Member user = event.getOption("user").getAsMember();
    String channel = event.getOption("channel").getAsString();
    final var categoryID = MMDBot.getConfig().getCommunityChannelCategory();
    final var category = guild.getCategoryById(categoryID);
    if (categoryID == 0 || category == null) {
        MMDBot.LOGGER.warn("Community channel category is incorrectly configured");
        event.reply("Community channel category is incorrectly configured. Please contact the bot maintainers.").queue();
        return;
    }
    final Set<Permission> ownerPermissions = MMDBot.getConfig().getCommunityChannelOwnerPermissions();
    if (ownerPermissions.isEmpty()) {
        MMDBot.LOGGER.warn("Community channel owner permissions is incorrectly configured");
        event.reply("Channel owner permissions is incorrectly configured. Please contact the bot maintainers.").queue();
        return;
    }
    final Set<Permission> diff = Sets.difference(ownerPermissions, event.getGuild().getSelfMember().getPermissions());
    if (!diff.isEmpty()) {
        MMDBot.LOGGER.warn("Cannot assign permissions to channel owner due to insufficient permissions: {}", diff);
        event.reply("Cannot assign certain permissions to channel owner due to insufficient permissions; " + "continuing anyway...").queue();
        ownerPermissions.removeIf(diff::contains);
    }
    final List<Emote> emote = new ArrayList<>(4);
    emote.addAll(guild.getEmotesByName("mmd1", true));
    emote.addAll(guild.getEmotesByName("mmd2", true));
    emote.addAll(guild.getEmotesByName("mmd3", true));
    emote.addAll(guild.getEmotesByName("mmd4", true));
    // Flavor text: if the emotes are available, use them, else just use plain MMD
    final var emoteText = emote.size() == 4 ? emote.stream().map(Emote::getAsMention).collect(Collectors.joining()) : "";
    final var flavorText = emoteText.isEmpty() ? "MMD" : emoteText;
    MMDBot.LOGGER.info("Creating new community channel for {} ({}), named \"{}\" (command issued by {} ({}))", user.getEffectiveName(), user.getUser().getName(), channel, author.getEffectiveName(), author.getUser().getName());
    category.createTextChannel(channel).flatMap(ch -> category.modifyTextChannelPositions().sortOrder(Comparator.comparing(GuildChannel::getName)).map($ -> ch)).flatMap(ch -> ch.putPermissionOverride(user).setAllow(ownerPermissions).map($ -> ch)).flatMap(ch -> ch.sendMessage(new MessageBuilder().appendFormat("Welcome %s to your new community channel, %s!%n", user.getAsMention(), ch.getAsMention()).append('\n').append("Please adhere to the Code of Conduct of the server").appendFormat(" (which can be visited from the <#%s> channel),", MMDBot.getConfig().getChannel("info.rules")).append(" specifically the Channel Policy section.").append('\n').append('\n').appendFormat("Thank you, and enjoy your new home here at %s!", flavorText).build()).map($ -> ch)).queue(c -> event.reply("Successfully created community channel at " + c.getAsMention() + "!").queue());
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Utils(com.mcmoddev.mmdbot.utilities.Utils) Permission(net.dv8tion.jda.api.Permission) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) GuildChannel(net.dv8tion.jda.api.entities.GuildChannel) Set(java.util.Set) Member(net.dv8tion.jda.api.entities.Member) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ArrayList(java.util.ArrayList) SlashCommandEvent(com.jagrosh.jdautilities.command.SlashCommandEvent) MMDBot(com.mcmoddev.mmdbot.MMDBot) List(java.util.List) SlashCommand(com.jagrosh.jdautilities.command.SlashCommand) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Comparator(java.util.Comparator) Emote(net.dv8tion.jda.api.entities.Emote) EnumSet(java.util.EnumSet) GuildChannel(net.dv8tion.jda.api.entities.GuildChannel) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Emote(net.dv8tion.jda.api.entities.Emote) Permission(net.dv8tion.jda.api.Permission) ArrayList(java.util.ArrayList) Member(net.dv8tion.jda.api.entities.Member)

Example 2 with SlashCommandEvent

use of com.jagrosh.jdautilities.command.SlashCommandEvent in project MMDBot by MinecraftModDevelopment.

the class CmdAddTrick method execute.

@Override
protected void execute(final SlashCommandEvent event) {
    if (!Utils.checkCommand(this, event)) {
        return;
    }
    if (!event.isFromGuild()) {
        event.deferReply(true).setContent("This command only works in a guild!").queue();
        return;
    }
    if (!Utils.memberHasRole(event.getMember(), MMDBot.getConfig().getRole("bot_maintainer"))) {
        event.deferReply(true).setContent("Only Bot Maintainers can use this command.").queue();
        return;
    }
    Trick trick = trickType.createFromCommand(event);
    Optional<Trick> originalTrick = Tricks.getTricks().stream().filter(t -> t.getNames().stream().anyMatch(n -> trick.getNames().contains(n))).findAny();
    originalTrick.ifPresentOrElse(original -> {
        Tricks.replaceTrick(original, trick);
        event.reply("Updated trick!").mentionRepliedUser(false).setEphemeral(true).queue();
    }, () -> {
        Tricks.addTrick(trick);
        event.reply("Added trick!").mentionRepliedUser(false).setEphemeral(true).queue();
    });
}
Also used : GistUtils(com.mcmoddev.mmdbot.gist.GistUtils) Command(com.jagrosh.jdautilities.command.Command) Utils(com.mcmoddev.mmdbot.utilities.Utils) IOException(java.io.IOException) CommandEvent(com.jagrosh.jdautilities.command.CommandEvent) Trick(com.mcmoddev.mmdbot.utilities.tricks.Trick) SlashCommandEvent(com.jagrosh.jdautilities.command.SlashCommandEvent) MMDBot(com.mcmoddev.mmdbot.MMDBot) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) SlashCommand(com.jagrosh.jdautilities.command.SlashCommand) Tricks(com.mcmoddev.mmdbot.utilities.tricks.Tricks) ScriptTrick(com.mcmoddev.mmdbot.utilities.tricks.ScriptTrick) Optional(java.util.Optional) Trick(com.mcmoddev.mmdbot.utilities.tricks.Trick) ScriptTrick(com.mcmoddev.mmdbot.utilities.tricks.ScriptTrick)

Aggregations

SlashCommand (com.jagrosh.jdautilities.command.SlashCommand)2 SlashCommandEvent (com.jagrosh.jdautilities.command.SlashCommandEvent)2 MMDBot (com.mcmoddev.mmdbot.MMDBot)2 Utils (com.mcmoddev.mmdbot.utilities.Utils)2 Sets (com.google.common.collect.Sets)1 Command (com.jagrosh.jdautilities.command.Command)1 CommandEvent (com.jagrosh.jdautilities.command.CommandEvent)1 GistUtils (com.mcmoddev.mmdbot.gist.GistUtils)1 ScriptTrick (com.mcmoddev.mmdbot.utilities.tricks.ScriptTrick)1 Trick (com.mcmoddev.mmdbot.utilities.tricks.Trick)1 Tricks (com.mcmoddev.mmdbot.utilities.tricks.Tricks)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 EnumSet (java.util.EnumSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1