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);
});
}
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();
}
Aggregations