Search in sources :

Example 1 with DiscordMessageTag

use of com.denizenscript.ddiscordbot.objects.DiscordMessageTag in project dDiscordBot by DenizenScript.

the class DiscordMessageModifiedScriptEvent method getContext.

@Override
public ObjectTag getContext(String name) {
    switch(name) {
        case "channel":
            return new DiscordChannelTag(botID, getEvent().getChannel());
        case "group":
            if (getEvent().isFromGuild()) {
                return new DiscordGroupTag(botID, getEvent().getGuild());
            }
            break;
        case "new_message":
            return new DiscordMessageTag(botID, getEvent().getMessage());
        case "old_message_valid":
            return new ElementTag(oldMessage != null);
        case "old_message":
            return oldMessage == null ? null : new DiscordMessageTag(botID, oldMessage);
        case "old_no_mention_message":
        case "old_formatted_message":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return null;
        case "message":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new ElementTag(getEvent().getMessage().getContentRaw());
        case "message_id":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new ElementTag(getEvent().getMessage().getId());
        case "no_mention_message":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new ElementTag(DiscordMessageTag.stripMentions(getEvent().getMessage().getContentRaw()));
        case "formatted_message":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new ElementTag(getEvent().getMessage().getContentDisplay());
        case "author":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new DiscordUserTag(botID, getEvent().getMessage().getAuthor());
        case "mentions":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            ListTag list = new ListTag();
            for (User user : getEvent().getMessage().getMentionedUsers()) {
                list.addObject(new DiscordUserTag(botID, user));
            }
            return list;
        case "is_direct":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new ElementTag(getEvent().getChannel() instanceof PrivateChannel);
    }
    return super.getContext(name);
}
Also used : PrivateChannel(net.dv8tion.jda.api.entities.PrivateChannel) DiscordUserTag(com.denizenscript.ddiscordbot.objects.DiscordUserTag) User(net.dv8tion.jda.api.entities.User) DiscordChannelTag(com.denizenscript.ddiscordbot.objects.DiscordChannelTag) DiscordGroupTag(com.denizenscript.ddiscordbot.objects.DiscordGroupTag) DiscordMessageTag(com.denizenscript.ddiscordbot.objects.DiscordMessageTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 2 with DiscordMessageTag

use of com.denizenscript.ddiscordbot.objects.DiscordMessageTag in project dDiscordBot by DenizenScript.

the class DiscordCreateThreadCommand method execute.

// <--[command]
// @Name discordcreatethread
// @Syntax discordcreatethread [id:<id>] [name:<name>] [message:<message>/parent:<channel> (private)]
// @Required 3
// @Maximum 4
// @Short Creates a new Discord thread.
// @Plugin dDiscordBot
// @Guide https://guide.denizenscript.com/guides/expanding/ddiscordbot.html
// @Group external
// 
// @Description
// Creates a new Discord thread.
// 
// You must specify the bot object ID, and the thread name.
// 
// You can either specify a full DiscordMessageTag instance to create a thread based on that message,
// OR specify a DiscordChannelTag parent and optionally mark it private (otherwise it's public).
// 
// @Tags
// <entry[saveName].created_thread> returns the newly created thread.
// 
// @Usage
// Use to create a new thread for a specific message and send a bot message to it.
// - ~discordcreatethread id:mybot "name:The bot's awesome thread" message:<context.message> save:thread
// - ~discordmessage id:mybot channel:<entry[thread].created_thread> "Here I made this thread just for you"
// 
// @Usage
// Use to create a new thread in a channel and link to it in some channel.
// - ~discordcreatethread id:mybot "name:The bot's awesome thread" parent:<[some_channel]> save:thread
// - ~discordmessage id:mybot channel:<[some_channel]> "I created thread <&lt>#<entry[thread].created_thread.id><&gt>!"
// 
// -->
@Override
public void execute(ScriptEntry scriptEntry) {
    DiscordBotTag bot = scriptEntry.requiredArgForPrefix("id", DiscordBotTag.class);
    ElementTag name = scriptEntry.requiredArgForPrefixAsElement("name");
    DiscordMessageTag message = scriptEntry.argForPrefix("message", DiscordMessageTag.class, true);
    DiscordChannelTag channel = scriptEntry.argForPrefix("parent", DiscordChannelTag.class, true);
    boolean isPrivate = scriptEntry.argAsBoolean("private");
    if (message != null) {
        if (channel != null || isPrivate) {
            throw new InvalidArgumentsRuntimeException("Cannot have both a 'message:' and channel/private");
        }
        if (message.channel_id == 0) {
            throw new InvalidArgumentsRuntimeException("DiscordMessageTag not fully formed - missing channel ID.");
        }
    } else if (channel == null) {
        throw new InvalidArgumentsRuntimeException("Missing message or channel argument!");
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), bot, name, message, channel, isPrivate ? db("private", true) : "");
    }
    Runnable runner = () -> {
        if (message != null) {
            DiscordMessageTag forMessage = message;
            if (forMessage.bot == null || !forMessage.bot.equals(bot.bot)) {
                forMessage = forMessage.duplicate();
                forMessage.bot = bot.bot;
            }
            Message actualMessage = forMessage.getMessage();
            if (actualMessage == null) {
                handleError(scriptEntry, "Invalid message reference.");
                return;
            }
            if (!(actualMessage.getChannel() instanceof GuildChannel)) {
                handleError(scriptEntry, "Message referenced is not in a group (can't create threads in a DM).");
                return;
            }
            if (actualMessage.getChannel() instanceof ThreadChannel) {
                handleError(scriptEntry, "Message referenced is in a thread - you can't have threads inside threads.");
                return;
            }
            try {
                ThreadChannel created = ((TextChannel) actualMessage.getChannel()).createThreadChannel(name.asString(), actualMessage.getIdLong()).complete();
                if (created != null) {
                    scriptEntry.addObject("created_thread", new DiscordChannelTag(bot.bot, created));
                }
            } catch (Throwable ex) {
                handleError(scriptEntry, ex);
            }
        } else {
            DiscordChannelTag forChannel = channel;
            if (forChannel.bot == null || !forChannel.bot.equals(bot.bot)) {
                forChannel = forChannel.duplicate();
                forChannel.bot = bot.bot;
            }
            Channel actualChannel = forChannel.getChannel();
            if (actualChannel == null) {
                handleError(scriptEntry, "Invalid channel reference.");
                return;
            }
            if (!(actualChannel instanceof GuildChannel)) {
                handleError(scriptEntry, "Channel referenced is not in a group (can't create threads in a DM).");
                return;
            }
            if (!(actualChannel instanceof TextChannel)) {
                handleError(scriptEntry, "Channel referenced is not a text channel (can't create threads in a voice channel).");
                return;
            }
            if (actualChannel instanceof ThreadChannel) {
                handleError(scriptEntry, "Channel referenced is a thread - you can't have threads inside threads.");
                return;
            }
            try {
                ThreadChannel created = ((TextChannel) actualChannel).createThreadChannel(name.asString(), isPrivate).complete();
                if (created != null) {
                    scriptEntry.addObject("created_thread", new DiscordChannelTag(bot.bot, created));
                }
            } catch (Throwable ex) {
                handleError(scriptEntry, ex);
            }
        }
    };
    Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
        runner.run();
        scriptEntry.setFinished(true);
    });
}
Also used : DiscordChannelTag(com.denizenscript.ddiscordbot.objects.DiscordChannelTag) InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) DiscordMessageTag(com.denizenscript.ddiscordbot.objects.DiscordMessageTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DiscordBotTag(com.denizenscript.ddiscordbot.objects.DiscordBotTag)

Example 3 with DiscordMessageTag

use of com.denizenscript.ddiscordbot.objects.DiscordMessageTag in project dDiscordBot by DenizenScript.

the class DiscordMessageReceivedScriptEvent method getContext.

@Override
public ObjectTag getContext(String name) {
    switch(name) {
        case "channel":
            return new DiscordChannelTag(botID, getEvent().getChannel());
        case "group":
            if (getEvent().isFromGuild()) {
                return new DiscordGroupTag(botID, getEvent().getGuild());
            }
            break;
        case "new_message":
            return new DiscordMessageTag(botID, getEvent().getMessage());
        case "message":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new ElementTag(getEvent().getMessage().getContentRaw());
        case "message_id":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new ElementTag(getEvent().getMessage().getId());
        case "no_mention_message":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new ElementTag(DiscordMessageTag.stripMentions(getEvent().getMessage().getContentRaw()));
        case "formatted_message":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new ElementTag(getEvent().getMessage().getContentDisplay());
        case "author":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new DiscordUserTag(botID, getEvent().getMessage().getAuthor());
        case "mentions":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            ListTag list = new ListTag();
            for (User user : getEvent().getMessage().getMentionedUsers()) {
                list.addObject(new DiscordUserTag(botID, user));
            }
            return list;
        case "is_direct":
            DenizenDiscordBot.oldMessageContexts.warn((TagContext) null);
            return new ElementTag(getEvent().getChannel() instanceof PrivateChannel);
    }
    return super.getContext(name);
}
Also used : PrivateChannel(net.dv8tion.jda.api.entities.PrivateChannel) DiscordUserTag(com.denizenscript.ddiscordbot.objects.DiscordUserTag) User(net.dv8tion.jda.api.entities.User) DiscordChannelTag(com.denizenscript.ddiscordbot.objects.DiscordChannelTag) DiscordGroupTag(com.denizenscript.ddiscordbot.objects.DiscordGroupTag) DiscordMessageTag(com.denizenscript.ddiscordbot.objects.DiscordMessageTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Aggregations

DiscordChannelTag (com.denizenscript.ddiscordbot.objects.DiscordChannelTag)3 DiscordMessageTag (com.denizenscript.ddiscordbot.objects.DiscordMessageTag)3 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)3 DiscordGroupTag (com.denizenscript.ddiscordbot.objects.DiscordGroupTag)2 DiscordUserTag (com.denizenscript.ddiscordbot.objects.DiscordUserTag)2 ListTag (com.denizenscript.denizencore.objects.core.ListTag)2 PrivateChannel (net.dv8tion.jda.api.entities.PrivateChannel)2 User (net.dv8tion.jda.api.entities.User)2 DiscordBotTag (com.denizenscript.ddiscordbot.objects.DiscordBotTag)1 InvalidArgumentsRuntimeException (com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException)1