-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Enhanced member kick event and added ban event
- Loading branch information
Showing
5 changed files
with
137 additions
and
61 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/info/itsthesky/disky/core/MemberRemoveEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/info/itsthesky/disky/elements/events/member/BukkitMemberRemoveEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/info/itsthesky/disky/elements/events/member/MemberBanEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters