Skip to content

Commit

Permalink
Localization support for slash commands
Browse files Browse the repository at this point in the history
Improved most slash commands, deprecated prefix commands and removed fallbacks for slash commands
JDA upgrade
  • Loading branch information
Alf-Melmac committed Jan 8, 2023
1 parent 34a8b9f commit 3d41760
Show file tree
Hide file tree
Showing 58 changed files with 864 additions and 1,021 deletions.
3 changes: 2 additions & 1 deletion .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 6 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--https://github.com/spring-projects/spring-boot/releases -->
<version>2.7.6</version>
<relativePath /> <!-- lookup parent from repository -->
<relativePath/> <!-- lookup parent from repository -->
</parent>

<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
<repository>
<id>dv8tion</id>
<name>m2-dv8tion</name>
<url>https://m2.dv8tion.net/releases</url>
</repository>
</repositories>

<groupId>de.webalf</groupId>
<artifactId>slotbot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>slotbot</name>
<description>Slotbot Server</description>
<scm>
<developerConnection>scm:git:https://github.com/Alf-Melmac/slotbotServer.git</developerConnection>
<tag>HEAD</tag>
</scm>
<tag>HEAD</tag>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -40,7 +29,7 @@
<!--https://github.com/atteo/classindex/tags -->
<classindex.version>3.13</classindex.version>
<!--https://ci.dv8tion.net/job/JDA/changes -->
<jda.version>4.4.0_352</jda.version>
<jda.version>5.0.0-beta.2</jda.version>
<!--https://jsoup.org/news/ -->
<jsoup.version>1.15.3</jsoup.version>
<!--https://github.com/ical4j/ical4j/tags -->
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/de/webalf/slotbot/constant/Emojis.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package de.webalf.slotbot.constant;

import lombok.experimental.UtilityClass;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.entities.emoji.UnicodeEmoji;

