Skip to content

Commit

Permalink
more thread tools and events
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 3, 2022
1 parent 836c73d commit 70a5e61
Show file tree
Hide file tree
Showing 20 changed files with 395 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ public void onEnable() {
DenizenCore.commandRegistry.registerCommand(DiscordReactCommand.class);
// Events
ScriptEvent.registerScriptEvent(DiscordButtonClickedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordChannelCreateScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordChannelDeleteScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordMessageDeletedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordMessageModifiedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordMessageReactionAddScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordMessageReactionRemoveScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordMessageReceivedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordSelectionUsedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordSlashCommandScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordThreadArchivedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordThreadRevealedScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordUserJoinsScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordUserLeavesScriptEvent.class);
ScriptEvent.registerScriptEvent(DiscordUserNicknameChangeScriptEvent.class);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/denizenscript/ddiscordbot/DiscordConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;
import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent;
Expand All @@ -20,9 +22,12 @@
import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent;
import net.dv8tion.jda.api.events.thread.ThreadHiddenEvent;
import net.dv8tion.jda.api.events.thread.ThreadRevealedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.bukkit.Bukkit;

import javax.annotation.Nonnull;
import java.util.function.Consumer;

public class DiscordConnection extends ListenerAdapter {
Expand Down Expand Up @@ -136,6 +141,26 @@ public void onSelectionMenu(SelectionMenuEvent event) {
autoHandle(event, DiscordSelectionUsedScriptEvent.instance);
}

@Override
public void onChannelCreate(@Nonnull ChannelCreateEvent event) {
autoHandle(event, DiscordChannelCreateScriptEvent.instance);
}

@Override
public void onChannelDelete(@Nonnull ChannelDeleteEvent event) {
autoHandle(event, DiscordChannelDeleteScriptEvent.instance);
}

@Override
public void onThreadRevealed(@Nonnull ThreadRevealedEvent event) {
autoHandle(event, DiscordThreadRevealedScriptEvent.instance);
}

@Override
public void onThreadHidden(@Nonnull ThreadHiddenEvent event) {
autoHandle(event, DiscordThreadArchivedScriptEvent.instance);
}

public void autoHandle(Event event, DiscordScriptEvent scriptEvent) {
autoHandle(event, scriptEvent, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import com.denizenscript.ddiscordbot.objects.DiscordBotTag;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.ObjectTag;
import net.dv8tion.jda.api.entities.Channel;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.events.Event;

public abstract class DiscordScriptEvent extends BukkitScriptEvent {

public DiscordScriptEvent() {
registerSwitches("for");
}

public String botID;

public Event event;
Expand Down Expand Up @@ -49,8 +53,12 @@ public void destroy() {
enabled = false;
}

public static boolean tryChannel(ScriptPath path, MessageChannel channel) {
String text = path.switches.get("channel");
public static boolean tryChannel(ScriptPath path, Channel channel) {
return tryChannel(path, channel, "channel");
}

public static boolean tryChannel(ScriptPath path, Channel channel, String switchName) {
String text = path.switches.get(switchName);
if (text == null) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DiscordButtonClickedScriptEvent extends DiscordScriptEvent {
public DiscordButtonClickedScriptEvent() {
instance = this;
registerCouldMatcher("discord button clicked");
registerSwitches("for", "channel", "group", "id");
registerSwitches("channel", "group", "id");
}

public ButtonClickEvent getEvent() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.denizenscript.ddiscordbot.events;

import com.denizenscript.ddiscordbot.DiscordScriptEvent;
import com.denizenscript.ddiscordbot.objects.DiscordChannelTag;
import com.denizenscript.ddiscordbot.objects.DiscordGroupTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;

public class DiscordChannelCreateScriptEvent extends DiscordScriptEvent {

// <--[event]
// @Events
// discord channel created
//
// @Switch for:<bot> to only process the event for a specified Discord bot.
// @Switch group:<group_id> to only process the event for a specified Discord group.
// @Switch type:<type> to only process the event if the channel is a specific channel_type.
//
// @Triggers when a Discord channel is created.
//
// @Plugin dDiscordBot
//
// @Group Discord
//
// @Context
// <context.bot> returns the relevant Discord bot object.
// <context.group> returns the group.
// <context.channel> returns the new channel.
// -->

public static DiscordChannelCreateScriptEvent instance;

public DiscordChannelCreateScriptEvent() {
instance = this;
registerCouldMatcher("discord channel created");
registerSwitches("group", "type");
}

public ChannelCreateEvent getEvent() {
return (ChannelCreateEvent) event;
}

@Override
public boolean matches(ScriptPath path) {
if (!tryGuild(path, getEvent().getGuild())) {
return false;
}
if (!runGenericSwitchCheck(path, "type", getEvent().getChannelType().name())) {
return false;
}
return super.matches(path);
}

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "group":
return new DiscordGroupTag(botID, getEvent().getGuild());
case "channel":
return new DiscordChannelTag(botID, getEvent().getChannel());
}
return super.getContext(name);
}

@Override
public String getName() {
return "DiscordChannelCreated";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.denizenscript.ddiscordbot.events;

import com.denizenscript.ddiscordbot.DiscordScriptEvent;
import com.denizenscript.ddiscordbot.objects.DiscordChannelTag;
import com.denizenscript.ddiscordbot.objects.DiscordGroupTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent;

public class DiscordChannelDeleteScriptEvent extends DiscordScriptEvent {

// <--[event]
// @Events
// discord channel deleted
//
// @Switch for:<bot> to only process the event for a specified Discord bot.
// @Switch group:<group_id> to only process the event for a specified Discord group.
//
// @Triggers when a Discord channel is created.
//
// @Plugin dDiscordBot
//
// @Group Discord
//
// @Context
// <context.bot> returns the relevant Discord bot object.
// <context.group> returns the group.
// <context.channel> returns the new channel.
// -->

public static DiscordChannelDeleteScriptEvent instance;

public DiscordChannelDeleteScriptEvent() {
instance = this;
registerCouldMatcher("discord channel deleted");
registerSwitches("group");
}

public ChannelDeleteEvent getEvent() {
return (ChannelDeleteEvent) event;
}

@Override
public boolean matches(ScriptPath path) {
if (!tryGuild(path, getEvent().getGuild())) {
return false;
}
return super.matches(path);
}

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "group":
return new DiscordGroupTag(botID, getEvent().getGuild());
case "channel":
return new DiscordChannelTag(botID, getEvent().getChannel());
}
return super.getContext(name);
}

@Override
public String getName() {
return "DiscordChannelDeleted";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class DiscordMessageDeletedScriptEvent extends DiscordScriptEvent {
public DiscordMessageDeletedScriptEvent() {
instance = this;
registerCouldMatcher("discord message deleted");
registerSwitches("for", "channel", "group");
registerSwitches("channel", "group");
}

public MessageDeleteEvent getEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class DiscordMessageModifiedScriptEvent extends DiscordScriptEvent {
public DiscordMessageModifiedScriptEvent() {
instance = this;
registerCouldMatcher("discord message modified");
registerSwitches("for", "channel", "group");
registerSwitches("channel", "group");
}

public MessageUpdateEvent getEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DiscordMessageReactionAddScriptEvent extends DiscordScriptEvent {
public DiscordMessageReactionAddScriptEvent() {
instance = this;
registerCouldMatcher("discord message reaction added");
registerSwitches("for", "channel", "group");
registerSwitches("channel", "group");
}

public MessageReactionAddEvent getEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DiscordMessageReactionRemoveScriptEvent extends DiscordScriptEvent
public DiscordMessageReactionRemoveScriptEvent() {
instance = this;
registerCouldMatcher("discord message reaction removed");
registerSwitches("for", "channel", "group");
registerSwitches("channel", "group");
}

public MessageReactionRemoveEvent getEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class DiscordMessageReceivedScriptEvent extends DiscordScriptEvent {
public DiscordMessageReceivedScriptEvent() {
instance = this;
registerCouldMatcher("discord message received");
registerSwitches("for", "channel", "group", "message");
registerSwitches("channel", "group", "message");
}

public MessageReceivedEvent getEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class DiscordSelectionUsedScriptEvent extends DiscordScriptEvent {
public DiscordSelectionUsedScriptEvent() {
instance = this;
registerCouldMatcher("discord selection used");
registerSwitches("for", "channel", "group", "id");
registerSwitches("channel", "group", "id");
}

public SelectionMenuEvent getEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class DiscordSlashCommandScriptEvent extends DiscordScriptEvent {
public DiscordSlashCommandScriptEvent() {
instance = this;
registerCouldMatcher("discord slash command");
registerSwitches("for", "channel", "group", "name");
registerSwitches("channel", "group", "name");
}

public SlashCommandEvent getEvent() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.denizenscript.ddiscordbot.events;

import com.denizenscript.ddiscordbot.DiscordScriptEvent;
import com.denizenscript.ddiscordbot.objects.DiscordChannelTag;
import com.denizenscript.ddiscordbot.objects.DiscordGroupTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import net.dv8tion.jda.api.events.thread.ThreadHiddenEvent;

public class DiscordThreadArchivedScriptEvent extends DiscordScriptEvent {

// <--[event]
// @Events
// discord thread archived
//
// @Switch for:<bot> to only process the event for a specified Discord bot.
// @Switch group:<group_id> to only process the event for a specified Discord group.
// @Switch parent:<channel_id> to only process the event for a specific parent channel ID.
//
// @Triggers when a Discord thread is archived.
//
// @Plugin dDiscordBot
//
// @Group Discord
//
// @Context
// <context.bot> returns the relevant Discord bot object.
// <context.group> returns the group.
// <context.thread> returns the thread channel.
// -->

public static DiscordThreadArchivedScriptEvent instance;

public DiscordThreadArchivedScriptEvent() {
instance = this;
registerCouldMatcher("discord thread archived");
registerSwitches("group", "parent");
}

public ThreadHiddenEvent getEvent() {
return (ThreadHiddenEvent) event;
}

@Override
public boolean matches(ScriptPath path) {
if (!tryGuild(path, getEvent().getGuild())) {
return false;
}
if (!tryChannel(path, getEvent().getThread().getParentChannel(), "parent")) {
return false;
}
return super.matches(path);
}

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "group":
return new DiscordGroupTag(botID, getEvent().getGuild());
case "thread":
return new DiscordChannelTag(botID, getEvent().getThread());
}
return super.getContext(name);
}

@Override
public String getName() {
return "DiscordThreadArchived";
}
}
Loading

0 comments on commit 70a5e61

Please sign in to comment.