guild setting tracking on guild join/leave
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package me.youhavetrouble.inviter;
|
package me.youhavetrouble.inviter;
|
||||||
|
|
||||||
|
import me.youhavetrouble.inviter.discord.listener.GuildJoinAndLeaveListener;
|
||||||
import me.youhavetrouble.inviter.http.ApiServer;
|
import me.youhavetrouble.inviter.http.ApiServer;
|
||||||
import me.youhavetrouble.inviter.discord.DiscordInviteManager;
|
import me.youhavetrouble.inviter.discord.DiscordInviteManager;
|
||||||
import me.youhavetrouble.inviter.storage.SqliteStorage;
|
import me.youhavetrouble.inviter.storage.SqliteStorage;
|
||||||
@@ -7,12 +8,12 @@ import me.youhavetrouble.inviter.storage.Storage;
|
|||||||
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 net.dv8tion.jda.api.requests.GatewayIntent;
|
||||||
import net.dv8tion.jda.api.utils.cache.CacheFlag;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
@@ -73,25 +74,14 @@ public class Main {
|
|||||||
|
|
||||||
storage = new SqliteStorage();
|
storage = new SqliteStorage();
|
||||||
|
|
||||||
jda = JDABuilder.create(
|
jda = JDABuilder.createLight(token, Set.of(GatewayIntent.GUILD_INVITES))
|
||||||
token,
|
.setCallbackPool(Executors.newVirtualThreadPerTaskExecutor())
|
||||||
Set.of(GatewayIntent.GUILD_INVITES)
|
.addEventListeners(new GuildJoinAndLeaveListener())
|
||||||
)
|
|
||||||
.disableCache(
|
|
||||||
CacheFlag.ACTIVITY,
|
|
||||||
CacheFlag.VOICE_STATE,
|
|
||||||
CacheFlag.EMOJI,
|
|
||||||
CacheFlag.STICKER,
|
|
||||||
CacheFlag.CLIENT_STATUS,
|
|
||||||
CacheFlag.ONLINE_STATUS,
|
|
||||||
CacheFlag.SCHEDULED_EVENTS
|
|
||||||
)
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
jda.awaitReady();
|
jda.awaitReady();
|
||||||
|
|
||||||
jda.getGuilds().parallelStream().forEach(guild -> storage.saveDefaultGuildSettings(guild.getIdLong()));
|
jda.getGuilds().parallelStream().forEach(guild -> storage.saveDefaultGuildSettings(guild.getIdLong()));
|
||||||
// TODO make sure to save default settings for guilds bot joins on runtime
|
|
||||||
|
|
||||||
discordInviteManager = new DiscordInviteManager(jda);
|
discordInviteManager = new DiscordInviteManager(jda);
|
||||||
|
|
||||||
@@ -105,6 +95,10 @@ public class Main {
|
|||||||
LOGGER.info("Welcome to the Inviter Application!");
|
LOGGER.info("Welcome to the Inviter Application!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static JDA getJda() {
|
||||||
|
return jda;
|
||||||
|
}
|
||||||
|
|
||||||
public static DiscordInviteManager getDiscordInviteMenager() {
|
public static DiscordInviteManager getDiscordInviteMenager() {
|
||||||
return discordInviteManager;
|
return discordInviteManager;
|
||||||
}
|
}
|
||||||
|
|||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
package me.youhavetrouble.inviter.discord.listener;
|
||||||
|
|
||||||
|
import me.youhavetrouble.inviter.Main;
|
||||||
|
import me.youhavetrouble.inviter.storage.Storage;
|
||||||
|
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
|
||||||
|
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
|
||||||
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class GuildJoinAndLeaveListener extends ListenerAdapter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGuildJoin(@NotNull GuildJoinEvent event) {
|
||||||
|
Storage storage = Main.getStorage();
|
||||||
|
long guildId = event.getGuild().getIdLong();
|
||||||
|
storage.removeGuildSettings(guildId);
|
||||||
|
storage.saveDefaultGuildSettings(guildId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGuildLeave(@NotNull GuildLeaveEvent event) {
|
||||||
|
Storage storage = Main.getStorage();
|
||||||
|
long guildId = event.getGuild().getIdLong();
|
||||||
|
storage.removeGuildSettings(guildId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
@@ -78,7 +79,7 @@ public class SqliteStorage implements Storage {
|
|||||||
public GuildSettings getGuildSettings(@NotNull String hostname) {
|
public GuildSettings getGuildSettings(@NotNull String hostname) {
|
||||||
|
|
||||||
try (Connection connection = dataSource.getConnection()) {
|
try (Connection connection = dataSource.getConnection()) {
|
||||||
var statement = connection.prepareStatement(
|
PreparedStatement statement = connection.prepareStatement(
|
||||||
"SELECT * FROM guild_settings WHERE api_hostname = ?"
|
"SELECT * FROM guild_settings WHERE api_hostname = ?"
|
||||||
);
|
);
|
||||||
statement.setString(1, hostname);
|
statement.setString(1, hostname);
|
||||||
@@ -98,7 +99,7 @@ public class SqliteStorage implements Storage {
|
|||||||
@Override
|
@Override
|
||||||
public void saveDefaultGuildSettings(long guildId) {
|
public void saveDefaultGuildSettings(long guildId) {
|
||||||
try (Connection connection = dataSource.getConnection()) {
|
try (Connection connection = dataSource.getConnection()) {
|
||||||
var statement = connection.prepareStatement(
|
PreparedStatement statement = connection.prepareStatement(
|
||||||
"INSERT OR IGNORE INTO guild_settings (guild_id) VALUES (?)"
|
"INSERT OR IGNORE INTO guild_settings (guild_id) VALUES (?)"
|
||||||
);
|
);
|
||||||
statement.setLong(1, guildId);
|
statement.setLong(1, guildId);
|
||||||
@@ -108,10 +109,23 @@ public class SqliteStorage implements Storage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeGuildSettings(long guildId) {
|
||||||
|
try (Connection connection = dataSource.getConnection()) {
|
||||||
|
PreparedStatement statement = connection.prepareStatement(
|
||||||
|
"DELETE FROM guild_settings WHERE guild_id = ?"
|
||||||
|
);
|
||||||
|
statement.setLong(1, guildId);
|
||||||
|
statement.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException("Failed to remove guild settings", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateDiscordApiEnabled(long guildId, boolean enabled) {
|
public void updateDiscordApiEnabled(long guildId, boolean enabled) {
|
||||||
try (Connection connection = dataSource.getConnection()) {
|
try (Connection connection = dataSource.getConnection()) {
|
||||||
var statement = connection.prepareStatement(
|
PreparedStatement statement = connection.prepareStatement(
|
||||||
"UPDATE guild_settings SET api_enabled = ? WHERE guild_id = ?"
|
"UPDATE guild_settings SET api_enabled = ? WHERE guild_id = ?"
|
||||||
);
|
);
|
||||||
statement.setBoolean(1, enabled);
|
statement.setBoolean(1, enabled);
|
||||||
@@ -126,7 +140,7 @@ public class SqliteStorage implements Storage {
|
|||||||
@Override
|
@Override
|
||||||
public void updateDiscordApiHostname(long guildId, @Nullable String hostname) {
|
public void updateDiscordApiHostname(long guildId, @Nullable String hostname) {
|
||||||
try (Connection connection = dataSource.getConnection()) {
|
try (Connection connection = dataSource.getConnection()) {
|
||||||
var statement = connection.prepareStatement(
|
PreparedStatement statement = connection.prepareStatement(
|
||||||
"UPDATE guild_settings SET api_hostname = ? WHERE guild_id = ?"
|
"UPDATE guild_settings SET api_hostname = ? WHERE guild_id = ?"
|
||||||
);
|
);
|
||||||
if (hostname == null || hostname.isEmpty()) {
|
if (hostname == null || hostname.isEmpty()) {
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ public interface Storage {
|
|||||||
|
|
||||||
void saveDefaultGuildSettings(long guildId);
|
void saveDefaultGuildSettings(long guildId);
|
||||||
|
|
||||||
|
void removeGuildSettings(long guildId);
|
||||||
|
|
||||||
void updateDiscordApiEnabled(long guildId, boolean enabled);
|
void updateDiscordApiEnabled(long guildId, boolean enabled);
|
||||||
|
|
||||||
void updateDiscordApiHostname(long guildId, @Nullable String hostname);
|
void updateDiscordApiHostname(long guildId, @Nullable String hostname);
|
||||||
|
|||||||
Reference in New Issue
Block a user