Search in sources :

Example 1 with IDeferrableCallback

use of net.dv8tion.jda.api.interactions.callbacks.IDeferrableCallback 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)

Aggregations

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 Message (net.dv8tion.jda.api.entities.Message)1 MessageEmbed (net.dv8tion.jda.api.entities.MessageEmbed)1 InteractionHook (net.dv8tion.jda.api.interactions.InteractionHook)1 IDeferrableCallback (net.dv8tion.jda.api.interactions.callbacks.IDeferrableCallback)1 IReplyCallback (net.dv8tion.jda.api.interactions.callbacks.IReplyCallback)1 ActionRow (net.dv8tion.jda.api.interactions.components.ActionRow)1 WebhookMessageAction (net.dv8tion.jda.api.requests.restaction.WebhookMessageAction)1 ReplyCallbackAction (net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction)1