Skip to content

Commit 7ad248f

Browse files
Fixed a lot of issues with backend (#41)
Switched from .env to json. Fixed an issue were buttons could not be used. Fixed some issues which caused commands to not work. That's it 🥳
1 parent 2900df1 commit 7ad248f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+283
-399
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ out/
2424
bin/
2525
!**/src/main/**/bin/
2626
!**/src/test/**/bin/
27-
*.env
2827

2928
### NetBeans ###
3029
/nbproject/private/
@@ -41,3 +40,10 @@ bin/
4140
codecov
4241
*.log
4342
gradle.properties
43+
44+
### log ###
45+
*.log.gz
46+
47+
### Security ###
48+
config.json
49+
*.env

README.md

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,26 @@ To get started, you'll need to create a Discord bot account and get a token.
3131

3232
### Part 2
3333
Once you have added the bot to your server, you will need to create a .env file as seen here.
34-
```env
35-
TOKEN=
36-
GUILD_ID=
37-
OWNER_ID=
38-
AUDIT_LOG_CHANNEL_ID=
39-
//for the excutor default is 1.
40-
CORE_POOL_SIZE=
41-
DATABASE_NAME=programming_bot
42-
DB_USER=
43-
DB_PASSWORD=
44-
DB_URL=
45-
PORT_NUMBER=
46-
SERVER_NAME=
47-
//used for creating questions
48-
HELP_CHANNEL_ID=
49-
//Used to post the thread channels which are open
50-
ACTIVE_QUESTION_CHANNEL_ID=
51-
//the max amount of help threads that can be created in a day, default is 2
52-
ASK_LIMIT=
53-
//Used to inform you how many members there is in the server
54-
USER_AMOUNT_CHANNEL_ID=
34+
35+
Where it says 11111111111 replace it with a long value
36+
```json
37+
{
38+
"TOKEN": "YOUR_TOKEN_HERE",
39+
"GUILD_ID": 11111111111,
40+
"OWNER_ID": 11111111111,
41+
"AUDIT_LOG_CHANNEL_ID": 11111111111,
42+
"CORE_POOL_SIZE": 1,
43+
"DATABASE_NAME": "YOUR_DATABASE_NAME_HERE",
44+
"DB_USER": "YOUR_DB_USER_HERE",
45+
"DB_PASSWORD": "YOUR_DB_PASSWORD_HERE",
46+
"DB_URL": "YOUR_DB_URL_HERE",
47+
"PORT_NUMBER": 8080,
48+
"SERVER_NAME": "YOUR_SERVER_NAME_HERE",
49+
"HELP_CHANNEL_ID": 11111111111,
50+
"ACTIVE_QUESTION_CHANNEL_ID": 11111111111,
51+
"ASK_LIMIT": 3,
52+
"USER_AMOUNT_CHANNEL_ID": 11111111111
53+
}
5554
```
5655

5756
### Part 3

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ dependencies {
5050
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.4.0'
5151
//implementation group: 'uk.org.lidalia', name: 'sysout-over-slf4j', version: '1.0.2'
5252
//config
53-
implementation group: 'io.github.yusufsdiscordbot', name: 'config', version: '1.0.4'
53+
implementation group: 'io.github.realyusufismail', name: 'jconfig', version: '1.0.5'
5454
//database
5555
implementation group: 'com.zaxxer', name: 'HikariCP', version: '5.0.1'
5656
implementation group: 'org.postgresql', name: 'postgresql', version: '42.5.0'
@@ -149,8 +149,8 @@ jar {
149149

150150
def props = new Properties()
151151
//if the file exists, load the properties
152-
if (file(".env").exists()) {
153-
file(".env").withInputStream { props.load(it) }
152+
if (file("config.json").exists()) {
153+
file("config.json").withInputStream { props.load(it) }
154154
}
155155

156156
//create the build directory so no errors happen with the workflow

example.config.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"TOKEN": "YOUR_TOKEN_HERE",
3+
"GUILD_ID": 11111111111,
4+
"OWNER_ID": 11111111111,
5+
"AUDIT_LOG_CHANNEL_ID": 11111111111,
6+
"CORE_POOL_SIZE": 1,
7+
"DATABASE_NAME": "YOUR_DATABASE_NAME_HERE",
8+
"DB_USER": "YOUR_DB_USER_HERE",
9+
"DB_PASSWORD": "YOUR_DB_PASSWORD_HERE",
10+
"DB_URL": "YOUR_DB_URL_HERE",
11+
"PORT_NUMBER": 8080,
12+
"SERVER_NAME": "YOUR_SERVER_NAME_HERE",
13+
"HELP_CHANNEL_ID": "YOUR_HELP_CHANNEL_ID_HERE",
14+
"ACTIVE_QUESTION_CHANNEL_ID": 11111111111,
15+
"ASK_LIMIT": 3,
16+
"USER_AMOUNT_CHANNEL_ID": 11111111111
17+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

src/main/java/io/github/org/programming/Bot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Bot {
2727

2828
public static void main(String[] args) throws Exception {
2929
try {
30-
new ProgrammingBot(args);
30+
new ProgrammingBot();
3131
} catch (Exception e) {
3232
logger.error("Failed to start bot", e);
3333
System.exit(1);

src/main/java/io/github/org/programming/backend/extension/MessageCommandExtender.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,14 @@
1919
package io.github.org.programming.backend.extension;
2020

2121
import io.github.org.programming.backend.builder.message.MessageCommand;
22-
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
23-
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
2422
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
25-
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
26-
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
23+
import net.dv8tion.jda.api.hooks.ListenerAdapter;
2724

2825
import javax.annotation.Nonnull;
2926

30-
public interface MessageCommandExtender {
27+
public abstract class MessageCommandExtender extends ListenerAdapter {
3128

32-
void onMessageContextInteraction(@Nonnull MessageContextInteractionEvent event);
29+
public abstract void onMessageContext(@Nonnull MessageContextInteractionEvent event);
3330

34-
default void onButtonClick(ButtonInteractionEvent event) {
35-
36-
}
37-
38-
default void onModalInteraction(ModalInteractionEvent event) {
39-
40-
}
41-
42-
default void onCommandAutoComplete(@Nonnull CommandAutoCompleteInteractionEvent event) {
43-
44-
}
45-
46-
default void onSelectMenu(@Nonnull SelectMenuInteractionEvent event) {
47-
48-
}
49-
50-
MessageCommand build();
31+
public abstract MessageCommand build();
5132
}

src/main/java/io/github/org/programming/backend/extension/SlashCommandExtender.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,14 @@
1919
package io.github.org.programming.backend.extension;
2020

2121
import io.github.org.programming.backend.builder.slash.SlashCommand;
22-
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
23-
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
2422
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
25-
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
26-
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
23+
import net.dv8tion.jda.api.hooks.ListenerAdapter;
2724
import org.jetbrains.annotations.NotNull;
2825

29-
import javax.annotation.Nonnull;
30-
31-
public interface SlashCommandExtender {
26+
public abstract class SlashCommandExtender extends ListenerAdapter {
3227

3328
// This method is called when the command is executed.
34-
void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event);
35-
36-
default void onButtonClick(ButtonInteractionEvent event) {
37-
38-
}
39-
40-
default void onModalInteraction(ModalInteractionEvent event) {
41-
42-
}
43-
44-
default void onCommandAutoComplete(@Nonnull CommandAutoCompleteInteractionEvent event) {
45-
46-
}
47-
48-
default void onSelectMenu(@Nonnull SelectMenuInteractionEvent event) {
49-
50-
}
29+
public abstract void onSlashCommand(@NotNull SlashCommandInteractionEvent event);
5130

52-
SlashCommand build();
31+
public abstract SlashCommand build();
5332
}

src/main/java/io/github/org/programming/backend/extension/UserCommandExtender.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,13 @@
1919
package io.github.org.programming.backend.extension;
2020

2121
import io.github.org.programming.backend.builder.user.UserCommand;
22-
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
23-
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
2422
import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent;
25-
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
26-
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
23+
import net.dv8tion.jda.api.hooks.ListenerAdapter;
2724
import org.jetbrains.annotations.NotNull;
2825

29-
import javax.annotation.Nonnull;
26+
public abstract class UserCommandExtender extends ListenerAdapter {
3027

31-
public interface UserCommandExtender {
28+
public abstract void onUserContext(@NotNull UserContextInteractionEvent event);
3229

33-
void onUserContextInteraction(@NotNull UserContextInteractionEvent event);
34-
35-
default void onButtonClick(ButtonInteractionEvent event) {
36-
37-
}
38-
39-
default void onModalInteraction(ModalInteractionEvent event) {
40-
41-
}
42-
43-
default void onCommandAutoComplete(@Nonnull CommandAutoCompleteInteractionEvent event) {
44-
45-
}
46-
47-
default void onSelectMenu(@Nonnull SelectMenuInteractionEvent event) {
48-
49-
}
50-
51-
UserCommand build();
30+
public abstract UserCommand build();
5231
}

src/main/java/io/github/org/programming/backend/handler/MessageCommandHandler.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@
2121
import io.github.org.programming.backend.extension.MessageCommandExtender;
2222
import net.dv8tion.jda.api.JDA;
2323
import net.dv8tion.jda.api.entities.Guild;
24-
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
25-
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
2624
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
27-
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
28-
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
2925
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
3026
import org.jetbrains.annotations.NotNull;
3127
import org.slf4j.Logger;
@@ -68,9 +64,8 @@ protected MessageCommandHandler(@NotNull JDA jda, @NotNull Guild guild) {
6864
*/
6965
@SuppressWarnings("ResultOfMethodCallIgnored")
7066
private void addMessageCommand(@NotNull MessageCommandExtender command) {
71-
7267
BaseHandler.checkIfBuildIsNull(command.build(), command.getClass().getSimpleName());
73-
68+
jda.addEventListener(command);
7469
messageCommand.put(command.build().getCommandData().getName(), command);
7570
if (command.build().isGuildOnly()) {
7671
guildCommandsData.addCommands(command.build().getCommandData());
@@ -136,35 +131,10 @@ public void onMessageContextInteraction(@Nonnull MessageContextInteractionEvent
136131
&& !event.getGuild().getSelfMember().hasPermission(cmd.build().getBotPerms())) {
137132
event.reply("I do not have permission to use this command.").setEphemeral(true).queue();
138133
} else {
139-
cmd.onMessageContextInteraction(event);
134+
cmd.onMessageContext(event);
140135
}
141136
}
142137

143-
@Override
144-
public void onButtonInteraction(@Nonnull ButtonInteractionEvent event) {
145-
final MessageCommandExtender cmd = messageCommand.get(event.getComponentId());
146-
cmd.onButtonClick(event);
147-
}
148-
149-
@Override
150-
public void onSelectMenuInteraction(@Nonnull SelectMenuInteractionEvent event) {
151-
final MessageCommandExtender cmd = messageCommand.get(event.getComponentId());
152-
cmd.onSelectMenu(event);
153-
}
154-
155-
@Override
156-
public void onCommandAutoCompleteInteraction(
157-
@Nonnull CommandAutoCompleteInteractionEvent event) {
158-
final MessageCommandExtender cmd = messageCommand.get(event.getName());
159-
cmd.onCommandAutoComplete(event);
160-
}
161-
162-
@Override
163-
public void onModalInteraction(@Nonnull ModalInteractionEvent event) {
164-
final MessageCommandExtender cmd = messageCommand.get(event.getModalId());
165-
cmd.onModalInteraction(event);
166-
}
167-
168138
/**
169139
* Gets slash commands as a list.
170140
*

0 commit comments

Comments
 (0)