Moved handling appeals from reactbot to here and improved it (more modern)
This commit is contained in:
parent
265ddd82c3
commit
a80d1952da
|
|
@ -9,6 +9,7 @@ import com.alttd.listeners.JDAListener;
|
||||||
import com.alttd.util.Logger;
|
import com.alttd.util.Logger;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.JDABuilder;
|
import net.dv8tion.jda.api.JDABuilder;
|
||||||
|
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
|
@ -30,7 +31,18 @@ public class AltitudeBot {
|
||||||
private void start() {
|
private void start() {
|
||||||
Logger.info("Starting bot...");
|
Logger.info("Starting bot...");
|
||||||
initConfigs();
|
initConfigs();
|
||||||
jda = JDABuilder.createDefault(SettingsConfig.TOKEN).build();
|
jda = JDABuilder.createDefault(SettingsConfig.TOKEN,
|
||||||
|
GatewayIntent.GUILD_MEMBERS,
|
||||||
|
GatewayIntent.GUILD_BANS,
|
||||||
|
GatewayIntent.GUILD_EMOJIS_AND_STICKERS,
|
||||||
|
GatewayIntent.GUILD_WEBHOOKS,
|
||||||
|
GatewayIntent.GUILD_PRESENCES,
|
||||||
|
GatewayIntent.GUILD_MESSAGES,
|
||||||
|
GatewayIntent.GUILD_MESSAGE_REACTIONS,
|
||||||
|
GatewayIntent.GUILD_MESSAGE_TYPING,
|
||||||
|
GatewayIntent.DIRECT_MESSAGES,
|
||||||
|
GatewayIntent.DIRECT_MESSAGE_TYPING,
|
||||||
|
GatewayIntent.MESSAGE_CONTENT).build();
|
||||||
ConsoleCommandManager.startConsoleCommands(jda);
|
ConsoleCommandManager.startConsoleCommands(jda);
|
||||||
DatabaseTables.createTables(Database.getDatabase().getConnection());
|
DatabaseTables.createTables(Database.getDatabase().getConnection());
|
||||||
// try {
|
// try {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
package com.alttd.buttonManager;
|
package com.alttd.buttonManager;
|
||||||
|
|
||||||
|
import com.alttd.buttonManager.buttons.autoReminder.ButtonAccepted;
|
||||||
|
import com.alttd.buttonManager.buttons.autoReminder.ButtonInProgress;
|
||||||
|
import com.alttd.buttonManager.buttons.autoReminder.ButtonRejected;
|
||||||
import com.alttd.buttonManager.buttons.remindMeConfirm.ButtonRemindMeCancel;
|
import com.alttd.buttonManager.buttons.remindMeConfirm.ButtonRemindMeCancel;
|
||||||
import com.alttd.buttonManager.buttons.remindMeConfirm.ButtonRemindMeConfirm;
|
import com.alttd.buttonManager.buttons.remindMeConfirm.ButtonRemindMeConfirm;
|
||||||
import com.alttd.buttonManager.buttons.suggestionReview.ButtonSuggestionReviewAccept;
|
import com.alttd.buttonManager.buttons.suggestionReview.ButtonSuggestionReviewAccept;
|
||||||
|
|
@ -26,7 +29,10 @@ public class ButtonManager extends ListenerAdapter {
|
||||||
new ButtonSuggestionReviewAccept(),
|
new ButtonSuggestionReviewAccept(),
|
||||||
new ButtonSuggestionReviewDeny(),
|
new ButtonSuggestionReviewDeny(),
|
||||||
new ButtonRemindMeCancel(),
|
new ButtonRemindMeCancel(),
|
||||||
new ButtonRemindMeConfirm());
|
new ButtonRemindMeConfirm(),
|
||||||
|
new ButtonAccepted(),
|
||||||
|
new ButtonInProgress(),
|
||||||
|
new ButtonRejected());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.alttd.buttonManager.buttons.autoReminder;
|
||||||
|
|
||||||
|
import com.alttd.buttonManager.DiscordButton;
|
||||||
|
import com.alttd.schedulers.ReminderScheduler;
|
||||||
|
import com.alttd.util.Util;
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
|
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
|
||||||
|
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
public class ButtonAccepted extends DiscordButton {
|
||||||
|
@Override
|
||||||
|
public String getButtonId() {
|
||||||
|
return "reminder_accepted";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(ButtonInteractionEvent event) {
|
||||||
|
Message message = event.getMessage();
|
||||||
|
if (!ButtonReminderUtil.shouldExecute(message, event))
|
||||||
|
return;
|
||||||
|
MessageEmbed embed = message.getEmbeds().get(0);
|
||||||
|
EmbedBuilder embedBuilder = new EmbedBuilder(embed).setColor(Color.GREEN);
|
||||||
|
ReminderScheduler.getInstance(event.getJDA()).removeReminder(message.getIdLong());
|
||||||
|
message.editMessageEmbeds(embedBuilder.build()).queue();
|
||||||
|
message.editMessageComponents().setComponents(Collections.emptyList()).queue();
|
||||||
|
event.replyEmbeds(Util.genericSuccessEmbed("Success", "This message has been marked as Accepted"))
|
||||||
|
.setEphemeral(true).queue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Button getButton() {
|
||||||
|
return Button.primary(getButtonId(), "Accepted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.alttd.buttonManager.buttons.autoReminder;
|
||||||
|
|
||||||
|
import com.alttd.buttonManager.DiscordButton;
|
||||||
|
import com.alttd.util.Util;
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
|
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
|
||||||
|
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class ButtonInProgress extends DiscordButton {
|
||||||
|
@Override
|
||||||
|
public String getButtonId() {
|
||||||
|
return "reminder_in_progress";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(ButtonInteractionEvent event) {
|
||||||
|
Message message = event.getMessage();
|
||||||
|
if (!ButtonReminderUtil.shouldExecute(message, event))
|
||||||
|
return;
|
||||||
|
MessageEmbed embed = message.getEmbeds().get(0);
|
||||||
|
EmbedBuilder embedBuilder = new EmbedBuilder(embed);
|
||||||
|
Color color = embed.getColor();
|
||||||
|
if (color == null || !color.equals(Color.ORANGE)) {
|
||||||
|
embedBuilder.setColor(Color.ORANGE).build();
|
||||||
|
} else {
|
||||||
|
embedBuilder.setColor(Color.GRAY).build();
|
||||||
|
}
|
||||||
|
message.editMessageEmbeds(embedBuilder.build()).queue();
|
||||||
|
event.replyEmbeds(Util.genericSuccessEmbed("Success", "This message has been marked as In Progress"))
|
||||||
|
.setEphemeral(true).queue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Button getButton() {
|
||||||
|
return Button.secondary(getButtonId(), "In Progress");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.alttd.buttonManager.buttons.autoReminder;
|
||||||
|
|
||||||
|
import com.alttd.buttonManager.DiscordButton;
|
||||||
|
import com.alttd.schedulers.ReminderScheduler;
|
||||||
|
import com.alttd.util.Util;
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
|
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
|
||||||
|
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
public class ButtonRejected extends DiscordButton {
|
||||||
|
@Override
|
||||||
|
public String getButtonId() {
|
||||||
|
return "reminder_denied";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(ButtonInteractionEvent event) {
|
||||||
|
Message message = event.getMessage();
|
||||||
|
if (!ButtonReminderUtil.shouldExecute(message, event))
|
||||||
|
return;
|
||||||
|
MessageEmbed embed = message.getEmbeds().get(0);
|
||||||
|
EmbedBuilder embedBuilder = new EmbedBuilder(embed).setColor(Color.RED);
|
||||||
|
ReminderScheduler.getInstance(event.getJDA()).removeReminder(message.getIdLong());
|
||||||
|
message.editMessageEmbeds(embedBuilder.build()).queue();
|
||||||
|
message.editMessageComponents().setComponents(Collections.emptyList()).queue();
|
||||||
|
event.replyEmbeds(Util.genericSuccessEmbed("Success", "This message has been marked as Denied"))
|
||||||
|
.setEphemeral(true).queue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Button getButton() {
|
||||||
|
return Button.danger(getButtonId(), "Denied");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.alttd.buttonManager.buttons.autoReminder;
|
||||||
|
|
||||||
|
import com.alttd.util.Util;
|
||||||
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
|
import net.dv8tion.jda.api.entities.Role;
|
||||||
|
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ButtonReminderUtil {
|
||||||
|
public static boolean shouldExecute(Message message, ButtonInteractionEvent event) {
|
||||||
|
if (message.getEmbeds().size() != 1) {
|
||||||
|
event.replyEmbeds(Util.genericErrorEmbed("Error", "This message has too many embeds, it should only have one?"))
|
||||||
|
.setEphemeral(true).queue();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Member member = event.getMember();
|
||||||
|
if (member == null) {
|
||||||
|
event.replyEmbeds(Util.genericErrorEmbed("Error", "You are not a member?"))
|
||||||
|
.setEphemeral(true).queue();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
List<String> collect = member.getRoles().stream().map(Role::getName).toList();
|
||||||
|
if (!collect.contains("Appeals")) {
|
||||||
|
event.replyEmbeds(Util.genericErrorEmbed("Error", "You must be part of the Appeals team to use these buttons"))
|
||||||
|
.setEphemeral(true).queue();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,11 +5,10 @@ import com.alttd.commandManager.commands.*;
|
||||||
import com.alttd.commandManager.commands.PollCommand.CommandPoll;
|
import com.alttd.commandManager.commands.PollCommand.CommandPoll;
|
||||||
import com.alttd.contextMenuManager.ContextMenuManager;
|
import com.alttd.contextMenuManager.ContextMenuManager;
|
||||||
import com.alttd.database.Database;
|
import com.alttd.database.Database;
|
||||||
import com.alttd.listeners.ChatListener;
|
import com.alttd.listeners.LockedChannel;
|
||||||
import com.alttd.modalManager.ModalManager;
|
import com.alttd.modalManager.ModalManager;
|
||||||
import com.alttd.selectMenuManager.SelectMenuManager;
|
import com.alttd.selectMenuManager.SelectMenuManager;
|
||||||
import com.alttd.util.Logger;
|
import com.alttd.util.Logger;
|
||||||
import net.dv8tion.jda.api.EmbedBuilder;
|
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||||
|
|
@ -17,7 +16,6 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEve
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
@ -32,7 +30,7 @@ public class CommandManager extends ListenerAdapter {
|
||||||
private final List<DiscordCommand> commands;
|
private final List<DiscordCommand> commands;
|
||||||
private final HashMap<String, List<ScopeInfo>> commandList = new HashMap<>();
|
private final HashMap<String, List<ScopeInfo>> commandList = new HashMap<>();
|
||||||
|
|
||||||
public CommandManager(JDA jda, ModalManager modalManager, ContextMenuManager contextMenuManager, ChatListener chatListener, SelectMenuManager selectMenuManager) {
|
public CommandManager(JDA jda, ModalManager modalManager, ContextMenuManager contextMenuManager, LockedChannel lockedChannel, SelectMenuManager selectMenuManager) {
|
||||||
commandList.put("manage", new ArrayList<>(List.of(new ScopeInfo(CommandScope.GLOBAL, 0))));
|
commandList.put("manage", new ArrayList<>(List.of(new ScopeInfo(CommandScope.GLOBAL, 0))));
|
||||||
loadCommands();
|
loadCommands();
|
||||||
Logger.info("Loading commands...");
|
Logger.info("Loading commands...");
|
||||||
|
|
@ -52,7 +50,7 @@ public class CommandManager extends ListenerAdapter {
|
||||||
commandSetToggleableRoles,
|
commandSetToggleableRoles,
|
||||||
new CommandToggleRole(commandSetToggleableRoles, jda, this),
|
new CommandToggleRole(commandSetToggleableRoles, jda, this),
|
||||||
new CommandRemindMe(jda, this, modalManager),
|
new CommandRemindMe(jda, this, modalManager),
|
||||||
new CommandSoftLock(jda, this, chatListener),
|
new CommandSoftLock(jda, this, lockedChannel),
|
||||||
new CommandAuction(jda, this, selectMenuManager));
|
new CommandAuction(jda, this, selectMenuManager));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,11 @@ package com.alttd.commandManager.commands;
|
||||||
|
|
||||||
import com.alttd.commandManager.CommandManager;
|
import com.alttd.commandManager.CommandManager;
|
||||||
import com.alttd.commandManager.DiscordCommand;
|
import com.alttd.commandManager.DiscordCommand;
|
||||||
import com.alttd.listeners.ChatListener;
|
import com.alttd.listeners.LockedChannel;
|
||||||
import com.alttd.util.Util;
|
import com.alttd.util.Util;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.Permission;
|
import net.dv8tion.jda.api.Permission;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
|
||||||
import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion;
|
import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion;
|
||||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||||
|
|
@ -24,10 +23,10 @@ import java.util.stream.Collectors;
|
||||||
public class CommandSoftLock extends DiscordCommand {
|
public class CommandSoftLock extends DiscordCommand {
|
||||||
|
|
||||||
private final CommandData commandData;
|
private final CommandData commandData;
|
||||||
private final ChatListener chatListener;
|
private final LockedChannel lockedChannel;
|
||||||
|
|
||||||
public CommandSoftLock(JDA jda, CommandManager commandManager, ChatListener chatListener) {
|
public CommandSoftLock(JDA jda, CommandManager commandManager, LockedChannel lockedChannel) {
|
||||||
this.chatListener = chatListener;
|
this.lockedChannel = lockedChannel;
|
||||||
this.commandData = Commands.slash(getName(), "Auto delete all messages in a channel")
|
this.commandData = Commands.slash(getName(), "Auto delete all messages in a channel")
|
||||||
.addOption(OptionType.STRING, "state", "Set the soft lock \"on\" or \"off\"", true, true)
|
.addOption(OptionType.STRING, "state", "Set the soft lock \"on\" or \"off\"", true, true)
|
||||||
.addOption(OptionType.CHANNEL, "channel", "Channel to change soft lock state for", true)
|
.addOption(OptionType.CHANNEL, "channel", "Channel to change soft lock state for", true)
|
||||||
|
|
@ -73,24 +72,24 @@ public class CommandSoftLock extends DiscordCommand {
|
||||||
state = state.toLowerCase();
|
state = state.toLowerCase();
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case "on" -> {
|
case "on" -> {
|
||||||
if (chatListener.containsChannel(guildId, channelId)) {
|
if (lockedChannel.containsChannel(guildId, channelId)) {
|
||||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "<#" + channelId + ">" + " is already locked"))
|
event.replyEmbeds(Util.genericErrorEmbed("Error", "<#" + channelId + ">" + " is already locked"))
|
||||||
.setEphemeral(true).queue();
|
.setEphemeral(true).queue();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!chatListener.lockChannel(guildId, channelId))
|
if (!lockedChannel.lockChannel(guildId, channelId))
|
||||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "<#" + channelId + ">" + " could not be locked"))
|
event.replyEmbeds(Util.genericErrorEmbed("Error", "<#" + channelId + ">" + " could not be locked"))
|
||||||
.setEphemeral(true).queue();
|
.setEphemeral(true).queue();
|
||||||
event.replyEmbeds(Util.genericSuccessEmbed("Success", "Soft locked " + "<#" + channelId + ">" + "!"))
|
event.replyEmbeds(Util.genericSuccessEmbed("Success", "Soft locked " + "<#" + channelId + ">" + "!"))
|
||||||
.setEphemeral(true).queue();
|
.setEphemeral(true).queue();
|
||||||
}
|
}
|
||||||
case "off" -> {
|
case "off" -> {
|
||||||
if (!chatListener.containsChannel(guildId, channelId)) {
|
if (!lockedChannel.containsChannel(guildId, channelId)) {
|
||||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "<#" + channelId + ">" + " isn't locked"))
|
event.replyEmbeds(Util.genericErrorEmbed("Error", "<#" + channelId + ">" + " isn't locked"))
|
||||||
.setEphemeral(true).queue();
|
.setEphemeral(true).queue();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!chatListener.unlockChannel(guildId, channelId))
|
if (!lockedChannel.unlockChannel(guildId, channelId))
|
||||||
event.replyEmbeds(Util.genericErrorEmbed("Error", "<#" + channelId + ">" + " could not be unlocked"))
|
event.replyEmbeds(Util.genericErrorEmbed("Error", "<#" + channelId + ">" + " could not be unlocked"))
|
||||||
.setEphemeral(true).queue();
|
.setEphemeral(true).queue();
|
||||||
event.replyEmbeds(Util.genericSuccessEmbed("Success", "Unlocked " + "<#" + channelId + ">"))
|
event.replyEmbeds(Util.genericSuccessEmbed("Success", "Unlocked " + "<#" + channelId + ">"))
|
||||||
|
|
|
||||||
|
|
@ -23,14 +23,14 @@ public class Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openConnection() throws SQLException {
|
private void openConnection() throws SQLException {
|
||||||
if (connection != null && !connection.isClosed()) {
|
if (connection != null && !connection.isClosed() && connection.isValid(50)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (connection != null && !connection.isClosed()) {
|
// if (connection != null && !connection.isClosed()) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
try {
|
try {
|
||||||
Class.forName("com.mysql.jdbc.Driver");
|
Class.forName("com.mysql.jdbc.Driver");
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
|
|
@ -54,9 +54,6 @@ public class Database {
|
||||||
public Connection getConnection() {
|
public Connection getConnection() {
|
||||||
try {
|
try {
|
||||||
instance.openConnection();
|
instance.openConnection();
|
||||||
if (!instance.connection.isValid(50)) {
|
|
||||||
instance.connection.prepareStatement("SHOW TABLES").executeQuery();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (SQLException e) {
|
catch (SQLException e) {
|
||||||
Logger.sql(e);
|
Logger.sql(e);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,105 @@
|
||||||
|
package com.alttd.database.queries;
|
||||||
|
|
||||||
|
import com.alttd.database.Database;
|
||||||
|
import com.alttd.util.Logger;
|
||||||
|
import com.google.protobuf.StringValue;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class QueriesAssignAppeal {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the user to assign an appeal to, and sets the next user to have one assigned to them
|
||||||
|
* @return a userId to assign an appeal to or -1 if it fails
|
||||||
|
*/
|
||||||
|
public static long getAssignAppeal() {
|
||||||
|
String sql = "SELECT userId FROM appeal_list WHERE next = ?";
|
||||||
|
try {
|
||||||
|
PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement(sql);
|
||||||
|
statement.setInt(1, 1);
|
||||||
|
|
||||||
|
ResultSet resultSet = statement.executeQuery();
|
||||||
|
if (resultSet.next()) {
|
||||||
|
long userId = resultSet.getLong("userId");
|
||||||
|
setNextAppeal(userId);
|
||||||
|
return userId;
|
||||||
|
} else {
|
||||||
|
resetAssignedAppeal();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the first user with a userId bigger than the number we gave
|
||||||
|
* @param userId The id that's smaller than the next one we want to find
|
||||||
|
* @return a userId bigger than the given one or -1 if it can't find any
|
||||||
|
*/
|
||||||
|
private static long selectNextAssignment(long userId) {
|
||||||
|
String sql = "SELECT * FROM appeal_list WHERE userId > ? LIMIT 1";
|
||||||
|
try {
|
||||||
|
PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement(sql);
|
||||||
|
statement.setLong(1, userId);
|
||||||
|
|
||||||
|
ResultSet resultSet = statement.executeQuery();
|
||||||
|
if (resultSet.next()) {
|
||||||
|
return resultSet.getLong("userId");
|
||||||
|
}
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the next user in the list or the first one if we reached the and and assign them the appeal
|
||||||
|
* if there's no one to assign to it logs a warning
|
||||||
|
*/
|
||||||
|
private static void setNextAppeal(long userId) {
|
||||||
|
long nextUserId = selectNextAssignment(userId);
|
||||||
|
if (nextUserId == -1) {
|
||||||
|
nextUserId = selectNextAssignment(0);
|
||||||
|
if (nextUserId == -1) {
|
||||||
|
Logger.warning("No one to assign appeals to!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String sql1 = "UPDATE appeal_list SET next = ? WHERE userId = ?";
|
||||||
|
String sql2 = "UPDATE appeal_list SET next = ? WHERE userId = ?";
|
||||||
|
try {
|
||||||
|
PreparedStatement statement1 = Database.getDatabase().getConnection().prepareStatement(sql1);
|
||||||
|
statement1.setInt(1, 0);
|
||||||
|
statement1.setLong(2, userId);
|
||||||
|
PreparedStatement statement2 = Database.getDatabase().getConnection().prepareStatement(sql2);
|
||||||
|
statement2.setInt(1, 1);
|
||||||
|
statement2.setLong(2, nextUserId);
|
||||||
|
|
||||||
|
int res1 = statement1.executeUpdate();
|
||||||
|
int res2 = statement2.executeUpdate();
|
||||||
|
if (res1 != 1 || res2 != 1) {
|
||||||
|
Logger.warning("Unable to assign next appeal but got no error? results: " + res1 + res2);
|
||||||
|
}
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the first user in the list and assign them the appeal
|
||||||
|
* if there's no one to assign to it logs a warning
|
||||||
|
*/
|
||||||
|
private static void resetAssignedAppeal() {
|
||||||
|
long userId = selectNextAssignment(0);
|
||||||
|
if (userId == -1) {
|
||||||
|
Logger.warning("No one to assign appeals to!");
|
||||||
|
} else {
|
||||||
|
setNextAppeal(userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -42,6 +42,28 @@ public class QueriesReminders {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int updateReminderDate(Reminder reminder) {
|
||||||
|
String sql = "UPDATE new_reminders SET remind_date = ? WHERE id = ?";
|
||||||
|
try {
|
||||||
|
PreparedStatement preparedStatement = Database.getDatabase().getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
|
||||||
|
preparedStatement.setLong(1, reminder.remindDate());
|
||||||
|
preparedStatement.setInt(2, reminder.id());
|
||||||
|
|
||||||
|
if (preparedStatement.executeUpdate() == 1) {
|
||||||
|
ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
|
||||||
|
if (generatedKeys.next()) {
|
||||||
|
return generatedKeys.getInt(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
Logger.exception(e);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean removeReminder(int id) {
|
public static boolean removeReminder(int id) {
|
||||||
String sql = "DELETE FROM new_reminders WHERE id = ?";
|
String sql = "DELETE FROM new_reminders WHERE id = ?";
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
99
src/main/java/com/alttd/listeners/AppealRepost.java
Normal file
99
src/main/java/com/alttd/listeners/AppealRepost.java
Normal file
|
|
@ -0,0 +1,99 @@
|
||||||
|
package com.alttd.listeners;
|
||||||
|
|
||||||
|
import com.alttd.AltitudeBot;
|
||||||
|
import com.alttd.buttonManager.ButtonManager;
|
||||||
|
import com.alttd.database.queries.QueriesAssignAppeal;
|
||||||
|
import com.alttd.util.Logger;
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
|
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class AppealRepost extends ListenerAdapter {
|
||||||
|
|
||||||
|
private final ButtonManager buttonManager;
|
||||||
|
|
||||||
|
public AppealRepost(ButtonManager buttonManager) {
|
||||||
|
Logger.info("Created Appeal Repost -----------------------------------------------------------------------------------------------------------");
|
||||||
|
this.buttonManager = buttonManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
|
||||||
|
Logger.info("Message received:");
|
||||||
|
Logger.info(event.getMessage() + "\n\n" + event.getMessage().getContentRaw() + "\n\nembeds: " + event.getMessage().getEmbeds().size());
|
||||||
|
if (event.getMember() != null) { //Webhooks aren't members
|
||||||
|
Logger.info("Return 1");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.getGuild().getIdLong() != 514920774923059209L) {
|
||||||
|
Logger.info("Return 2");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.getChannel().getIdLong() != 514922555950235681L) {
|
||||||
|
Logger.info("Return 3 channel was: " + event.getChannel().getId());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Message message = event.getMessage();
|
||||||
|
List<MessageEmbed> embeds = message.getEmbeds();
|
||||||
|
if (embeds.size() == 0) {
|
||||||
|
Logger.info("Return 4");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MessageEmbed messageEmbed = embeds.get(0);
|
||||||
|
List<MessageEmbed.Field> fields = messageEmbed.getFields();
|
||||||
|
if (fields.size() == 0) {
|
||||||
|
Logger.info("Return 5");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String name = fields.get(0).getName();
|
||||||
|
if (name == null || !name.equals("Punishment info")) {
|
||||||
|
Logger.info("Return 6");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
EmbedBuilder embedBuilder = new EmbedBuilder(messageEmbed);
|
||||||
|
long userId = QueriesAssignAppeal.getAssignAppeal();
|
||||||
|
if (userId == -1){
|
||||||
|
Logger.info("user id was -1");
|
||||||
|
assignAndSendAppeal(embedBuilder, message, null);
|
||||||
|
} else {
|
||||||
|
Guild guild = message.getGuild();
|
||||||
|
Member member = guild.getMemberById(userId);
|
||||||
|
if (member != null) {
|
||||||
|
Logger.info("member was in cache");
|
||||||
|
assignAndSendAppeal(embedBuilder, message, member);
|
||||||
|
} else {
|
||||||
|
Logger.info("member wasn't in cache");
|
||||||
|
guild.retrieveMemberById(userId).queue(result -> assignAndSendAppeal(embedBuilder, message, result));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assignAndSendAppeal(EmbedBuilder embedBuilder, Message message, Member member) {
|
||||||
|
embedBuilder.addField(
|
||||||
|
"Assigned to " + (member == null ? " no one since we couldn't find a next user (check console)" : member.getEffectiveName()),
|
||||||
|
"",
|
||||||
|
false);
|
||||||
|
MessageEmbed embed = embedBuilder.build();
|
||||||
|
Button reminderAccepted = buttonManager.getButtonFor("reminder_accepted");
|
||||||
|
Button reminderInProgress = buttonManager.getButtonFor("reminder_in_progress");
|
||||||
|
Button reminderDenied = buttonManager.getButtonFor("reminder_denied");
|
||||||
|
if (reminderAccepted == null || reminderInProgress == null || reminderDenied == null) {
|
||||||
|
Logger.warning("Unable to get a button for appeals");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
message.getChannel().sendMessageEmbeds(embed).queue(res -> {
|
||||||
|
res.editMessageComponents().setActionRow(reminderAccepted, reminderInProgress, reminderDenied).queue();
|
||||||
|
res.createThreadChannel("Appeal").queue();
|
||||||
|
});
|
||||||
|
message.delete().queue();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -28,13 +28,14 @@ public class JDAListener extends ListenerAdapter {
|
||||||
@Override
|
@Override
|
||||||
public void onReady(@NotNull ReadyEvent event) {
|
public void onReady(@NotNull ReadyEvent event) {
|
||||||
Logger.info("JDA ready to register commands.");
|
Logger.info("JDA ready to register commands.");
|
||||||
ChatListener chatListener = new ChatListener();
|
LockedChannel lockedChannel = new LockedChannel();
|
||||||
ButtonManager buttonManager = new ButtonManager();
|
ButtonManager buttonManager = new ButtonManager();
|
||||||
|
AppealRepost appealRepost = new AppealRepost(buttonManager);
|
||||||
ModalManager modalManager = new ModalManager(buttonManager);
|
ModalManager modalManager = new ModalManager(buttonManager);
|
||||||
ContextMenuManager contextMenuManager = new ContextMenuManager(modalManager);
|
ContextMenuManager contextMenuManager = new ContextMenuManager(modalManager);
|
||||||
SelectMenuManager selectMenuManager = new SelectMenuManager();
|
SelectMenuManager selectMenuManager = new SelectMenuManager();
|
||||||
CommandManager commandManager = new CommandManager(jda, modalManager, contextMenuManager, chatListener, selectMenuManager);
|
CommandManager commandManager = new CommandManager(jda, modalManager, contextMenuManager, lockedChannel, selectMenuManager);
|
||||||
jda.addEventListener(buttonManager, modalManager, commandManager, contextMenuManager, chatListener, selectMenuManager);
|
jda.addEventListener(buttonManager, modalManager, commandManager, contextMenuManager, lockedChannel, appealRepost, selectMenuManager);
|
||||||
startSchedulers();
|
startSchedulers();
|
||||||
// RequestManager.init();
|
// RequestManager.init();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,11 @@ import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class ChatListener extends ListenerAdapter {
|
public class LockedChannel extends ListenerAdapter {
|
||||||
|
|
||||||
private final HashMap<Long, HashSet<Long>> lockedChannels;
|
private final HashMap<Long, HashSet<Long>> lockedChannels;
|
||||||
|
|
||||||
public ChatListener() {
|
public LockedChannel() {
|
||||||
HashMap<Long, HashSet<Long>> tmp = QueriesLockedChannels.getLockedChannels();
|
HashMap<Long, HashSet<Long>> tmp = QueriesLockedChannels.getLockedChannels();
|
||||||
lockedChannels = Objects.requireNonNullElseGet(tmp, HashMap::new);
|
lockedChannels = Objects.requireNonNullElseGet(tmp, HashMap::new);
|
||||||
if (tmp == null)
|
if (tmp == null)
|
||||||
|
|
@ -65,13 +65,19 @@ public class ReminderScheduler {
|
||||||
QueriesReminders.removeReminder(reminder.id());
|
QueriesReminders.removeReminder(reminder.id());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized void removeReminder(long messageId) {
|
||||||
|
reminders.stream()
|
||||||
|
.filter(reminder -> reminder.messageId() == messageId)
|
||||||
|
.findAny()
|
||||||
|
.ifPresent(this::removeReminder);
|
||||||
|
}
|
||||||
|
|
||||||
private class ReminderRun implements Runnable {
|
private class ReminderRun implements Runnable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
long time = new Date().getTime();
|
long time = new Date().getTime();
|
||||||
while (nextReminder != null && time > nextReminder.remindDate()) {
|
while (nextReminder != null && time > nextReminder.remindDate()) {
|
||||||
//TODO run reminder
|
|
||||||
TextChannel channel = nextReminder.getChannel(jda);
|
TextChannel channel = nextReminder.getChannel(jda);
|
||||||
if (channel == null || !channel.canTalk()) {
|
if (channel == null || !channel.canTalk()) {
|
||||||
Logger.warning("Unable to run reminder: " + nextReminder.id() +
|
Logger.warning("Unable to run reminder: " + nextReminder.id() +
|
||||||
|
|
@ -80,6 +86,22 @@ public class ReminderScheduler {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sendEmbed(nextReminder, channel);
|
sendEmbed(nextReminder, channel);
|
||||||
|
if (nextReminder.shouldRepeat()) {
|
||||||
|
Reminder repeatedReminder = new Reminder(
|
||||||
|
nextReminder.id(),
|
||||||
|
nextReminder.title(),
|
||||||
|
nextReminder.description(),
|
||||||
|
nextReminder.userId(),
|
||||||
|
nextReminder.guildId(),
|
||||||
|
nextReminder.channelId(),
|
||||||
|
nextReminder.messageId(),
|
||||||
|
nextReminder.shouldRepeat(),
|
||||||
|
nextReminder.creationDate(),
|
||||||
|
nextReminder.remindDate() + TimeUnit.DAYS.toMillis(1));
|
||||||
|
addReminder(repeatedReminder);
|
||||||
|
QueriesReminders.updateReminderDate(repeatedReminder);
|
||||||
|
}
|
||||||
|
else
|
||||||
removeReminder(nextReminder);
|
removeReminder(nextReminder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user