From 147d488bb0576af4aca48af43d6749edf9963d31 Mon Sep 17 00:00:00 2001 From: RealMangoRage <64402114+RealMangorage@users.noreply.github.com> Date: Wed, 22 May 2024 19:09:13 -0700 Subject: [PATCH] Updated Scriptable Tricks! --- .../mangorage/mangobot/MangoBotPlugin.java | 3 + .../mangobot/modules/tricks/Trick.java | 12 +++ .../mangobot/modules/tricks/TrickCommand.java | 25 +++++-- .../modules/tricks/TrickScriptable.java | 10 +-- .../modules/tricks/lua/ILuaAction.java | 7 -- .../modules/tricks/lua/LuaActions.java | 29 -------- .../mangobot/modules/tricks/lua/LuaJDA.java | 61 ++++++++++++++++ .../tricks/lua/LuaPrimitiveStringArray.java | 17 ----- .../modules/tricks/lua/MemoryBank.java | 5 ++ .../modules/tricks/lua/MessageAction.java | 28 ------- .../tricks/lua/builders/LuaEmbedBuilder.java | 31 ++++++++ .../builders/LuaMessageResponseBuilder.java | 73 +++++++++++++++++++ .../tricks/lua/helpers/LuaInfoHelper.java | 22 ++++++ .../tricks/lua/helpers/LuaObjectHelper.java | 22 ++++++ .../tricks/lua/objects/LuaStringArray.java | 22 ++++++ 15 files changed, 274 insertions(+), 93 deletions(-) delete mode 100644 src/main/java/org/mangorage/mangobot/modules/tricks/lua/ILuaAction.java delete mode 100644 src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaActions.java create mode 100644 src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaJDA.java delete mode 100644 src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaPrimitiveStringArray.java create mode 100644 src/main/java/org/mangorage/mangobot/modules/tricks/lua/MemoryBank.java delete mode 100644 src/main/java/org/mangorage/mangobot/modules/tricks/lua/MessageAction.java create mode 100644 src/main/java/org/mangorage/mangobot/modules/tricks/lua/builders/LuaEmbedBuilder.java create mode 100644 src/main/java/org/mangorage/mangobot/modules/tricks/lua/builders/LuaMessageResponseBuilder.java create mode 100644 src/main/java/org/mangorage/mangobot/modules/tricks/lua/helpers/LuaInfoHelper.java create mode 100644 src/main/java/org/mangorage/mangobot/modules/tricks/lua/helpers/LuaObjectHelper.java create mode 100644 src/main/java/org/mangorage/mangobot/modules/tricks/lua/objects/LuaStringArray.java diff --git a/src/main/java/org/mangorage/mangobot/MangoBotPlugin.java b/src/main/java/org/mangorage/mangobot/MangoBotPlugin.java index 7652414..641bf66 100644 --- a/src/main/java/org/mangorage/mangobot/MangoBotPlugin.java +++ b/src/main/java/org/mangorage/mangobot/MangoBotPlugin.java @@ -34,6 +34,9 @@ import java.util.EnumSet; import java.util.List; import java.util.Scanner; +import java.util.Timer; +import java.util.TimerTask; + import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.mangorage.basicutils.config.Config; diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/Trick.java b/src/main/java/org/mangorage/mangobot/modules/tricks/Trick.java index ffb1a19..71ec21b 100644 --- a/src/main/java/org/mangorage/mangobot/modules/tricks/Trick.java +++ b/src/main/java/org/mangorage/mangobot/modules/tricks/Trick.java @@ -1,9 +1,12 @@ package org.mangorage.mangobot.modules.tricks; import com.google.gson.annotations.Expose; +import org.mangorage.mangobot.modules.tricks.lua.MemoryBank; import org.mangorage.mangobotapi.core.data.FileName; import org.mangorage.mangobotapi.core.data.IFileNameResolver; +import java.util.HashMap; + public class Trick implements IFileNameResolver { @Expose private long ownerID; @@ -47,10 +50,15 @@ public class Trick implements IFileNameResolver { @Expose private TrickType type; + @Expose + private MemoryBank memoryBank; + protected Trick(String trickID, long guildID) { this.trickID = trickID; this.guildID = guildID; this.created = System.currentTimeMillis(); + if (getType() == TrickType.SCRIPT) + memoryBank = new MemoryBank(new HashMap<>()); } protected void setAliasTarget(String target) { @@ -140,6 +148,10 @@ public boolean isSuppressed() { return suppress; } + public MemoryBank getMemoryBank() { + return memoryBank; + } + protected void use() { timesUsed++; } diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/TrickCommand.java b/src/main/java/org/mangorage/mangobot/modules/tricks/TrickCommand.java index a611ecb..2c8caa3 100644 --- a/src/main/java/org/mangorage/mangobot/modules/tricks/TrickCommand.java +++ b/src/main/java/org/mangorage/mangobot/modules/tricks/TrickCommand.java @@ -43,7 +43,7 @@ public class TrickCommand implements IBasicCommand { private static final boolean ALLOW_SCRIPT_TRICKS = true; private final CorePlugin plugin; - private final DataHandler TRICK_DATA_HANDLER = DataHandler.create() + public static final DataHandler TRICK_DATA_HANDLER = DataHandler.create() .path("data/tricksV2") .maxDepth(3) .build(Trick.class); @@ -426,11 +426,13 @@ public CommandResult execute(Message message, Arguments args) { if (trick.getType() == TrickType.NORMAL) { - details = details + "Content: \n" + trick.getContent(); + if (trick.getContent().length() < 2000) + details = details + "Content: \n" + trick.getContent(); } else if (trick.getType() == TrickType.ALIAS) { details = details + "Alias -> " + trick.getAliasTarget(); } else if (trick.getType() == TrickType.SCRIPT) { - details = details + "Script: \n" + trick.getScript(); + if (trick.getScript().length() < 2000) + details = details + "Script: \n" + trick.getScript(); } dMessage.apply(message.reply(details)) @@ -519,9 +521,17 @@ private void useTrick(Trick trick, Message message, MessageChannel channel, long var type = trick.getType(); if (type == TrickType.NORMAL) { dMessage.withButton( - dMessage.apply(channel.sendMessage(trick.getContent())).setSuppressEmbeds(trick.isSuppressed()), - MangoBotPlugin.ACTION_REGISTRY.get(TrashButtonAction.class).createForUser(message.getAuthor()) - ).queue(); + dMessage.apply(channel.sendMessage(trick.getContent())) + .setSuppressEmbeds(trick.isSuppressed()), MangoBotPlugin.ACTION_REGISTRY.get(TrashButtonAction.class).createForUser(message.getAuthor()) + ).setAllowedMentions( + Arrays.stream(Message.MentionType.values()) + .filter(t -> { + if (t == Message.MentionType.EVERYONE) return false; + if (t == Message.MentionType.HERE) return false; + return true; + }) + .toList() + ).queue(); trick.use(); save(trick); } else if (type == TrickType.ALIAS) { @@ -539,10 +549,11 @@ private void useTrick(Trick trick, Message message, MessageChannel channel, long trick.use(); var script = MarkdownSanitizer.sanitize(trick.getScript()); SCRIPT_RUNNER.execute( + trick, script, message, channel, - args.getArgs().length > 1 ? args.getFrom(2).split(" ") : new String[]{} + args.getArgs().length > 0 ? args.getFrom(2).split(" ") : new String[]{} ); } } diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/TrickScriptable.java b/src/main/java/org/mangorage/mangobot/modules/tricks/TrickScriptable.java index dad6295..3e8a0fa 100644 --- a/src/main/java/org/mangorage/mangobot/modules/tricks/TrickScriptable.java +++ b/src/main/java/org/mangorage/mangobot/modules/tricks/TrickScriptable.java @@ -33,8 +33,8 @@ import org.luaj.vm2.lib.jse.CoerceJavaToLua; import org.luaj.vm2.lib.jse.JseBaseLib; import org.luaj.vm2.lib.jse.JseMathLib; -import org.mangorage.mangobot.modules.tricks.lua.LuaActions; -import org.mangorage.mangobot.modules.tricks.lua.LuaPrimitiveStringArray; +import org.mangorage.mangobot.modules.tricks.lua.LuaJDA; +import org.mangorage.mangobot.modules.tricks.lua.objects.LuaStringArray; import org.mangorage.mangobotapi.core.plugin.api.CorePlugin; import java.util.concurrent.Executors; @@ -76,7 +76,7 @@ public Globals sandBoxedGlobals() { return server_globals; } - public void execute(String script, Message message, MessageChannel messageChannel, String[] args) { + public void execute(Trick trick, String script, Message message, MessageChannel messageChannel, String[] args) { // Create a ScheduledExecutorService ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); AtomicReference> TASK = new AtomicReference<>(); @@ -97,8 +97,8 @@ public void execute(String script, Message message, MessageChannel messageChanne if (!method.isnil()) { method.call( - CoerceJavaToLua.coerce(new LuaActions(plugin, message, messageChannel)), - CoerceJavaToLua.coerce(new LuaPrimitiveStringArray(args)) + CoerceJavaToLua.coerce(new LuaJDA(plugin, trick, message, messageChannel)), + CoerceJavaToLua.coerce(new LuaStringArray(args)) ); } if (TASK.get() != null) { diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/ILuaAction.java b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/ILuaAction.java deleted file mode 100644 index 12d96f2..0000000 --- a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/ILuaAction.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.mangorage.mangobot.modules.tricks.lua; - -import org.mangorage.mangobotapi.core.util.MessageSettings; - -public interface ILuaAction { - void submit(MessageSettings settings); -} diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaActions.java b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaActions.java deleted file mode 100644 index b55b544..0000000 --- a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaActions.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.mangorage.mangobot.modules.tricks.lua; - -import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; -import org.mangorage.mangobotapi.core.plugin.api.CorePlugin; - -import java.util.ArrayList; -import java.util.List; - -public final class LuaActions { - private final CorePlugin corePlugin; - private final List actions = new ArrayList<>(); - private final Message message; - private final MessageChannel messageChannel; - - public LuaActions(CorePlugin plugin, Message message, MessageChannel channel) { - this.corePlugin = plugin; - this.message = message; - this.messageChannel = channel; - } - - public void reply(String s) { - actions.add(new MessageAction(s, true, message)); - } - - public void finish() { - actions.forEach(a -> a.submit(corePlugin.getMessageSettings())); - } -} diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaJDA.java b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaJDA.java new file mode 100644 index 0000000..f0e055f --- /dev/null +++ b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaJDA.java @@ -0,0 +1,61 @@ +package org.mangorage.mangobot.modules.tricks.lua; + +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import org.mangorage.mangobot.modules.tricks.Trick; +import org.mangorage.mangobot.modules.tricks.TrickCommand; +import org.mangorage.mangobot.modules.tricks.lua.builders.LuaEmbedBuilder; +import org.mangorage.mangobot.modules.tricks.lua.builders.LuaMessageResponseBuilder; +import org.mangorage.mangobot.modules.tricks.lua.helpers.LuaInfoHelper; +import org.mangorage.mangobot.modules.tricks.lua.helpers.LuaObjectHelper; +import org.mangorage.mangobotapi.core.plugin.api.CorePlugin; + + +public final class LuaJDA { + private final CorePlugin corePlugin; + private final Message message; + private final MessageChannel messageChannel; + private final Trick trick; + + + public LuaJDA(CorePlugin plugin, Trick trick, Message message, MessageChannel channel) { + this.corePlugin = plugin; + this.message = message; + this.messageChannel = channel; + this.trick = trick; + } + + public Object getStored(String key) { + return trick.getMemoryBank().bank().get(key); + } + + public Object getStoredOrSetAndGet(String key, Object value) { + var result = getStored(key); + if (result == null) { + storeValue(key, value); + return value; + } + return result; + } + + public void storeValue(String key, Object o) { + trick.getMemoryBank().bank().put(key, o); + TrickCommand.TRICK_DATA_HANDLER.save(corePlugin.getPluginDirectory(), trick); + } + + public LuaEmbedBuilder createEmbed() { + return new LuaEmbedBuilder(); + } + + public LuaMessageResponseBuilder respond() { + return new LuaMessageResponseBuilder(message.reply("")); + } + + public LuaInfoHelper getInfoHelper() { + return new LuaInfoHelper(message, messageChannel); + } + + public LuaObjectHelper getObjectHelper() { + return new LuaObjectHelper(); + } +} diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaPrimitiveStringArray.java b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaPrimitiveStringArray.java deleted file mode 100644 index 783b232..0000000 --- a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/LuaPrimitiveStringArray.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.mangorage.mangobot.modules.tricks.lua; - -public class LuaPrimitiveStringArray { - private final String[] array; - - public LuaPrimitiveStringArray(String... array) { - this.array = array; - } - - public String get(int index) { - return array[index]; - } - - public int getSize() { - return array.length; - } -} diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/MemoryBank.java b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/MemoryBank.java new file mode 100644 index 0000000..9c0688a --- /dev/null +++ b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/MemoryBank.java @@ -0,0 +1,5 @@ +package org.mangorage.mangobot.modules.tricks.lua; + +import java.util.Map; + +public record MemoryBank(Map bank) { } diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/MessageAction.java b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/MessageAction.java deleted file mode 100644 index 3f241eb..0000000 --- a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/MessageAction.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.mangorage.mangobot.modules.tricks.lua; - -import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.channel.Channel; -import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; -import org.mangorage.mangobot.MangoBotPlugin; -import org.mangorage.mangobotapi.core.util.MessageSettings; - -public final class MessageAction implements ILuaAction { - private final String string; - private final boolean isReply; - private final Object instance; - - public MessageAction(String s, boolean reply, Object instance) { - this.string = s; - this.isReply = reply; - this.instance = instance; - } - - @Override - public void submit(MessageSettings settings) { - if (isReply && instance instanceof Message message) { - settings.apply(message.reply(string)).queue(); - } else if (!isReply && instance instanceof MessageChannel channel) { - settings.apply(channel.sendMessage(string)).queue(); - } - } -} diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/builders/LuaEmbedBuilder.java b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/builders/LuaEmbedBuilder.java new file mode 100644 index 0000000..9680dcb --- /dev/null +++ b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/builders/LuaEmbedBuilder.java @@ -0,0 +1,31 @@ +package org.mangorage.mangobot.modules.tricks.lua.builders; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.MessageEmbed; + +public class LuaEmbedBuilder { + private final EmbedBuilder builder = new EmbedBuilder(); + + public LuaEmbedBuilder setTitle(String title) { + builder.setTitle(title); + return this; + } + + public LuaEmbedBuilder setTitle(String title, String url) { + builder.setTitle(title, url); + return this; + } + + public LuaEmbedBuilder setDescription(String description) { + builder.setDescription(description); + return this; + } + + public EmbedBuilder getBuilder() { + return builder; + } + + public MessageEmbed build() { + return builder.build(); + } +} diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/builders/LuaMessageResponseBuilder.java b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/builders/LuaMessageResponseBuilder.java new file mode 100644 index 0000000..28931a2 --- /dev/null +++ b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/builders/LuaMessageResponseBuilder.java @@ -0,0 +1,73 @@ +package org.mangorage.mangobot.modules.tricks.lua.builders; + +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; + +import java.util.Arrays; +import java.util.List; + +public class LuaMessageResponseBuilder { + private final MessageCreateAction message; + public LuaMessageResponseBuilder(MessageCreateAction message) { + this.message = message; + } + + public LuaMessageResponseBuilder reply(String content) { + message.setContent(content); + return this; + } + + public LuaMessageResponseBuilder setMentions(String name) { + String[] names = name.split(":"); + if (names.length == 0 || names[0].isEmpty()) { + message.setAllowedMentions(List.of()); + } else { + message.setAllowedMentions( + Arrays.stream(names) + .map(Message.MentionType::valueOf) + .filter(t -> { + if (t == Message.MentionType.EVERYONE) return false; + if (t == Message.MentionType.HERE) return false; + return true; + }) + .toList() + ); + } + return this; + } + + public LuaMessageResponseBuilder setSuppressedNotifications(boolean value) { + message.setSuppressedNotifications(value); + return this; + } + + public LuaMessageResponseBuilder setMentionsUser(boolean mentionsUser) { + message.mentionRepliedUser(mentionsUser); + return this; + } + + public LuaMessageResponseBuilder setMention(String user) { + String[] users = user.split(":"); + message.mentionUsers(users); + return this; + } + + public LuaMessageResponseBuilder setEmbed(MessageEmbed embed) { + message.setEmbeds(embed); + return this; + } + + public void queue() { + message.setAllowedMentions( + message.getAllowedMentions() + .stream() + .filter(t -> { + if (t == Message.MentionType.EVERYONE) return false; + if (t == Message.MentionType.HERE) return false; + return true; + }) + .toList() + ).queue(); + } +} diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/helpers/LuaInfoHelper.java b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/helpers/LuaInfoHelper.java new file mode 100644 index 0000000..16c4467 --- /dev/null +++ b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/helpers/LuaInfoHelper.java @@ -0,0 +1,22 @@ +package org.mangorage.mangobot.modules.tricks.lua.helpers; + +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; + +public class LuaInfoHelper { + private final Message message; + private final MessageChannel channel; + + public LuaInfoHelper(Message message, MessageChannel channel) { + this.message = message; + this.channel = channel; + } + + public long getAuthorId() { + return message.getAuthor().getIdLong(); + } + + public String getAuthorIdString() { + return message.getAuthor().getId(); + } +} diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/helpers/LuaObjectHelper.java b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/helpers/LuaObjectHelper.java new file mode 100644 index 0000000..2140d15 --- /dev/null +++ b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/helpers/LuaObjectHelper.java @@ -0,0 +1,22 @@ +package org.mangorage.mangobot.modules.tricks.lua.helpers; + +import java.util.Objects; + +public class LuaObjectHelper { + public Integer getInteger(String value) { + try { + return Integer.parseInt(value); + } catch (Exception e) { + + } + return null; + } + + public boolean isInteger(String value) { + return getInteger(value) != null; + } + + public boolean isSameNumber(Number number, Number number2) { + return Objects.equals(number, number2); + } +} diff --git a/src/main/java/org/mangorage/mangobot/modules/tricks/lua/objects/LuaStringArray.java b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/objects/LuaStringArray.java new file mode 100644 index 0000000..db5c6f1 --- /dev/null +++ b/src/main/java/org/mangorage/mangobot/modules/tricks/lua/objects/LuaStringArray.java @@ -0,0 +1,22 @@ +package org.mangorage.mangobot.modules.tricks.lua.objects; + +public class LuaStringArray { + private final String[] strings; + + public LuaStringArray(String[] strings) { + this.strings = strings; + } + + public int getSize() { + return strings.length; + } + + public String getEntry(int index) { + return strings[index]; + } + + public boolean isEmpty() { + if (strings.length == 0) return true; + return strings.length == 1 && strings[0].isEmpty(); + } +}