Skip to content

Commit

Permalink
use modern mech registration format
Browse files Browse the repository at this point in the history
also make all flaggables be adjustable, as there are flag mechs
  • Loading branch information
mcmonkey4eva committed Mar 12, 2023
1 parent dc6d609 commit 8d42379
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ public ObjectTag setPrefix(String prefix) {
return this;
}

@Override
public void applyProperty(Mechanism mechanism) {
mechanism.echoError("Cannot apply properties to Discord bots.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,68 +426,6 @@ public static void register() {
}
return result;
});
}

public static ObjectTagProcessor<DiscordChannelTag> tagProcessor = new ObjectTagProcessor<>();

@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}

String prefix = "discordchannel";

@Override
public String getPrefix() {
return prefix;
}

@Override
public String debuggable() {
if (channel != null) {
return identify() + " <GR>(" + channel.getName() + ")";
}
return identify();
}

@Override
public boolean isUnique() {
return true;
}

@Override
public String identify() {
if (bot != null) {
return "discordchannel@" + bot + "," + channel_id;
}
return "discordchannel@" + channel_id;
}

@Override
public String identifySimple() {
return identify();
}

@Override
public String toString() {
return identify();
}

@Override
public ObjectTag setPrefix(String prefix) {
if (prefix != null) {
this.prefix = prefix;
}
return this;
}

