refactor: bot.getServerString() to reduce mess

This commit is contained in:
ChomeNS
2025-02-16 08:03:38 +07:00
parent 4061bc5874
commit 8b5e706769
14 changed files with 25 additions and 21 deletions

View File

@@ -311,6 +311,10 @@ public class Bot {
session.connect(false);
}
public String getServerString () {
return host + ":" + port;
}
public void stop () {
session.disconnect("Received stop signal");
Main.bots.remove(this);

View File

@@ -197,7 +197,7 @@ public class Main {
for (Bot bot : copiedList) {
try {
if (discordEnabled) {
final String channelId = bot.discord.servers.get(bot.host + ":" + bot.port);
final String channelId = bot.discord.servers.get(bot.getServerString());
final MessageCreateAction messageAction = bot.discord.sendMessageInstantly(stoppingMessage, channelId, false);

View File

@@ -55,7 +55,7 @@ public class MailCommand extends Command {
sender.profile.getName(),
context.getString(false, true),
Instant.now().toEpochMilli(),
bot.host + ":" + bot.port,
bot.getServerString(),
context.getString(true, true)
)
);
@@ -86,7 +86,7 @@ public class MailCommand extends Command {
sender.profile.getName(),
context.getString(true, true),
Instant.now().toEpochMilli(),
bot.host + ":" + bot.port,
bot.getServerString(),
output
)
);

View File

@@ -29,18 +29,18 @@ public class NetMessageCommand extends Command {
final Bot bot = context.bot;
final List<Bot> bots = bot.bots;
final String hostAndPort = bot.host + ":" + bot.port;
final String originServer = bot.getServerString();
final Component component = Component.translatable(
"[%s]%s%s%s %s",
Component
.text(hostAndPort)
.text(originServer)
.color(NamedTextColor.GRAY)
.clickEvent(ClickEvent.copyToClipboard(hostAndPort))
.clickEvent(ClickEvent.copyToClipboard(originServer))
.hoverEvent(
HoverEvent.showText(
Component.empty()
.append(Component.text(hostAndPort).color(NamedTextColor.GRAY))
.append(Component.text(originServer).color(NamedTextColor.GRAY))
.append(Component.newline())
.append(Component.text("Click here to copy the server host and port to your clipboard").color(NamedTextColor.GREEN))
)

View File

@@ -16,7 +16,7 @@ public class AuthPlugin extends PlayersPlugin.Listener {
public AuthPlugin (Bot bot) {
this.bot = bot;
this.ownerIpForServer = bot.config.ownerAuthentication.ips.get(bot.host + ":" + bot.port);
this.ownerIpForServer = bot.config.ownerAuthentication.ips.get(bot.getServerString());
if (!bot.config.ownerAuthentication.enabled || ownerIpForServer == null) return;

View File

@@ -77,9 +77,9 @@ public class ConsolePlugin implements Completer {
if (line == null) return;
for (Bot bot : allBots) {
final String hostAndPort = bot.host + ":" + bot.port;
final String server = bot.getServerString();
if (!hostAndPort.equals(consoleServer) && !consoleServer.equals("all")) continue;
if (!server.equals(consoleServer) && !consoleServer.equals("all")) continue;
if (line.startsWith(prefix)) {
final ConsoleCommandContext context = new ConsoleCommandContext(bot, prefix);

View File

@@ -77,7 +77,7 @@ public class DiscordPlugin {
jda.getPresence().setPresence(Activity.playing(config.discord.statusMessage), false);
for (Bot bot : Main.bots) {
final String channelId = servers.get(bot.host + ":" + bot.port);
final String channelId = servers.get(bot.getServerString());
totalConnects.put(channelId, 0); // is this necessary?

View File

@@ -88,7 +88,7 @@ public class GrepLogPlugin {
if (matches == 0) throw new CommandException(Component.text("No matches has been found"));
final String channelId = bot.discord.servers.get(bot.host + ":" + bot.port);
final String channelId = bot.discord.servers.get(bot.getServerString());
final TextChannel logChannel = bot.discord.jda.getTextChannelById(channelId);
if (logChannel == null) return;

View File

@@ -209,7 +209,7 @@ public class IRCPlugin extends ListenerAdapter {
}
private void addMessageToQueue (Bot bot, String message) {
final String channel = servers.get(bot.host + ":" + bot.port);
final String channel = servers.get(bot.getServerString());
addMessageToQueue(channel, message);
}
@@ -227,7 +227,7 @@ public class IRCPlugin extends ListenerAdapter {
}
public void sendMessage (Bot bot, String message) {
final String hostAndPort = bot.host + ":" + bot.port;
final String hostAndPort = bot.getServerString();
final String channel = servers.get(hostAndPort);

View File

@@ -68,7 +68,7 @@ public class LoggerPlugin extends ChatPlugin.Listener {
final Component reason = event.getReason();
final String message = "Disconnected from " + bot.host + ":" + bot.port + ", reason: ";
final String message = "Disconnected from " + bot.getServerString() + ", reason: ";
final String string = ComponentUtilities.stringify(reason);

View File

@@ -87,7 +87,7 @@ public class PlayersDatabasePlugin extends PlayersPlugin.Listener {
} else {
statement = bot.database.connection.prepareStatement(FIND_ALTS_SINGLE_SERVER);
statement.setString(1, bot.host + ":" + bot.port);
statement.setString(1, bot.getServerString());
statement.setString(2, ip);
}
@@ -141,7 +141,7 @@ public class PlayersDatabasePlugin extends PlayersPlugin.Listener {
updatePlayerStatement.setString(2, "$.ips");
final ObjectNode ipsObject = JsonNodeFactory.instance.objectNode();
ipsObject.put(bot.host + ":" + bot.port, output);
ipsObject.put(bot.getServerString(), output);
updatePlayerStatement.setString(3, objectMapper.writeValueAsString(ipsObject));
@@ -186,7 +186,7 @@ public class PlayersDatabasePlugin extends PlayersPlugin.Listener {
final ObjectNode object = JsonNodeFactory.instance.objectNode();
object.put("time", Instant.now().toEpochMilli());
object.put("server", bot.host + ":" + bot.port);
object.put("server", bot.getServerString());
return object;
}

View File

@@ -77,7 +77,7 @@ public class PlayersPlugin extends Bot.Listener {
if (ipsObject == null || ipsObject.isNull() || !ipsObject.isObject()) return null;
final JsonNode targetIP = ipsObject.get(bot.host + ":" + bot.port);
final JsonNode targetIP = ipsObject.get(bot.getServerString());
if (targetIP == null || targetIP.isNull() || !targetIP.isTextual()) return null;

View File

@@ -52,7 +52,7 @@ public class LoggerUtilities {
final String formattedMessage = bot == null ? "" :
String.format(
"[%s] %s",
bot.host + ":" + bot.port,
bot.getServerString(),
message
);