Search in sources :

Example 6 with ReplyCallbackAction

use of net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction in project dDiscordBot by DenizenScript.

the class DiscordInteractionCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag instruction = scriptEntry.getElement("instruction");
    DiscordInteractionTag interaction = scriptEntry.requiredArgForPrefix("interaction", DiscordInteractionTag.class);
    boolean ephemeral = scriptEntry.argAsBoolean("ephemeral");
    ElementTag attachFileName = scriptEntry.argForPrefixAsElement("attach_file_name", null);
    ElementTag attachFileText = scriptEntry.argForPrefixAsElement("attach_file_text", null);
    ObjectTag rows = scriptEntry.argForPrefix("rows", ObjectTag.class, true);
    ObjectTag message = scriptEntry.getObjectTag("message");
    if (scriptEntry.dbCallShouldDebug()) {
        // Note: attachFileText intentionally at end
        Debug.report(scriptEntry, getName(), instruction, interaction, ephemeral, rows, message, attachFileName, attachFileText);
    }
    DiscordInteractionInstruction instructionEnum = DiscordInteractionInstruction.valueOf(instruction.asString().toUpperCase());
    Runnable runner = () -> {
        try {
            switch(instructionEnum) {
                case DEFER:
                    {
                        if (interaction.interaction == null) {
                            handleError(scriptEntry, "Invalid interaction! Has it expired?");
                            return;
                        }
                        if (interaction.interaction instanceof IReplyCallback) {
                            ((IReplyCallback) interaction.interaction).deferReply(ephemeral).complete();
                        } else {
                            handleError(scriptEntry, "Interaction is not a reply callback!");
                        }
                        break;
                    }
                case EDIT:
                case REPLY:
                    {
                        if (interaction.interaction == null) {
                            handleError(scriptEntry, "Invalid interaction! Has it expired?");
                            return;
                        } else /*
                         * Messages aren't allowed to have attachments in ephemeral messages
                         * Since you can't see if the acknowledged message is ephemeral or not, this is a requirement so we don't have to try/catch
                         */
                        if (message == null) {
                            handleError(scriptEntry, "Must have a message!");
                            return;
                        }
                        MessageEmbed embed = null;
                        List<ActionRow> actionRows = DiscordMessageCommand.createRows(scriptEntry, rows);
                        if (message.shouldBeType(DiscordEmbedTag.class)) {
                            embed = message.asType(DiscordEmbedTag.class, scriptEntry.context).build(scriptEntry.getContext()).build();
                        }
                        if (instructionEnum == DiscordInteractionInstruction.EDIT) {
                            WebhookMessageUpdateAction<Message> action;
                            InteractionHook hook = ((IDeferrableCallback) interaction.interaction).getHook();
                            if (embed != null) {
                                action = hook.editOriginalEmbeds(embed);
                            } else {
                                action = hook.editOriginal(message.toString());
                            }
                            if (attachFileName != null) {
                                if (attachFileText != null) {
                                    action = action.addFile(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
                                } else {
                                    handleError(scriptEntry, "Failed to send attachment - missing content?");
                                }
                            }
                            if (actionRows != null) {
                                action = action.setActionRows(actionRows);
                            }
                            action.complete();
                        } else if (interaction.interaction.isAcknowledged()) {
                            WebhookMessageAction<Message> action;
                            InteractionHook hook = ((IDeferrableCallback) interaction.interaction).getHook();
                            if (embed != null) {
                                action = hook.sendMessageEmbeds(embed);
                            } else {
                                action = hook.sendMessage(message.toString());
                            }
                            if (attachFileName != null) {
                                if (attachFileText != null) {
                                    action = action.addFile(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
                                } else {
                                    handleError(scriptEntry, "Failed to send attachment - missing content?");
                                }
                            }
                            if (actionRows != null) {
                                action = action.addActionRows(actionRows);
                            }
                            action.complete();
                        } else {
                            ReplyCallbackAction action;
                            IReplyCallback replyTo = (IReplyCallback) interaction.interaction;
                            if (embed != null) {
                                action = replyTo.replyEmbeds(embed);
                            } else {
                                action = replyTo.reply(message.toString());
                            }
                            if (attachFileName != null) {
                                if (attachFileText != null) {
                                    action = action.addFile(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
                                } else {
                                    handleError(scriptEntry, "Failed to send attachment - missing content?");
                                }
                            }
                            if (actionRows != null) {
                                action = action.addActionRows(actionRows);
                            }
                            action = action.setEphemeral(ephemeral);
                            action.complete();
                        }
                        break;
                    }
                case DELETE:
                    {
                        if (interaction.interaction == null) {
                            handleError(scriptEntry, "Invalid interaction! Has it expired?");
                            return;
                        }
                        ((IDeferrableCallback) interaction.interaction).getHook().deleteOriginal().complete();
                        break;
                    }
            }
        } catch (Exception ex) {
            handleError(scriptEntry, ex);
        }
    };
    Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
        runner.run();
        scriptEntry.setFinished(true);
    });
}
Also used : IReplyCallback(net.dv8tion.jda.api.interactions.callbacks.IReplyCallback) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Message(net.dv8tion.jda.api.entities.Message) DiscordInteractionTag(com.denizenscript.ddiscordbot.objects.DiscordInteractionTag) ActionRow(net.dv8tion.jda.api.interactions.components.ActionRow) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) InteractionHook(net.dv8tion.jda.api.interactions.InteractionHook) DiscordEmbedTag(com.denizenscript.ddiscordbot.objects.DiscordEmbedTag) WebhookMessageAction(net.dv8tion.jda.api.requests.restaction.WebhookMessageAction) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ReplyCallbackAction(net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction) IDeferrableCallback(net.dv8tion.jda.api.interactions.callbacks.IDeferrableCallback)