@Override
public void applyProperty(Mechanism mechanism) {
mechanism.echoError("Cannot apply properties to a DiscordChannelTag!");
}

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object DiscordChannelTag
Expand All @@ -496,18 +434,15 @@ public void adjust(Mechanism mechanism) {
// @description
// Adds the specified user to this thread.
// -->
if (mechanism.matches("add_thread_member") && mechanism.requireObject(DiscordUserTag.class)) {
Channel channel = getChannel();
tagProcessor.registerMechanism("add_thread_member", false, DiscordUserTag.class, (object, mechanism, user) -> {
Channel channel = object.getChannel();
if (!(channel instanceof ThreadChannel)) {
mechanism.echoError("Cannot adjust 'add_thread_member': this channel is not a thread.");
return;
}
DiscordUserTag user = mechanism.valueAsType(DiscordUserTag.class);
if (user.bot == null) {
user = new DiscordUserTag(bot, user.user_id);
}
user = new DiscordUserTag(object.bot, user.user_id);
((ThreadChannel) channel).addThreadMember(user.getUser()).submit();
}
});

// <--[mechanism]
// @object DiscordChannelTag
Expand All @@ -516,18 +451,15 @@ public void adjust(Mechanism mechanism) {
// @description
// Removes the specified user from this thread.
// -->
if (mechanism.matches("remove_thread_member") && mechanism.requireObject(DiscordUserTag.class)) {
Channel channel = getChannel();
tagProcessor.registerMechanism("remove_thread_member", false, DiscordUserTag.class, (object, mechanism, user) -> {
Channel channel = object.getChannel();
if (!(channel instanceof ThreadChannel)) {
mechanism.echoError("Cannot adjust 'remove_thread_member': this channel is not a thread.");
return;
}
DiscordUserTag user = mechanism.valueAsType(DiscordUserTag.class);
if (user.bot == null) {
user = new DiscordUserTag(bot, user.user_id);
}
user = new DiscordUserTag(object.bot, user.user_id);
((ThreadChannel) channel).removeThreadMember(user.getUser()).submit();
}
});

// <--[mechanism]
// @object DiscordChannelTag
Expand All @@ -538,14 +470,14 @@ public void adjust(Mechanism mechanism) {
// @tags
// <DiscordChannelTag.is_thread_archived>
// -->
if (mechanism.matches("is_thread_archived") && mechanism.requireBoolean()) {
Channel channel = getChannel();
tagProcessor.registerMechanism("is_thread_archived", false, ElementTag.class, (object, mechanism, param) -> {
Channel channel = object.getChannel();
if (!(channel instanceof ThreadChannel)) {
mechanism.echoError("Cannot adjust 'thread_archived': this channel is not a thread.");
return;
}
((ThreadChannel) channel).getManager().setArchived(mechanism.getValue().asBoolean()).submit();
}
((ThreadChannel) channel).getManager().setArchived(param.asBoolean()).submit();
});

// <--[mechanism]
// @object DiscordChannelTag
Expand All @@ -556,14 +488,14 @@ public void adjust(Mechanism mechanism) {
// @tags
// <DiscordChannelTag.is_thread_locked>
// -->
if (mechanism.matches("is_thread_locked") && mechanism.requireBoolean()) {
Channel channel = getChannel();
tagProcessor.registerMechanism("is_thread_locked", false, ElementTag.class, (object, mechanism, param) -> {
Channel channel = object.getChannel();
if (!(channel instanceof ThreadChannel)) {
mechanism.echoError("Cannot adjust 'is_thread_locked': this channel is not a thread.");
return;
}
((ThreadChannel) channel).getManager().setLocked(mechanism.getValue().asBoolean()).submit();
}
((ThreadChannel) channel).getManager().setLocked(param.asBoolean()).submit();
});

// <--[mechanism]
// @object DiscordChannelTag
Expand All @@ -572,9 +504,9 @@ public void adjust(Mechanism mechanism) {
// @description
// Deletes this channel.
// -->
if (mechanism.matches("delete")) {
getChannel().delete().complete();
}
tagProcessor.registerMechanism("delete", false, (object, mechanism) -> {
object.getChannel().delete().complete();
});

// <--[mechanism]
// @object DiscordChannelTag
Expand All @@ -583,8 +515,71 @@ public void adjust(Mechanism mechanism) {
// @description
// Renames this channel.
// -->
if (mechanism.matches("name") && mechanism.requireObject(ElementTag.class)) {
((GuildChannel) getChannel()).getManager().setName(mechanism.getValue().asString()).submit();
tagProcessor.registerMechanism("name", false, ElementTag.class, (object, mechanism, param) -> {
((GuildChannel) object.getChannel()).getManager().setName(param.asString()).submit();
});
}

public static ObjectTagProcessor<DiscordChannelTag> tagProcessor = new ObjectTagProcessor<>();

@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}

String prefix = "discordchannel";

@Override
public String getPrefix() {
return prefix;
}

@Override
public String debuggable() {
if (channel != null) {
return identify() + " <GR>(" + channel.getName() + ")";
}
return identify();
}

@Override
public boolean isUnique() {
return true;
}

@Override
public String identify() {
if (bot != null) {
return "discordchannel@" + bot + "," + channel_id;
}
return "discordchannel@" + channel_id;
}

@Override
public String identifySimple() {
return identify();
}

@Override
public String toString() {
return identify();
}

@Override
public ObjectTag setPrefix(String prefix) {
if (prefix != null) {
this.prefix = prefix;
}
return this;
}

@Override
public void applyProperty(Mechanism mechanism) {
mechanism.echoError("Cannot apply properties to a DiscordChannelTag!");
}

@Override
public void adjust(Mechanism mechanism) {
tagProcessor.processMechanism(this, mechanism);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.flags.RedirectionFlagTracker;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.Fetchable;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.MapTag;
Expand All @@ -22,7 +20,7 @@

import java.util.List;

public class DiscordCommandTag implements ObjectTag, FlaggableObject {
public class DiscordCommandTag implements ObjectTag, FlaggableObject, Adjustable {

// <--[ObjectType]
// @name DiscordCommandTag
Expand Down Expand Up @@ -292,4 +290,14 @@ public ObjectTag setPrefix(String prefix) {
}
return this;
}

@Override
public void applyProperty(Mechanism mechanism) {
mechanism.echoError("Cannot apply properties to Discord bots.");
}

@Override
public void adjust(Mechanism mechanism) {
tagProcessor.processMechanism(this, mechanism);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;
import java.util.stream.Collectors;

public class DiscordGroupTag implements ObjectTag, FlaggableObject {
public class DiscordGroupTag implements ObjectTag, FlaggableObject, Adjustable {

// <--[ObjectType]
// @name DiscordGroupTag
Expand Down Expand Up @@ -469,4 +469,14 @@ public ObjectTag setPrefix(String prefix) {
}
return this;
}

@Override
public void applyProperty(Mechanism mechanism) {
mechanism.echoError("Cannot apply properties to Discord groups.");
}

@Override
public void adjust(Mechanism mechanism) {
tagProcessor.processMechanism(this, mechanism);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.flags.SavableMapFlagTracker;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.Fetchable;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
Expand All @@ -25,7 +23,7 @@
import java.util.List;
import java.util.Map;

public class DiscordInteractionTag implements ObjectTag, FlaggableObject {
public class DiscordInteractionTag implements ObjectTag, FlaggableObject, Adjustable {

// <--[ObjectType]
// @name DiscordInteractionTag
Expand Down Expand Up @@ -271,4 +269,14 @@ public ObjectTag setPrefix(String prefix) {
}
return this;
}

@Override
public void applyProperty(Mechanism mechanism) {
mechanism.echoError("Cannot apply properties to Discord interactions.");
}

@Override
public void adjust(Mechanism mechanism) {
tagProcessor.processMechanism(this, mechanism);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,23 @@ public static void register() {
}
return result;
});

// <--[mechanism]
// @object DiscordMessageTag
// @name delete
// @input None
// @description
// Deletes the message.
// -->
tagProcessor.registerMechanism("delete", false, (object, mechanism) -> {
Message message = object.getMessage();
try {
message.delete().submit();
}
catch (Throwable ex) {
mechanism.echoError("Failed to delete message: " + ex.getClass().getCanonicalName() + ": " + ex.getMessage());
}
});
}

public static ObjectTagProcessor<DiscordMessageTag> tagProcessor = new ObjectTagProcessor<>();
Expand Down Expand Up @@ -485,30 +502,13 @@ public ObjectTag setPrefix(String prefix) {
return this;
}


@Override
public void applyProperty(Mechanism mechanism) {
mechanism.echoError("Cannot apply properties to a DiscordMessageTag!");
}

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object DiscordMessageTag
// @name delete
// @input None
// @description
// Deletes the message.
// -->
if (mechanism.matches("delete")) {
Message message = getMessage();
try {
message.delete().submit();
}
catch (Throwable ex) {
mechanism.echoError("Failed to delete message: " + ex.getClass().getCanonicalName() + ": " + ex.getMessage());
}
}
tagProcessor.processMechanism(this, mechanism);
}
}
Loading

0 comments on commit 8d42379

Please sign in to comment.