Skip to content

Commit

Permalink
✨ Enhanced member kick event and added ban event
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed May 5, 2024
1 parent 238a4f0 commit 78babc8
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -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<Long, MemberKickEvent.BukkitMemberKickEvent> WaitingKicks = new WeakHashMap<>();
private static final WeakHashMap<Long, MemberBanEvent.BukkitMemberBanEvent> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long, BukkitMemberKickEvent> 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,
Expand All @@ -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
Expand All @@ -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);
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/info/itsthesky/disky/managers/BotManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit 78babc8

Please sign in to comment.