use of net.dv8tion.jda.api.requests.restaction.WebhookMessageAction 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);
});
}
Aggregations