Skip to content

Commit 09f0d0b

Browse files
committed
📝 Fix bot
1 parent 87a9c8c commit 09f0d0b

File tree

6 files changed

+65
-94
lines changed

6 files changed

+65
-94
lines changed

DiscordBot/src/main/java/fr/maxlego08/essentials/bot/DiscordBot.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,42 +47,43 @@ private DiscordBot() {
4747
this.storageManager.connect(this.configuration);
4848

4949
this.jda = builder.build();
50-
this.linkManager.loadCodes();
5150

52-
this.shutdown();
51+
this.addShutdownHook();
52+
this.listenForCommands();
5353
}
5454

5555
public static void main(String[] args) {
5656
new DiscordBot();
5757
}
5858

59-
private void shutdown() {
60-
// Add shutdown hook to disconnect the bot
59+
private void addShutdownHook() {
60+
// Add shutdown hook to ensure proper cleanup
6161
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
6262
if (this.jda != null) {
63-
this.jda.shutdown();
64-
System.out.println("Bot disconnected.");
63+
this.jda.shutdownNow();
64+
System.out.println("Bot disconnected by shutdown hook.");
6565
}
6666
}));
67+
}
6768

69+
private void listenForCommands() {
6870
// Scanner to listen for "stop" command
6971
this.scanner = new Scanner(System.in);
70-
while (true) {
71-
String command = scanner.nextLine();
72-
if (command.equalsIgnoreCase("stop")) {
73-
if (this.jda != null) {
74-
this.jda.shutdownNow();
75-
System.out.println("Bot stopped by user command.");
72+
Thread commandThread = new Thread(() -> {
73+
while (true) {
74+
String command = scanner.nextLine();
75+
if (command.equalsIgnoreCase("stop")) {
76+
if (this.jda != null) {
77+
this.jda.shutdownNow();
78+
System.out.println("Bot stopped by user command.");
79+
}
80+
break;
7681
}
77-
break;
7882
}
79-
}
80-
}
81-
82-
public void forceShutdown() {
83-
this.scanner.close();
84-
this.jda.shutdown();
85-
System.out.println("Bot disconnected.");
83+
scanner.close();
84+
});
85+
commandThread.setDaemon(true); // Ensures JVM exits when the main thread stops
86+
commandThread.start();
8687
}
8788

8889
public Configuration getConfiguration() {
@@ -102,9 +103,7 @@ public LinkManager getLinkManager() {
102103
}
103104

104105
public void reload() {
105-
106106
this.configurationManager.loadOrCreateConfig();
107107
this.configuration.loadConfiguration(configurationManager.getConfig());
108-
109108
}
110-
}
109+
}

DiscordBot/src/main/java/fr/maxlego08/essentials/bot/command/CommandManager.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import fr.maxlego08.essentials.bot.DiscordBot;
55
import fr.maxlego08.essentials.bot.command.commands.CommandReload;
66
import fr.maxlego08.essentials.bot.command.commands.CommandSetLinkMessage;
7-
import fr.maxlego08.essentials.bot.command.commands.CommandStop;
87
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
98

109
import java.util.ArrayList;
@@ -24,7 +23,6 @@ public CommandManager(DiscordBot instance) {
2423

2524
public void registerCommands() {
2625
this.commands.clear();
27-
registerCommand("stop", new CommandStop(this), "end");
2826
registerCommand("setlinkmessage", new CommandSetLinkMessage(this));
2927
registerCommand("reload", new CommandReload(this));
3028
}

DiscordBot/src/main/java/fr/maxlego08/essentials/bot/command/commands/CommandStop.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

DiscordBot/src/main/java/fr/maxlego08/essentials/bot/link/LinkManager.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,18 @@
1414
import net.dv8tion.jda.internal.interactions.component.ButtonImpl;
1515
import org.jetbrains.annotations.NotNull;
1616

17-
import java.util.ArrayList;
18-
import java.util.List;
1917
import java.util.Optional;
18+
import java.util.UUID;
2019

2120
public class LinkManager extends ListenerAdapter {
2221

2322
public static final String BUTTON_LINK_NAME = "zessentials:link";
2423
private final DiscordBot instance;
25-
private final List<DiscordCodeDTO> codes = new ArrayList<>();
2624

2725
public LinkManager(DiscordBot instance) {
2826
this.instance = instance;
2927
}
3028

31-
public void loadCodes() {
32-
var codes = instance.getStorageManager().loadCodes();
33-
this.codes.clear();
34-
this.codes.addAll(codes);
35-
}
36-
3729
public void sendLinkMessage(MessageChannelUnion textChannel) {
3830

3931
EmbedBuilder builder = instance.getConfiguration().getLink().embed().toEmbed();
@@ -81,9 +73,8 @@ private void createCode(ButtonInteractionEvent event, Guild guild, User user) {
8173
}
8274

8375
// Otherwise, we will create one
84-
String generatedCode = java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 16);
76+
String generatedCode = UUID.randomUUID().toString().replace("-", "").substring(0, 16);
8577
DiscordCodeDTO newCode = new DiscordCodeDTO(generatedCode, user.getIdLong(), user.getName());
86-
this.codes.add(newCode);
8778
replyCode(generatedCode, event);
8879
storage.saveCode(newCode);
8980
storage.insertLog(DiscordAction.CREATE_CODE, null, null, user.getEffectiveName(), user.getIdLong(), generatedCode);
@@ -114,6 +105,6 @@ private void replyCode(String code, ButtonInteractionEvent event) {
114105
}
115106

116107
private Optional<DiscordCodeDTO> getCode(long userId) {
117-
return this.codes.stream().filter(code -> code.user_id() == userId).findFirst();
108+
return this.instance.getStorageManager().getCode(userId);
118109
}
119110
}

DiscordBot/src/main/java/fr/maxlego08/essentials/bot/storage/StorageManager.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import fr.maxlego08.sarah.RequestHelper;
1212
import fr.maxlego08.sarah.logger.Logger;
1313

14-
import java.util.List;
1514
import java.util.Optional;
1615
import java.util.UUID;
1716
import java.util.concurrent.Executor;
@@ -41,10 +40,6 @@ public RequestHelper getRequestHelper() {
4140
return requestHelper;
4241
}
4342

44-
public List<DiscordCodeDTO> loadCodes() {
45-
return this.requestHelper.selectAll(Tables.LINK_CODES, DiscordCodeDTO.class);
46-
}
47-
4843
public void saveCode(DiscordCodeDTO code) {
4944
this.executor.execute(() -> this.requestHelper.insert(Tables.LINK_CODES, table -> {
5045
table.object("code", code.code());
@@ -71,4 +66,8 @@ public void insertLog(DiscordAction action, UUID uniqueId, String minecraftName,
7166
if (data != null) table.object("data", data);
7267
}));
7368
}
69+
70+
public Optional<DiscordCodeDTO> getCode(long userId) {
71+
return this.requestHelper.select(Tables.LINK_CODES, DiscordCodeDTO.class, table -> table.where("user_id", userId)).stream().findFirst();
72+
}
7473
}
Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
# Se bot discord is not a minecraft plugin!
2+
3+
# The token of your Discord Bot
14
bot-token: "DISCORD-TOKEN"
25

3-
guild-id: 0
6+
# The guild ID of your Discord Server
7+
guild-id: 511516467615760405
48

59
database-configuration:
610
# The prefix that will be applied to all tables, if you have several plugins with the same database you must have one.
@@ -16,46 +20,48 @@ database-configuration:
1620
password: 'secret'
1721
# Database
1822
database: zessentials
19-
# Enable of not the sql debug mode
23+
# Enable of not the SQL debug mode
2024
debug: false
2125

2226
link:
27+
2328
enable: true
24-
replyMessage: "Use the command `/link %code%` to link your account."
29+
30+
# Allows logs of actions performed by a user
31+
log:
32+
channel: 1324025378142163025
33+
create: "**%name%** (%id%) has just created the code `%code%`."
34+
ask: "**%name%** (%id%) has just requested the code `%code%`."
35+
36+
# Message sent when user interacts with the message
37+
messages:
38+
code: "Use the command `/link %code%` to link your account."
39+
already: "Your account is already linked."
40+
41+
# Button that the player will have to interact to link his account
2542
button:
26-
name: "Click to link your minecraft account"
43+
name: "Click to link your account"
2744
style: SUCCESS
28-
disabled: false
29-
# emoji: "U+2795"
45+
emoji: "U+2795" # The emoji that will be displayed on the button
3046

3147
embed:
3248
title:
33-
title: "Zessentials"
34-
url: "https://zessentials.net" # Ajout de l'URL au titre
35-
description: "Check out our website at: https://zessentials.net"
49+
title: "Link my account"
50+
# url: "https://zessentials.net" # The URL of the title
51+
description: "To link your account, follow these steps:\n1. Click the button above.\n2. Run the command in-game to complete the linking of your account."
3652
color:
3753
r: 10
3854
g: 150
3955
b: 10
40-
alpha: 255 # Valeur maximale pour l'alpha (optionnel)
56+
alpha: 255 # The alpha value of the color (optional)
4157
footer:
42-
text: "Footer text here"
43-
iconUrl: "https://example.com/footer-icon.png" # Optionnel
44-
thumbnail:
45-
url: "https://example.com/thumbnail.png"
46-
image:
47-
url: "https://example.com/image.png"
48-
author:
49-
name: "Author Name"
50-
url: "https://authorwebsite.net" # Optionnel
51-
iconUrl: "https://example.com/author-icon.png"
52-
fields:
53-
- name: "Field 1"
54-
value: "Value for field 1"
55-
inline: true # True pour aligner les champs sur la même ligne
56-
- name: "Field 2"
57-
value: "Value for field 2"
58-
inline: true
59-
- name: "Field 3"
60-
value: "Value for field 3"
61-
inline: false # False pour placer le champ sur une nouvelle ligne
58+
text: "zEssentials - 2025"
59+
# iconUrl: "https://groupez.dev/storage/images/users/0/0/0/1.png" # Optional
60+
# thumbnail:
61+
# url: "https://groupez.dev/storage/images/users/0/0/0/1.png"
62+
# image:
63+
# url: "https://example.com/image.png"
64+
# author:
65+
# name: "Author Name"
66+
# url: "https://authorwebsite.net" # Optional
67+
# iconUrl: "https://groupez.dev/storage/images/users/0/0/0/1.png"

0 commit comments

Comments
 (0)