/**
* @author Alf
* @since 15.01.2021
*/
@UtilityClass
public class Emojis {
@Getter
@AllArgsConstructor
public enum Emojis {
//Codepoint notation
public static final String THUMBS_UP = "U+1F44D";
public static final String THUMBS_DOWN = "U+1F44E";
THUMBS_UP(Emoji.fromUnicode("U+1F44D")), //👍
THUMBS_DOWN(Emoji.fromUnicode("U+1F44E")), //👎
CHECKBOX(Emoji.fromUnicode("U+2611")), //☑
CROSS_MARK(Emoji.fromUnicode("U+274C")); //❌

//Standard discord notation
public static final String CHECKBOX = ":ballot_box_with_check:";
private final UnicodeEmoji emoji;

/**
* Get emoji as formatted string to be used in messages
*
* @return Formatted string
*/
public String getFormatted() {
return getEmoji().getFormatted();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.webalf.slotbot.model.annotations;
package de.webalf.slotbot.model.annotations.bot;

import de.webalf.slotbot.util.permissions.BotPermissionHelper.Authorization;
import org.atteo.classindex.IndexAnnotated;
Expand All @@ -15,6 +15,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@IndexAnnotated
@Deprecated
public @interface Command {
String[] names();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.webalf.slotbot.model.annotations.bot;

import de.webalf.slotbot.service.bot.command.DiscordSlashCommand;
import net.dv8tion.jda.api.Permission;
import org.atteo.classindex.IndexAnnotated;

import java.lang.annotation.*;

/**
* @author Alf
* @since 15.07.2021
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(SlashCommands.class)
@IndexAnnotated
public @interface SlashCommand {
/**
* Translatable name key
*/
String name();

/**
* Translatable description key
*/
String description();

Permission authorization();

/**
* Determines if options are available and which index should be used for {@link DiscordSlashCommand#getOptions(int)}
*/
int optionPosition() default -1;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.webalf.slotbot.model.annotations;
package de.webalf.slotbot.model.annotations.bot;

import org.atteo.classindex.IndexAnnotated;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.webalf.slotbot.model.annotations;
package de.webalf.slotbot.model.annotations.bot;

import org.atteo.classindex.IndexAnnotated;

Expand All @@ -14,6 +14,6 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@IndexAnnotated
public @interface SelectionMenuListener {
public @interface StringSelectInteraction {
String[] value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.webalf.slotbot.model.bot;

import lombok.Value;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;

/**
* Copy of {@link OptionData} to support translation keys in name and description
*
* @author Alf
* @since 07.01.2023
*/
@Value
public class TranslatableOptionData {
OptionType type;
String name;
String description;
boolean isRequired;

public OptionData toOptionData(String name, String description) {
return new OptionData(type, name, description, isRequired);
}
}
12 changes: 1 addition & 11 deletions src/main/java/de/webalf/slotbot/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,6 @@ public Event findByChannel(long channel) {
return findOptionalByChannel(channel).orElseThrow(ResourceNotFoundException::new);
}

/**
* Returns an optional for the event associated with the given eventId
*
* @param eventId to find event for
* @return Event found by id or empty optional
*/
public Optional<Event> findOptionalById(long eventId) {
return eventRepository.findById(eventId);
}

/**
* Returns the event associated with the given eventId
*
Expand All @@ -122,7 +112,7 @@ public Optional<Event> findOptionalById(long eventId) {
* @throws ResourceNotFoundException if no event with this eventId could be found
*/
public Event findById(long eventId) {
return findOptionalById(eventId).orElseThrow(ResourceNotFoundException::new);
return eventRepository.findById(eventId).orElseThrow(ResourceNotFoundException::new);
}

/**
Expand Down
29 changes: 13 additions & 16 deletions src/main/java/de/webalf/slotbot/service/bot/BotService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import javax.annotation.PreDestroy;
import javax.security.auth.login.LoginException;

import static de.webalf.slotbot.util.StringUtils.stripPrefixIfExists;
import static net.dv8tion.jda.api.requests.GatewayIntent.*;
Expand All @@ -31,7 +31,8 @@ public class BotService {
private final DiscordProperties discordProperties;
private final CommandClassHelper commandClassHelper;
private final ReactionAddService reactionAddService;
private final SlashCommandsService slashCommandsService;
private final CommandsService commandsService;
private final MessageSource messageSource;

@Getter
private JDA jda;
Expand All @@ -41,20 +42,16 @@ public class BotService {
public void startUp() {
String token = stripPrefixIfExists(discordProperties.getToken(), TOKEN_PREFIX);

try {
jda = JDABuilder
.createLight(token)
.enableIntents(GUILD_MEMBERS)
.addEventListeners(
new MessageReceivedListener(discordProperties, commandClassHelper),
new ReactionAddListener(reactionAddService),
new GuildReadyListener(slashCommandsService),
new InteractionListener(commandClassHelper))
.disableIntents(GUILD_BANS, GUILD_EMOJIS, GUILD_WEBHOOKS, GUILD_INVITES, GUILD_VOICE_STATES, GUILD_PRESENCES, GUILD_MESSAGE_REACTIONS, GUILD_MESSAGE_TYPING, DIRECT_MESSAGE_TYPING)
.build();
} catch (LoginException e) {
log.error("Failed to start discord bot", e);
}
jda = JDABuilder
.createLight(token)
.enableIntents(GUILD_MEMBERS, GUILD_MESSAGES, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS, MESSAGE_CONTENT) //TODO Validate which intents are needed
.addEventListeners(
new MessageReceivedListener(discordProperties, commandClassHelper),
new ReactionAddListener(reactionAddService),
new GuildReadyListener(commandsService),
new InteractionListener(commandClassHelper, messageSource))
.disableIntents(GUILD_BANS, GUILD_EMOJIS_AND_STICKERS, GUILD_WEBHOOKS, GUILD_INVITES, GUILD_VOICE_STATES, GUILD_PRESENCES, GUILD_MESSAGE_REACTIONS, GUILD_MESSAGE_TYPING, DIRECT_MESSAGE_TYPING, SCHEDULED_EVENTS)
.build();
}

@PreDestroy
Expand Down

0 comments on commit 3d41760

Please sign in to comment.