Example 7 with ReplyCallbackAction

use of net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction in project Ninbot by Nincodedo.

the class PollCommandTest method executePollCommand.

@Test
void executePollCommand() {
    Member member = Mockito.mock(Member.class);
    TextChannel textChannel = Mockito.mock(TextChannel.class);
    Guild guild = Mockito.mock(Guild.class);
    ReplyCallbackAction replyAction = Mockito.mock(ReplyCallbackAction.class);
    OptionMapping choice1 = Mockito.mock(OptionMapping.class);
    OptionMapping choice2 = Mockito.mock(OptionMapping.class);
    OptionMapping questionOption = Mockito.mock(OptionMapping.class);
    when(slashCommandEvent.getChannel()).thenReturn(textChannel);
    when(slashCommandEvent.getGuild()).thenReturn(guild);
    when(slashCommandEvent.getMember()).thenReturn(member);
    when(slashCommandEvent.getOption("choice1")).thenReturn(choice1);
    when(slashCommandEvent.getOption("choice2")).thenReturn(choice2);
    when(slashCommandEvent.getOption("question")).thenReturn(questionOption);
    when(slashCommandEvent.reply(any(Message.class))).thenReturn(replyAction);
    when(choice1.getAsString()).thenReturn("1st");
    when(choice2.getAsString()).thenReturn("2nd");
    when(questionOption.getAsString()).thenReturn("why?");
    when(guild.getId()).thenReturn("1");
    when(textChannel.getId()).thenReturn("1");
    when(member.getEffectiveAvatarUrl()).thenReturn("http://google.com/a-url");
    when(guild.getLocale()).thenReturn(Locale.ENGLISH);
    var actualMessageAction = pollCommand.execute(slashCommandEvent);
    assertThat(actualMessageAction).isNotNull();
    assertThat(actualMessageAction.getReactions()).isEmpty();
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Message(net.dv8tion.jda.api.entities.Message) ReplyCallbackAction(net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction) Guild(net.dv8tion.jda.api.entities.Guild) Member(net.dv8tion.jda.api.entities.Member) Test(org.junit.jupiter.api.Test)

Aggregations

ReplyCallbackAction (net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction)7 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)5 Message (net.dv8tion.jda.api.entities.Message)4 Test (org.junit.jupiter.api.Test)3 Responses (de.lightbolt.meeting.command.Responses)2 SystemsConfig (de.lightbolt.meeting.data.config.SystemsConfig)2 MeetingManager (de.lightbolt.meeting.systems.meeting.MeetingManager)2 MeetingSubcommand (de.lightbolt.meeting.systems.meeting.MeetingSubcommand)2 MeetingRepository (de.lightbolt.meeting.systems.meeting.dao.MeetingRepository)2 Meeting (de.lightbolt.meeting.systems.meeting.model.Meeting)2 LocaleConfig (de.lightbolt.meeting.utils.localization.LocaleConfig)2 SQLException (java.sql.SQLException)2 Arrays (java.util.Arrays)2 Optional (java.util.Optional)2 DiscordEmbedTag (com.denizenscript.ddiscordbot.objects.DiscordEmbedTag)1 DiscordInteractionTag (com.denizenscript.ddiscordbot.objects.DiscordInteractionTag)1 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)1 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 IOException (java.io.IOException)1