Skip to content

Commit

Permalink
Updated Scriptable Tricks!
Browse files Browse the repository at this point in the history
  • Loading branch information
RealMangorage committed May 23, 2024
1 parent 6abf50e commit 147d488
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 93 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/mangorage/mangobot/MangoBotPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/mangorage/mangobot/modules/tricks/Trick.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -140,6 +148,10 @@ public boolean isSuppressed() {
return suppress;
}

public MemoryBank getMemoryBank() {
return memoryBank;
}

protected void use() {
timesUsed++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> TRICK_DATA_HANDLER = DataHandler.create()
public static final DataHandler<Trick> TRICK_DATA_HANDLER = DataHandler.create()
.path("data/tricksV2")
.maxDepth(3)
.build(Trick.class);
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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) {
Expand All @@ -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[]{}
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ScheduledFuture<?>> TASK = new AtomicReference<>();
Expand All @@ -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) {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mangorage.mangobot.modules.tricks.lua;

import java.util.Map;

public record MemoryBank(Map<String, Object> bank) { }

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading

0 comments on commit 147d488

Please sign in to comment.