Skip to content

Commit

Permalink
DiscordEmbedTag
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 22, 2020
1 parent 737a8db commit 10b0eea
Show file tree
Hide file tree
Showing 3 changed files with 419 additions and 10 deletions.
27 changes: 23 additions & 4 deletions src/main/java/com/denizenscript/ddiscordbot/DenizenDiscordBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public void onEnable() {
ScriptEvent.registerScriptEvent(DiscordUserJoinsScriptEvent.instance = new DiscordUserJoinsScriptEvent());
ScriptEvent.registerScriptEvent(DiscordUserLeavesScriptEvent.instance = new DiscordUserLeavesScriptEvent());
ScriptEvent.registerScriptEvent(DiscordUserRoleChangeScriptEvent.instance = new DiscordUserRoleChangeScriptEvent());
ObjectFetcher.registerWithObjectFetcher(DiscordChannelTag.class, DiscordChannelTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordBotTag.class, DiscordBotTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordChannelTag.class, DiscordChannelTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordEmbedTag.class, DiscordEmbedTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordGroupTag.class, DiscordGroupTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordRoleTag.class, DiscordRoleTag.tagProcessor);
ObjectFetcher.registerWithObjectFetcher(DiscordUserTag.class, DiscordUserTag.tagProcessor);
Expand All @@ -45,23 +46,41 @@ public void run(ReplaceableTagEvent event) {
discordTagBase(event);
}
}, "discord");
TagManager.registerTagHandler(new TagRunnable.RootForm() {
@Override
public void run(ReplaceableTagEvent event) {
discordEmbedTagBase(event);
}
}, "discord_embed");
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}

public void discordEmbedTagBase(ReplaceableTagEvent event) {
if (!event.matches("discord_embed") || event.replaced()) {
return;
}
DiscordEmbedTag embed;
if (event.hasNameContext()) {
embed = DiscordEmbedTag.valueOf(event.getNameContext(), event.getAttributes().context);
}
else {
embed = new DiscordEmbedTag();
}
Attribute attribute = event.getAttributes().fulfill(1);
event.setReplacedObject(CoreUtilities.autoAttrib(embed, attribute));
}

public void discordTagBase(ReplaceableTagEvent event) {
if (!event.matches("discord") || event.replaced()) {
return;
}

DiscordBotTag bot = null;

if (event.hasNameContext()) {
bot = DiscordBotTag.valueOf(event.getNameContext(), event.getAttributes().context);
}

Attribute attribute = event.getAttributes().fulfill(1);

// <--[tag]
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/com/denizenscript/ddiscordbot/DiscordCommand.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.denizenscript.ddiscordbot;

import com.denizenscript.ddiscordbot.objects.DiscordChannelTag;
import com.denizenscript.ddiscordbot.objects.DiscordGroupTag;
import com.denizenscript.ddiscordbot.objects.DiscordRoleTag;
import com.denizenscript.ddiscordbot.objects.DiscordUserTag;
import com.denizenscript.ddiscordbot.objects.*;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
Expand Down Expand Up @@ -72,6 +69,10 @@ public DiscordCommand() {
// - ~discord id:mybot message channel:<discord[mybot].group[Denizen].channel[bot-spam]> "Hello world!"
//
// @Usage
// Use to message an embed to a Discord channel.
// - ~discord id:mybot message channel:<discord[mybot].group[Denizen].channel[bot-spam]> "<discord_embed.with[title].as[hi].with[description].as[This is an embed!]>"
//
// @Usage
// Use to message a Discord channel and record the ID.
// - ~discord id:mybot message channel:<discord[mybot].group[Denizen].channel[bot-spam]> "Hello world!" save:sent
// - announce "Sent as <entry[sent].message_id>"
Expand Down Expand Up @@ -344,7 +345,14 @@ public void execute(ScriptEntry scriptEntry) {
else {
textChan = client.getTextChannelById(channel.channel_id);
}
Message sentMessage = textChan.sendMessage(message.asString()).complete();
Message sentMessage;
if (message.asString().startsWith("discordembed@")) {
MessageEmbed embed = DiscordEmbedTag.valueOf(message.asString(), scriptEntry.context).build(scriptEntry.context).build();
sentMessage = textChan.sendMessage(embed).complete();
}
else {
sentMessage = textChan.sendMessage(message.asString()).complete();
}
scriptEntry.addObject("message_id", new ElementTag(sentMessage.getId()));
scriptEntry.setFinished(true);
break;
Expand Down Expand Up @@ -385,7 +393,14 @@ public void execute(ScriptEntry scriptEntry) {
if (requireClientObject.apply(client)) {
return;
}
client.getTextChannelById(channel.channel_id).editMessageById(messageId.asLong(), message.asString()).complete();
MessageChannel textChannel = client.getTextChannelById(channel.channel_id);
if (message.asString().startsWith("discordembed@")) {
MessageEmbed embed = DiscordEmbedTag.valueOf(message.asString(), scriptEntry.context).build(scriptEntry.context).build();
textChannel.editMessageById(messageId.asLong(), embed).complete();
}
else {
textChannel.editMessageById(messageId.asLong(), message.asString()).complete();
}
scriptEntry.setFinished(true);
break;
}
Expand Down
Loading

0 comments on commit 10b0eea

Please sign in to comment.