-
Notifications
You must be signed in to change notification settings - Fork 24
Move Conversation #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Move Conversation #318
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
42906b7
Introduction of the `/move-conversation` command
jasonlessenich 8222561
Added spaces between constants
jasonlessenich 88f2802
Removed `MoveConversationCommand#REQUIRED_PERMISSIONS`
jasonlessenich 824ca9a
Made it more clear that this is only _suggesting_ to move the convers…
jasonlessenich 829dff9
Adjusted command description to better reflect that this is just a su…
jasonlessenich f4f112a
Moved the order the messages are sent to ensure that the message whic…
jasonlessenich 8eeb0b8
Added SelfMember Permission Checks & Error Consumer
jasonlessenich 4f98cc5
Added Buttons
jasonlessenich 2d8c01d
Removed button from the "Move To" Message
jasonlessenich d30e168
Added Credits to README.md
jasonlessenich c4009db
Added check for slowmode
jasonlessenich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/net/javadiscord/javabot/systems/user_commands/MoveConversationCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package net.javadiscord.javabot.systems.user_commands; | ||
|
|
||
| import com.dynxsty.dih4jda.interactions.commands.SlashCommand; | ||
| import net.dv8tion.jda.api.Permission; | ||
| import net.dv8tion.jda.api.entities.ChannelType; | ||
| import net.dv8tion.jda.api.entities.GuildMessageChannel; | ||
| import net.dv8tion.jda.api.entities.Member; | ||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; | ||
| import net.dv8tion.jda.api.interactions.commands.OptionMapping; | ||
| import net.dv8tion.jda.api.interactions.commands.OptionType; | ||
| import net.dv8tion.jda.api.interactions.commands.build.Commands; | ||
| import net.dv8tion.jda.api.interactions.commands.build.OptionData; | ||
| import net.dv8tion.jda.api.interactions.components.buttons.Button; | ||
| import net.dv8tion.jda.api.requests.restaction.MessageAction; | ||
| import net.javadiscord.javabot.Bot; | ||
| import net.javadiscord.javabot.data.config.guild.ModerationConfig; | ||
| import net.javadiscord.javabot.util.Responses; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.Collections; | ||
|
|
||
| /** | ||
| * <h3>This class represents the /move-conversation command.</h3> | ||
| */ | ||
| public class MoveConversationCommand extends SlashCommand { | ||
| private static final String MOVE_TO_MESSAGE = "``` ```\n\uD83D\uDCE4 %s has suggested to move **this conversation** over to %s\n%s\n\n``` ```"; | ||
|
|
||
| private static final String MOVED_FROM_MESSAGE = "\uD83D\uDCE5 Conversation moved **here** from %s by %s\n%s"; | ||
|
|
||
| /** | ||
| * The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}. | ||
| */ | ||
| public MoveConversationCommand() { | ||
| setSlashCommandData(Commands.slash("move-conversation", "Suggest to move the current conversation into another channel!") | ||
| .addOptions( | ||
| new OptionData(OptionType.CHANNEL, "channel", "Where should the current conversation be continued?", true) | ||
| .setChannelTypes(ChannelType.TEXT, ChannelType.GUILD_NEWS_THREAD, ChannelType.GUILD_PRIVATE_THREAD, ChannelType.GUILD_PUBLIC_THREAD, ChannelType.VOICE, ChannelType.STAGE) | ||
| ).setGuildOnly(true) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(@NotNull SlashCommandInteractionEvent event) { | ||
| OptionMapping channelMapping = event.getOption("channel"); | ||
| if (channelMapping == null) { | ||
| Responses.replyMissingArguments(event).queue(); | ||
| return; | ||
| } | ||
| if (event.getGuild() == null || event.getMember() == null) { | ||
| Responses.replyGuildOnly(event).queue(); | ||
| return; | ||
| } | ||
| GuildMessageChannel channel = channelMapping.getAsChannel().asGuildMessageChannel(); | ||
| if (event.getChannel().getIdLong() == channel.getIdLong()) { | ||
| Responses.warning(event, "Invalid Channel", "You cannot move the conversation to the same channel!").queue(); | ||
| return; | ||
| } | ||
| if (channelMapping.getAsChannel().getType() == ChannelType.TEXT && channelMapping.getAsChannel().asTextChannel().getSlowmode() > 0) { | ||
| Responses.warning(event, "Invalid Channel", "You cannot move the conversation to a channel that has slowmode enabled!").queue(); | ||
| return; | ||
| } | ||
| if (isInvalidChannel(event.getMember(), channel)) { | ||
| Responses.warning(event, "Invalid Channel", "You're not allowed to move the conversation to %s", channel.getAsMention()).queue(); | ||
| return; | ||
| } | ||
| if (isInvalidChannel(event.getGuild().getSelfMember(), channel)) { | ||
| Responses.error(event, "Insufficient Permissions", "I'm not allowed to sent messages to %s", channel.getAsMention()).queue(); | ||
| return; | ||
| } | ||
| event.deferReply(true).queue(); | ||
| sendMovedFromChannelMessage(event, channel).queue(movedTo -> | ||
| sendMoveToChannelMessage(event, channel, movedTo).queue(movedFrom -> | ||
| editMovedFromChannelMessage(event, movedFrom, movedTo).queue(s -> | ||
| event.getHook().sendMessage("Done!").queue() | ||
| ) | ||
| ), err -> Responses.error(event, "Could not move conversation: " + err.getMessage()).queue() | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the specified {@link Member} does have the required permissions in order | ||
| * to move the conversation to the specified channel. | ||
| * | ||
| * @param member The {@link Member} to check for. | ||
| * @param channel The {@link GuildMessageChannel} the conversation should be moved to. | ||
| * @return Whether the {@link GuildMessageChannel} provided by the {@link Member} is invalid. | ||
| */ | ||
| private boolean isInvalidChannel(@NotNull Member member, GuildMessageChannel channel) { | ||
| ModerationConfig config = Bot.config.get(member.getGuild()).getModerationConfig(); | ||
| return !member.hasPermission(channel, Permission.MESSAGE_SEND, Permission.VIEW_CHANNEL) || | ||
| // check thread permissions | ||
| channel.getType().isThread() && !member.hasPermission(channel, Permission.MESSAGE_SEND_IN_THREADS) || | ||
| // check for suggestion channel check for share knowledge channel | ||
| channel.getIdLong() == config.getSuggestionChannelId() || channel.getIdLong() == config.getShareKnowledgeChannelId() || | ||
| // check for job channel check for application channel | ||
| channel.getIdLong() == config.getJobChannelId() || channel.getIdLong() == config.getApplicationChannelId() || | ||
| // check for log channel | ||
| channel.getIdLong() == config.getLogChannelId(); | ||
| } | ||
|
|
||
| private @NotNull MessageAction sendMoveToChannelMessage(@NotNull SlashCommandInteractionEvent event, @NotNull GuildMessageChannel channel, @NotNull Message movedTo) { | ||
| return event.getChannel().sendMessageFormat(MOVE_TO_MESSAGE, event.getUser().getAsMention(), channel.getAsMention(), movedTo.getJumpUrl()) | ||
| .allowedMentions(Collections.emptySet()); | ||
| } | ||
|
|
||
| private @NotNull MessageAction sendMovedFromChannelMessage(@NotNull SlashCommandInteractionEvent event, @NotNull GuildMessageChannel channel) { | ||
| return channel.sendMessageFormat(MOVED_FROM_MESSAGE, event.getChannel().getAsMention(), event.getUser().getAsMention(), "Waiting for Link...") | ||
| .allowedMentions(Collections.emptySet()); | ||
| } | ||
|
|
||
| private @NotNull MessageAction editMovedFromChannelMessage(@NotNull SlashCommandInteractionEvent event, @NotNull Message movedFrom, @NotNull Message movedTo) { | ||
| return movedTo.editMessageFormat(MOVED_FROM_MESSAGE, event.getChannel().getAsMention(), event.getUser().getAsMention(), movedFrom.getJumpUrl()) | ||
| .setActionRow(Button.link(movedFrom.getJumpUrl(), "Jump to Message")) | ||
| .allowedMentions(Collections.emptySet()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.