diff --git a/src/main/java/info/itsthesky/disky/core/MemberRemoveEventListener.java b/src/main/java/info/itsthesky/disky/core/MemberRemoveEventListener.java new file mode 100644 index 0000000..fe9b6ea --- /dev/null +++ b/src/main/java/info/itsthesky/disky/core/MemberRemoveEventListener.java @@ -0,0 +1,42 @@ +package info.itsthesky.disky.core; + +import info.itsthesky.disky.elements.events.member.MemberBanEvent; +import info.itsthesky.disky.elements.events.member.MemberKickEvent; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.audit.ActionType; +import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent; +import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +import java.util.WeakHashMap; + +/** + * Handler used to provide more information about a member remove event, e.g. kick or ban + * @author ItsTheSky + */ +public class MemberRemoveEventListener extends ListenerAdapter { + + private static final WeakHashMap WaitingKicks = new WeakHashMap<>(); + private static final WeakHashMap WaitingBans = new WeakHashMap<>(); + + @Override + public void onGuildMemberRemove(GuildMemberRemoveEvent event) { + WaitingKicks.put(event.getGuild().getIdLong(), + new MemberKickEvent.BukkitMemberKickEvent(event.getUser(), event.getGuild(), event.getJDA())); + WaitingBans.put(event.getGuild().getIdLong(), + new MemberBanEvent.BukkitMemberBanEvent(event.getUser(), event.getGuild(), event.getJDA())); + } + + @Override + public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) { + if (event.getEntry().getType() == ActionType.KICK && WaitingKicks.containsKey(event.getGuild().getIdLong())) { + final MemberKickEvent.BukkitMemberKickEvent e = WaitingKicks.remove(event.getGuild().getIdLong()); + e.author = event.getGuild().getMemberById(event.getEntry().getUserIdLong()); + SkriptUtils.dispatchEvent(e); + } else if (event.getEntry().getType() == ActionType.BAN && WaitingBans.containsKey(event.getGuild().getIdLong())) { + final MemberBanEvent.BukkitMemberBanEvent e = WaitingBans.remove(event.getGuild().getIdLong()); + e.author = event.getGuild().getMemberById(event.getEntry().getUserIdLong()); + SkriptUtils.dispatchEvent(e); + } + } +} diff --git a/src/main/java/info/itsthesky/disky/elements/events/member/BukkitMemberRemoveEvent.java b/src/main/java/info/itsthesky/disky/elements/events/member/BukkitMemberRemoveEvent.java new file mode 100644 index 0000000..e8b58d5 --- /dev/null +++ b/src/main/java/info/itsthesky/disky/elements/events/member/BukkitMemberRemoveEvent.java @@ -0,0 +1,33 @@ +package info.itsthesky.disky.elements.events.member; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.User; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class BukkitMemberRemoveEvent extends Event { + + public final User target; + public final Guild guild; + public final JDA bot; + public Member author; + + public BukkitMemberRemoveEvent(User target, Guild guild, JDA bot) { + this.target = target; + this.guild = guild; + this.bot = bot; + }; + + private final static HandlerList handlers = new HandlerList(); + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/info/itsthesky/disky/elements/events/member/MemberBanEvent.java b/src/main/java/info/itsthesky/disky/elements/events/member/MemberBanEvent.java new file mode 100644 index 0000000..0d34066 --- /dev/null +++ b/src/main/java/info/itsthesky/disky/elements/events/member/MemberBanEvent.java @@ -0,0 +1,57 @@ +package info.itsthesky.disky.elements.events.member; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.Literal; +import ch.njol.skript.lang.SkriptEvent; +import ch.njol.skript.lang.SkriptParser; +import info.itsthesky.disky.core.Bot; +import info.itsthesky.disky.core.SkriptUtils; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.User; +import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * @author ItsTheSky + */ +public class MemberBanEvent extends SkriptEvent { + + static { + Skript.registerEvent("Member Ban Event", + MemberBanEvent.class, BukkitMemberBanEvent.class, + "[discord] member ban[ned]"); + + SkriptUtils.registerValue(BukkitMemberBanEvent.class, User.class, + event -> event.target); + SkriptUtils.registerValue(BukkitMemberBanEvent.class, Guild.class, + event -> event.guild); + SkriptUtils.registerValue(BukkitMemberBanEvent.class, Bot.class, + event -> Bot.byJDA(event.bot)); + SkriptUtils.registerValue(BukkitMemberBanEvent.class, Member.class, + event -> event.author); + } + + @Override + public boolean init(Literal @NotNull [] args, int matchedPattern, SkriptParser.@NotNull ParseResult parseResult) { + return true; + } + + @Override + public boolean check(@NotNull Event event) { + return event instanceof BukkitMemberBanEvent; + } + + @Override + public @NotNull String toString(@Nullable Event event, boolean debug) { + return "member ban event"; + } + + public static class BukkitMemberBanEvent extends BukkitMemberRemoveEvent { + public BukkitMemberBanEvent(User target, Guild guild, JDA bot) { + super(target, guild, bot); + } + } +} diff --git a/src/main/java/info/itsthesky/disky/elements/events/member/MemberKickEvent.java b/src/main/java/info/itsthesky/disky/elements/events/member/MemberKickEvent.java index 6e77d75..acbd8ca 100644 --- a/src/main/java/info/itsthesky/disky/elements/events/member/MemberKickEvent.java +++ b/src/main/java/info/itsthesky/disky/elements/events/member/MemberKickEvent.java @@ -7,47 +7,18 @@ import info.itsthesky.disky.core.Bot; import info.itsthesky.disky.core.SkriptUtils; import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.audit.ActionType; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent; -import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; -import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.WeakHashMap; - /** * @author ItsTheSky */ public class MemberKickEvent extends SkriptEvent { - public static class MemberKickEventListener extends ListenerAdapter { - - private static final WeakHashMap WaitingKicks = new WeakHashMap<>(); - - @Override - public void onGuildMemberRemove(GuildMemberRemoveEvent event) { - WaitingKicks.put(event.getGuild().getIdLong(), - new BukkitMemberKickEvent(event.getUser(), event.getGuild(), event.getJDA())); - } - - @Override - public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) { - if (event.getEntry().getType() == ActionType.KICK && WaitingKicks.containsKey(event.getGuild().getIdLong())) { - final BukkitMemberKickEvent e = WaitingKicks.remove(event.getGuild().getIdLong()); - final JDA bot = e.bot; - e.setAuthor(bot.getUserById(event.getEntry().getUserIdLong())); - e.setAuthorMember(event.getGuild().getMemberById(event.getEntry().getUserIdLong())); - SkriptUtils.dispatchEvent(e); - } - } - } - static { Skript.registerEvent("Member Kick Event", MemberKickEvent.class, BukkitMemberKickEvent.class, @@ -60,7 +31,7 @@ public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) { SkriptUtils.registerValue(BukkitMemberKickEvent.class, Bot.class, event -> Bot.byJDA(event.bot)); SkriptUtils.registerValue(BukkitMemberKickEvent.class, Member.class, - event -> event.authorMember); + event -> event.author); } @Override @@ -78,37 +49,9 @@ public boolean check(@NotNull Event event) { return "member kick event"; } - public static class BukkitMemberKickEvent extends Event { - - private final User target; - private final Guild guild; - private final JDA bot; - private User author; - private Member authorMember; - - private final static HandlerList handlers = new HandlerList(); - + public static class BukkitMemberKickEvent extends BukkitMemberRemoveEvent { public BukkitMemberKickEvent(User target, Guild guild, JDA bot) { - this.target = target; - this.guild = guild; - this.bot = bot; - } - - public void setAuthor(User author) { - this.author = author; - } - - public void setAuthorMember(Member authorMember) { - this.authorMember = authorMember; - } - - @NotNull - @Override - public HandlerList getHandlers() { - return handlers; - } - public static HandlerList getHandlerList() { - return handlers; + super(target, guild, bot); } } } diff --git a/src/main/java/info/itsthesky/disky/managers/BotManager.java b/src/main/java/info/itsthesky/disky/managers/BotManager.java index ec08429..dccff08 100644 --- a/src/main/java/info/itsthesky/disky/managers/BotManager.java +++ b/src/main/java/info/itsthesky/disky/managers/BotManager.java @@ -5,6 +5,7 @@ import info.itsthesky.disky.api.events.EventListener; import info.itsthesky.disky.api.skript.ErrorHandler; import info.itsthesky.disky.core.Bot; +import info.itsthesky.disky.core.MemberRemoveEventListener; import info.itsthesky.disky.core.ReactionListener; import info.itsthesky.disky.elements.commands.CommandListener; import info.itsthesky.disky.elements.events.member.MemberKickEvent; @@ -52,7 +53,7 @@ private void configureBot(Bot bot) { bot.getInstance().addEventListener(new CommandListener()); bot.getInstance().addEventListener(new ReactionListener()); bot.getInstance().addEventListener(new MessageManager(bot)); - bot.getInstance().addEventListener(new MemberKickEvent.MemberKickEventListener()); + bot.getInstance().addEventListener(new MemberRemoveEventListener()); bot.getInstance().addEventListener(queuedListeners.toArray()); }