Reorganize Listeners & Remove GamePrevention

I'm removing gamePrevention as it was out of scope for kya.
Splitting the MainListener should help with expanding kya's capabilities.
The SkynetListener class will provide further info for the logChannel.
This commit is contained in:
Anya 2022-05-21 01:37:44 -07:00
parent 2cdfef9d8a
commit b8887705ef
5 changed files with 83 additions and 120 deletions

View file

@ -3,6 +3,7 @@ package moe.oko.Kiafumi;
import moe.oko.Kiafumi.command.CommandClass;
import moe.oko.Kiafumi.command.CommandRegistrar;
import moe.oko.Kiafumi.listener.MainListener;
import moe.oko.Kiafumi.listener.SkynetListener;
import moe.oko.Kiafumi.model.KiafumiDB;
import moe.oko.Kiafumi.model.ServerManager;
import moe.oko.Kiafumi.util.CommandInfo;
@ -168,7 +169,10 @@ public class Kiafumi {
.disableCache(CacheFlag.EMOTE)
.setActivity(Activity.of(Activity.ActivityType.valueOf(config.getActivityType()), config.getActivityMsg()))
.setStatus(OnlineStatus.valueOf(config.getStatusType()))
.addEventListeners(new MainListener()).build().awaitReady();
.addEventListeners(
new MainListener(),
new SkynetListener()
).build().awaitReady();
} catch(Exception ex) {
error("Initialization broke...");
ex.printStackTrace();

View file

@ -35,9 +35,7 @@ public class KiafumiConfig {
private String activityMsg;
private String statusType;
private List<String> pingResponses;
private boolean gamePrevention;
private int gameCheckTime;
private String gameToPrevent;
/*
SQL Variable Section
@ -92,9 +90,7 @@ public class KiafumiConfig {
activityMsg = main.getString("activityMsg");
statusType = main.getString("statusType");
pingResponses = main.getStringList("pingResponses");
gamePrevention = main.getBoolean("gamePrevention");
gameCheckTime = main.getInt("gameCheckTime");
gameToPrevent = main.getString("gameToPrevent");
// SQL loaders
var sql = configuration.getConfigurationSection("sql");
@ -130,12 +126,8 @@ public class KiafumiConfig {
public String getLogChannel() { return logChannel; }
public boolean isGamePreventionEnabled() { return gamePrevention; }
public int getGameCheckTime() { return gameCheckTime; }
public String getGameToPrevent() { return gameToPrevent; }
public List<String> getPingResponses() { return pingResponses; }
public KiafumiDB createDb() { return new KiafumiDB(username, password, host, port, database); }

View file

@ -3,31 +3,19 @@ package moe.oko.Kiafumi.listener;
import moe.oko.Kiafumi.Kiafumi;
import moe.oko.Kiafumi.util.EmbedUI;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.user.update.GenericUserPresenceEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Timer;
import java.util.TimerTask;
import static moe.oko.Kiafumi.Kiafumi.info;
import static moe.oko.Kiafumi.Kiafumi.debug;
import static moe.oko.Kiafumi.Kiafumi.error;
/**
* Main Listener
* Used for all utility garbage like guild handling and persistence. (Also LOL prevention).
* Used for all essential utility listeners, such as guild handling and persistence.
* @author Kay, oko, Tiddy
*/
public class MainListener extends ListenerAdapter {
@ -73,95 +61,4 @@ public class MainListener extends ListenerAdapter {
event.getChannel().sendMessageEmbeds(eb.build()).queue();
}
}
/**
* Game Prevention, is a global mechanic :^)
* @param event - GenericUserPresenceEvent event to be used.
*/
@Deprecated
@Override
public void onGenericUserPresence(@NotNull GenericUserPresenceEvent event) {
if(Kiafumi.instance.config.isGamePreventionEnabled()) {
// Proceed.
if(event.getMember().getActivities().contains(Activity.playing(Kiafumi.instance.config.getGameToPrevent()))) {
// THEY ARE PLAYING THE BIG BAD GAME!!!! WARN THE MIMMEDIATELYOYUITHTHHT
event.getMember().getUser().openPrivateChannel().complete().sendMessage("**YOU ARE PLAYING THE BIG BAD GAME STOP STOP STOP** " +
"(in " + Kiafumi.instance.config.getGameCheckTime() + " minutes i wll EVAPORATE you from every server i am IN.").queue();
var timer = new Timer();
int timeInMilisToKill = (Kiafumi.instance.config.getGameCheckTime() * 60) * 1000;
timer.schedule(new TimerTask() {
@Override
public void run() {
if(event.getMember().getActivities().contains(Activity.playing(Kiafumi.instance.config.getGameToPrevent()))) {
event.getMember().getUser().openPrivateChannel().complete().sendMessage("**I warned you, NOW SUFFER MY RAGE**").queue();
for(Guild guild : event.getJDA().getGuilds()) {
if(guild.isMember(event.getMember().getUser())) {
guild.ban(guild.getMember(event.getMember().getUser()), 0, "PLAYED A HORRIBLE GAME").queue();
}
}
} else {
event.getMember().getUser().openPrivateChannel().complete().sendMessage("Thank you for not playing that shitty game anymore.").queue();
}
}
}, timeInMilisToKill);
}
}
}
@Override
public void onGuildMemberJoin(@NotNull GuildMemberJoinEvent event) {
var server = Kiafumi.instance.getServerManager().getOrCreateServer(event.getGuild());
if(server.getJoinRole() != null) {
try {
event.getGuild().addRoleToMember(event.getMember(), event.getGuild().getRoleById(server.getJoinRole())).queue();
} catch(Exception ex) {
error("Failed to apply welcome role to " + event.getMember().getEffectiveName() + ", as the role does not exist.");
}
}
if(server.isWelcomeEnabled()) {
info("User [" + event.getUser().getName() + ":" + event.getUser().getId() + "] joined guild [" + event.getGuild().getName() + ':' + event.getGuild().getId() + "].");
// Fetch the welcome channel from settings.
TextChannel textChannel;
try {
textChannel = event.getGuild().getTextChannelById(server.getWelcomeChannel());
} catch (Exception ex) {
error("Failed to send join message to guild " + event.getGuild().getId() + " as the welcome channel was not found.");
return;
}
var dTF = DateTimeFormatter.ofPattern("MM-dd-yyyy");
// Prepare embed.
var embedBuilder = new EmbedBuilder()
.setColor(EmbedUI.SUCCESS)
.setAuthor(event.getMember().getEffectiveName() + "#" + event.getMember().getUser().getDiscriminator() + " ("
+ event.getMember().getId() + ")", null, event.getUser().getAvatarUrl()).setFooter("User Joined").setTimestamp(OffsetDateTime.now())
.setDescription(event.getMember().getAsMention() + " | **Joined Discord**: " + event.getMember().getTimeCreated().format(dTF));
textChannel.sendMessageEmbeds(embedBuilder.build()).queue();
debug("Guild join message successfully sent.");
}
}
@Override
public void onGuildMemberRemove(@NotNull GuildMemberRemoveEvent event) {
info("User [" + event.getUser().getName() + ":" + event.getUser().getId() + "] left guild [" + event.getGuild().getName() + ':' + event.getGuild().getId() + "].");
var server = Kiafumi.instance.getServerManager().getOrCreateServer(event.getGuild());
if(server.isWelcomeEnabled()) {
// Fetch the welcome channel from settings.
TextChannel textChannel;
try {
textChannel = event.getGuild().getTextChannelById(server.getWelcomeChannel());
} catch (Exception ex) {
error("Failed to send leave message to guild " + event.getGuild().getId() + " as the welcome channel was not found.");
return;
}
// Prepare embed.
var dTF = DateTimeFormatter.ofPattern("MM-dd-yyyy");
var embedBuilder = new EmbedBuilder()
.setColor(EmbedUI.FAILURE)
.setAuthor(event.getMember().getEffectiveName() + "#" + event.getMember().getUser().getDiscriminator() + " ("
+ event.getMember().getId() + ")", null, event.getUser().getAvatarUrl()).setFooter("User Left").setTimestamp(OffsetDateTime.now())
.setDescription(event.getMember().getAsMention() + " | **Joined Server**: " + event.getMember().getTimeJoined().format(dTF));
textChannel.sendMessageEmbeds(embedBuilder.build()).queue();
debug("Guild leave message successfully sent.");
}
}
}

View file

@ -0,0 +1,76 @@
package moe.oko.Kiafumi.listener;
import moe.oko.Kiafumi.Kiafumi;
import moe.oko.Kiafumi.util.EmbedUI;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import static moe.oko.Kiafumi.Kiafumi.error;
import static moe.oko.Kiafumi.Kiafumi.debug;
import static moe.oko.Kiafumi.Kiafumi.info;
public class SkynetListener extends ListenerAdapter {
@Override
public void onGuildMemberJoin(@NotNull GuildMemberJoinEvent event) {
var server = Kiafumi.instance.getServerManager().getOrCreateServer(event.getGuild());
if(server.getJoinRole() != null) {
try {
event.getGuild().addRoleToMember(event.getMember(), event.getGuild().getRoleById(server.getJoinRole())).queue();
} catch(Exception ex) {
error("Failed to apply welcome role to " + event.getMember().getEffectiveName() + ", as the role does not exist.");
}
}
if(server.isWelcomeEnabled()) {
info("User [" + event.getUser().getName() + ":" + event.getUser().getId() + "] joined guild [" + event.getGuild().getName() + ':' + event.getGuild().getId() + "].");
// Fetch the welcome channel from settings.
TextChannel textChannel;
try {
textChannel = event.getGuild().getTextChannelById(server.getWelcomeChannel());
} catch (Exception ex) {
error("Failed to send join message to guild " + event.getGuild().getId() + " as the welcome channel was not found.");
return;
}
var dTF = DateTimeFormatter.ofPattern("MM-dd-yyyy");
// Prepare embed.
var embedBuilder = new EmbedBuilder()
.setColor(EmbedUI.SUCCESS)
.setAuthor(event.getMember().getEffectiveName() + "#" + event.getMember().getUser().getDiscriminator() + " ("
+ event.getMember().getId() + ")", null, event.getUser().getAvatarUrl()).setFooter("User Joined").setTimestamp(OffsetDateTime.now())
.setDescription(event.getMember().getAsMention() + " | **Joined Discord**: " + event.getMember().getTimeCreated().format(dTF));
textChannel.sendMessageEmbeds(embedBuilder.build()).queue();
debug("Guild join message successfully sent.");
}
}
@Override
public void onGuildMemberRemove(@NotNull GuildMemberRemoveEvent event) {
info("User [" + event.getUser().getName() + ":" + event.getUser().getId() + "] left guild [" + event.getGuild().getName() + ':' + event.getGuild().getId() + "].");
var server = Kiafumi.instance.getServerManager().getOrCreateServer(event.getGuild());
if(server.isWelcomeEnabled()) {
// Fetch the welcome channel from settings.
TextChannel textChannel;
try {
textChannel = event.getGuild().getTextChannelById(server.getWelcomeChannel());
} catch (Exception ex) {
error("Failed to send leave message to guild " + event.getGuild().getId() + " as the welcome channel was not found.");
return;
}
// Prepare embed.
var dTF = DateTimeFormatter.ofPattern("MM-dd-yyyy");
var embedBuilder = new EmbedBuilder()
.setColor(EmbedUI.FAILURE)
.setAuthor(event.getMember().getEffectiveName() + "#" + event.getMember().getUser().getDiscriminator() + " ("
+ event.getMember().getId() + ")", null, event.getUser().getAvatarUrl()).setFooter("User Left").setTimestamp(OffsetDateTime.now())
.setDescription(event.getMember().getAsMention() + " | **Joined Server**: " + event.getMember().getTimeJoined().format(dTF));
textChannel.sendMessageEmbeds(embedBuilder.build()).queue();
debug("Guild leave message successfully sent.");
}
}
}

View file

@ -11,7 +11,7 @@
discord:
#Paste in your bot token here.
token: "DO NOT SHARE"
#The main guild the bot will recognize and do protection actions on. (Use LossPrevention for more in-depth actions!)
#The main guild the bot will recognize and do protection actions on.
mainGuild: ""
#The channel ID the bot will send important logging messages to.
logChannel: ""
@ -49,12 +49,6 @@ main:
- "Pinged pong"
- "Pinged the admins"
- "Pinged the guy in your walls"
#Prevents users in the guild from playing a certain game
gamePrevention: true
#Amount of time in minutes to check after a user starts playing the game until you kick them.
gameCheckTime: 30
#The game to be prevented
gameToPrevent: "League Of Legends"
sql:
host: "localhost"
port: 3306