refactor: make the discord channel in config based on channel id instead of channel name so we can rename the channels from tonyboom to jorkboom for example, without having to modify the config

This commit is contained in:
ChomeNS
2025-05-27 18:21:08 +07:00
parent 48ffc7c01c
commit 76e874e077
9 changed files with 15 additions and 30 deletions

View File

@@ -1 +1 @@
3354
3358

View File

@@ -153,7 +153,7 @@ public class Configuration {
public int port;
public String username;
public String serverName;
public String discordChannel;
public String discordChannelId;
public String ircChannel;
public boolean hidden = false;
public boolean useCore = true;

View File

@@ -208,7 +208,7 @@ public class Main {
for (final Bot bot : copiedList) {
try {
if (discordEnabled) {
final String channelId = Main.discord.findChannelId(bot.options.discordChannel);
final String channelId = bot.options.discordChannelId;
final MessageCreateAction messageAction = Main.discord.sendMessageInstantly(stoppingMessage, channelId, false);

View File

@@ -81,7 +81,7 @@ public class ConsoleCommand extends Command {
throw new CommandException(Component.translatable("commands.generic.error.discord_disabled"));
}
final String channelId = Main.discord.findChannelId(context.bot.options.discordChannel);
final String channelId = context.bot.options.discordChannelId;
if (channelId == null) return null;

View File

@@ -51,7 +51,7 @@ public class GuildMessageEventHandler extends ListenerAdapter {
if (event.getAuthor().getId().equals(jda.getSelfUser().getId())) return;
for (final Bot bot : Main.bots) {
final String channelId = Main.discord.findChannelId(bot.options.discordChannel);
final String channelId = bot.options.discordChannelId;
if (channelId == null || !event.getChannel().getId().equals(channelId)) continue;

View File

@@ -60,7 +60,7 @@ public class SlashCommandHandler extends ListenerAdapter {
boolean found = false;
for (final Bot bot : Main.bots) {
final String channelId = Main.discord.findChannelId(bot.options.discordChannel);
final String channelId = bot.options.discordChannelId;
if (channelId == null || !event.getChannel().getId().equals(channelId)) continue;

View File

@@ -1,5 +1,6 @@
package me.chayapak1.chomens_bot.plugins;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.Configuration;
import me.chayapak1.chomens_bot.Main;
@@ -15,7 +16,6 @@ import me.chayapak1.chomens_bot.util.LoggerUtilities;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.requests.GatewayIntent;
@@ -26,11 +26,8 @@ import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.geysermc.mcprotocollib.network.event.session.ConnectedEvent;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
@@ -94,7 +91,7 @@ public class DiscordPlugin {
Main.EXECUTOR.scheduleAtFixedRate(this::onDiscordTick, 0, 50, TimeUnit.MILLISECONDS);
for (final Bot bot : Main.bots) {
final String channelId = findChannelId(bot.options.discordChannel);
final String channelId = bot.options.discordChannelId;
if (channelId == null) continue;
@@ -166,20 +163,8 @@ public class DiscordPlugin {
}
}
public @Nullable String findChannelId (final String channelName) {
if (channelName == null) return null;
final Guild guild = jda.getGuildById(serverId);
if (guild == null) return null;
final List<TextChannel> channels = guild.getTextChannelsByName(channelName, true);
if (channels.isEmpty()) return null;
return channels.getFirst().getId();
}
// based from HBot (and modified quite a bit)
private final Map<String, LogData> logData = new ConcurrentHashMap<>();
private final Map<String, LogData> logData = new Object2ObjectOpenHashMap<>();
public void sendMessage (final String message, final String channelId) {
final LogData data = logData.get(channelId);
@@ -230,7 +215,7 @@ public class DiscordPlugin {
public void onDiscordTick () {
for (final Bot bot : Main.bots) {
final String channelId = findChannelId(bot.options.discordChannel);
final String channelId = bot.options.discordChannelId;
if (channelId == null) continue;

View File

@@ -87,7 +87,7 @@ public class GrepLogPlugin {
if (matches == 0) throw new CommandException(Component.translatable("commands.greplog.error.no_matches_found"));
final String channelId = Main.discord.findChannelId(bot.options.discordChannel);
final String channelId = bot.options.discordChannelId;
if (channelId == null) return;
final TextChannel logChannel = Main.discord.jda.getTextChannelById(channelId);

View File

@@ -132,8 +132,8 @@ selfCare:
bots:
# username - optional, if not specified it will just use a random username
# serverName - name it whatever you like, it will be used as server name in trusted broadcast and in console
# discordChannel - the channel name like `localhost` (without #) for use with discord
# ircChannel - same as discordChannel but for irc
# discordChannelId - the channel ID for use with discord
# ircChannel - same as discordChannelId but for irc and is a channel name
# hidden - if enabled, the server address will not show anywhere in the bot, including discord, netmsg, etc., except console
# useCore - if enabled it just sends the command using chat instead of using core. recommended to enable useChat too when this is disabled
# useCorePlaceBlock - uses the place block core instead of the main core. only used if useCore is enabled
@@ -148,7 +148,7 @@ bots:
- host: 'localhost'
port: 25565
serverName: 'Localhost'
# be sure to set discordChannel and/or ircChannel here if you use the discord integration
# be sure to set discordChannelId and/or ircChannel here if you use the discord integration
reconnectDelay: 2000
chatQueueDelay: 125
@@ -157,7 +157,7 @@ bots:
# port: 25565
# username: 'ChomeNS_Bot'
# serverName: 'Localhost'
# discordChannel: 'localhost'
# discordChannelId: '1234567890'
# ircChannel: '#chomens/localhost'
# hidden: false
# useCore: true