|
| 1 | +package net.javadiscord.javabot.systems.user_commands; |
| 2 | + |
| 3 | +import com.dynxsty.dih4jda.interactions.commands.SlashCommand; |
| 4 | +import net.dv8tion.jda.api.Permission; |
| 5 | +import net.dv8tion.jda.api.entities.ChannelType; |
| 6 | +import net.dv8tion.jda.api.entities.GuildMessageChannel; |
| 7 | +import net.dv8tion.jda.api.entities.Member; |
| 8 | +import net.dv8tion.jda.api.entities.Message; |
| 9 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 10 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 11 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 12 | +import net.dv8tion.jda.api.interactions.commands.build.Commands; |
| 13 | +import net.dv8tion.jda.api.interactions.commands.build.OptionData; |
| 14 | +import net.dv8tion.jda.api.interactions.components.buttons.Button; |
| 15 | +import net.dv8tion.jda.api.requests.restaction.MessageAction; |
| 16 | +import net.javadiscord.javabot.Bot; |
| 17 | +import net.javadiscord.javabot.data.config.guild.ModerationConfig; |
| 18 | +import net.javadiscord.javabot.util.Responses; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | + |
| 21 | +import java.util.Collections; |
| 22 | + |
| 23 | +/** |
| 24 | + * <h3>This class represents the /move-conversation command.</h3> |
| 25 | + */ |
| 26 | +public class MoveConversationCommand extends SlashCommand { |
| 27 | + private static final String MOVE_TO_MESSAGE = "``` ```\n\uD83D\uDCE4 %s has suggested to move **this conversation** over to %s\n%s\n\n``` ```"; |
| 28 | + |
| 29 | + private static final String MOVED_FROM_MESSAGE = "\uD83D\uDCE5 Conversation moved **here** from %s by %s\n%s"; |
| 30 | + |
| 31 | + /** |
| 32 | + * The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}. |
| 33 | + */ |
| 34 | + public MoveConversationCommand() { |
| 35 | + setSlashCommandData(Commands.slash("move-conversation", "Suggest to move the current conversation into another channel!") |
| 36 | + .addOptions( |
| 37 | + new OptionData(OptionType.CHANNEL, "channel", "Where should the current conversation be continued?", true) |
| 38 | + .setChannelTypes(ChannelType.TEXT, ChannelType.GUILD_NEWS_THREAD, ChannelType.GUILD_PRIVATE_THREAD, ChannelType.GUILD_PUBLIC_THREAD, ChannelType.VOICE, ChannelType.STAGE) |
| 39 | + ).setGuildOnly(true) |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void execute(@NotNull SlashCommandInteractionEvent event) { |
| 45 | + OptionMapping channelMapping = event.getOption("channel"); |
| 46 | + if (channelMapping == null) { |
| 47 | + Responses.replyMissingArguments(event).queue(); |
| 48 | + return; |
| 49 | + } |
| 50 | + if (event.getGuild() == null || event.getMember() == null) { |
| 51 | + Responses.replyGuildOnly(event).queue(); |
| 52 | + return; |
| 53 | + } |
| 54 | + GuildMessageChannel channel = channelMapping.getAsChannel().asGuildMessageChannel(); |
| 55 | + if (event.getChannel().getIdLong() == channel.getIdLong()) { |
| 56 | + Responses.warning(event, "Invalid Channel", "You cannot move the conversation to the same channel!").queue(); |
| 57 | + return; |
| 58 | + } |
| 59 | + if (channelMapping.getAsChannel().getType() == ChannelType.TEXT && channelMapping.getAsChannel().asTextChannel().getSlowmode() > 0) { |
| 60 | + Responses.warning(event, "Invalid Channel", "You cannot move the conversation to a channel that has slowmode enabled!").queue(); |
| 61 | + return; |
| 62 | + } |
| 63 | + if (isInvalidChannel(event.getMember(), channel)) { |
| 64 | + Responses.warning(event, "Invalid Channel", "You're not allowed to move the conversation to %s", channel.getAsMention()).queue(); |
| 65 | + return; |
| 66 | + } |
| 67 | + if (isInvalidChannel(event.getGuild().getSelfMember(), channel)) { |
| 68 | + Responses.error(event, "Insufficient Permissions", "I'm not allowed to sent messages to %s", channel.getAsMention()).queue(); |
| 69 | + return; |
| 70 | + } |
| 71 | + event.deferReply(true).queue(); |
| 72 | + sendMovedFromChannelMessage(event, channel).queue(movedTo -> |
| 73 | + sendMoveToChannelMessage(event, channel, movedTo).queue(movedFrom -> |
| 74 | + editMovedFromChannelMessage(event, movedFrom, movedTo).queue(s -> |
| 75 | + event.getHook().sendMessage("Done!").queue() |
| 76 | + ) |
| 77 | + ), err -> Responses.error(event, "Could not move conversation: " + err.getMessage()).queue() |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Checks if the specified {@link Member} does have the required permissions in order |
| 83 | + * to move the conversation to the specified channel. |
| 84 | + * |
| 85 | + * @param member The {@link Member} to check for. |
| 86 | + * @param channel The {@link GuildMessageChannel} the conversation should be moved to. |
| 87 | + * @return Whether the {@link GuildMessageChannel} provided by the {@link Member} is invalid. |
| 88 | + */ |
| 89 | + private boolean isInvalidChannel(@NotNull Member member, GuildMessageChannel channel) { |
| 90 | + ModerationConfig config = Bot.config.get(member.getGuild()).getModerationConfig(); |
| 91 | + return !member.hasPermission(channel, Permission.MESSAGE_SEND, Permission.VIEW_CHANNEL) || |
| 92 | + // check thread permissions |
| 93 | + channel.getType().isThread() && !member.hasPermission(channel, Permission.MESSAGE_SEND_IN_THREADS) || |
| 94 | + // check for suggestion channel check for share knowledge channel |
| 95 | + channel.getIdLong() == config.getSuggestionChannelId() || channel.getIdLong() == config.getShareKnowledgeChannelId() || |
| 96 | + // check for job channel check for application channel |
| 97 | + channel.getIdLong() == config.getJobChannelId() || channel.getIdLong() == config.getApplicationChannelId() || |
| 98 | + // check for log channel |
| 99 | + channel.getIdLong() == config.getLogChannelId(); |
| 100 | + } |
| 101 | + |
| 102 | + private @NotNull MessageAction sendMoveToChannelMessage(@NotNull SlashCommandInteractionEvent event, @NotNull GuildMessageChannel channel, @NotNull Message movedTo) { |
| 103 | + return event.getChannel().sendMessageFormat(MOVE_TO_MESSAGE, event.getUser().getAsMention(), channel.getAsMention(), movedTo.getJumpUrl()) |
| 104 | + .allowedMentions(Collections.emptySet()); |
| 105 | + } |
| 106 | + |
| 107 | + private @NotNull MessageAction sendMovedFromChannelMessage(@NotNull SlashCommandInteractionEvent event, @NotNull GuildMessageChannel channel) { |
| 108 | + return channel.sendMessageFormat(MOVED_FROM_MESSAGE, event.getChannel().getAsMention(), event.getUser().getAsMention(), "Waiting for Link...") |
| 109 | + .allowedMentions(Collections.emptySet()); |
| 110 | + } |
| 111 | + |
| 112 | + private @NotNull MessageAction editMovedFromChannelMessage(@NotNull SlashCommandInteractionEvent event, @NotNull Message movedFrom, @NotNull Message movedTo) { |
| 113 | + return movedTo.editMessageFormat(MOVED_FROM_MESSAGE, event.getChannel().getAsMention(), event.getUser().getAsMention(), movedFrom.getJumpUrl()) |
| 114 | + .setActionRow(Button.link(movedFrom.getJumpUrl(), "Jump to Message")) |
| 115 | + .allowedMentions(Collections.emptySet()); |
| 116 | + } |
| 117 | +} |
0 commit comments