Skip to content

Commit

Permalink
Add slash commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Chickenpowerrr committed Sep 3, 2022
1 parent 132078f commit 05643c3
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 72 deletions.
4 changes: 2 additions & 2 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ranksync</artifactId>
<groupId>com.gmail.chickenpowerrr</groupId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -23,7 +23,7 @@
<dependency>
<groupId>com.gmail.chickenpowerrr</groupId>
<artifactId>languagehelper</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions bungee/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<artifactId>ranksync</artifactId>
<groupId>com.gmail.chickenpowerrr</groupId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>

<groupId>com.gmail.chickenpowerrr.ranksync</groupId>
Expand Down Expand Up @@ -72,7 +72,7 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
<version>2.9.0</version>
<scope>compile</scope>
</dependency>

Expand Down
6 changes: 3 additions & 3 deletions discord/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ranksync</artifactId>
<groupId>com.gmail.chickenpowerrr</groupId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -61,7 +61,7 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
<version>20220320</version>
<scope>compile</scope>
</dependency>

Expand Down Expand Up @@ -103,7 +103,7 @@
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.29</version>
<version>1.30</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.gmail.chickenpowerrr.ranksync.api.player.PlayerFactory;
import com.gmail.chickenpowerrr.ranksync.api.rank.RankFactory;
import com.gmail.chickenpowerrr.ranksync.discord.command.LinkCommand;
import com.gmail.chickenpowerrr.ranksync.discord.event.DiscordCommandListeners;
import com.gmail.chickenpowerrr.ranksync.discord.event.DiscordEventListeners;
import com.gmail.chickenpowerrr.ranksync.discord.language.Translation;
import java.util.Collection;
Expand Down Expand Up @@ -68,7 +69,8 @@ public class DiscordBot implements Bot<Member, Role> {

try {
this.jda = JDABuilder.createDefault(properties.getString("token"))
.addEventListeners(new DiscordEventListeners(this)).build();
.addEventListeners(new DiscordEventListeners(this))
.addEventListeners(new DiscordCommandListeners(this)).build();
} catch (LoginException e) {
if (e.toString().contains("The provided token is invalid!")) {
RankSyncApi.getApi()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.gmail.chickenpowerrr.ranksync.discord.event;

import com.gmail.chickenpowerrr.ranksync.api.bot.Bot;
import com.gmail.chickenpowerrr.ranksync.api.player.Player;
import com.gmail.chickenpowerrr.ranksync.discord.bot.DiscordBot;
import com.gmail.chickenpowerrr.ranksync.discord.command.LinkCommand;
import com.gmail.chickenpowerrr.ranksync.discord.language.Translation;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Objects;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.guild.GuildReadyEvent;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import org.jetbrains.annotations.NotNull;

public class DiscordCommandListeners extends ListenerAdapter {

private final DiscordBot bot;
private final LinkCommand command;

public DiscordCommandListeners(Bot<Member, Role> bot) {
this.bot = (DiscordBot) bot;
this.command = new LinkCommand("link", new HashSet<>());
}

@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
if (!event.getUser().isBot()
&& Objects.equals(event.getGuild(), this.bot.getGuild())) {
this.bot.getPlayerFactory().getPlayer(event.getMember())
.thenAccept(Player::update).exceptionally(throwable -> {
throwable.printStackTrace();
return null;
});

this.bot.getPlayerFactory().getPlayer(event.getMember())
.thenAccept(player -> {
String message = this.command.execute(player, new ArrayList<>());
if (message != null) {
event.reply(message).setEphemeral(true).queue();
}
}).exceptionally(throwable -> {
throwable.printStackTrace();
return null;
});
}
}

@Override
public void onGuildReady(@NotNull GuildReadyEvent event) {
event.getGuild().updateCommands()
.addCommands(new CommandData("link",
Translation.LINK_COMMAND_DESCRIPTION.getTranslation()))
.queue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,16 @@

import com.gmail.chickenpowerrr.ranksync.api.RankSyncApi;
import com.gmail.chickenpowerrr.ranksync.api.bot.Bot;
import com.gmail.chickenpowerrr.ranksync.api.command.Command;
import com.gmail.chickenpowerrr.ranksync.api.event.PlayerUpdateOnlineStatusEvent;
import com.gmail.chickenpowerrr.ranksync.api.player.Player;
import com.gmail.chickenpowerrr.ranksync.api.player.Status;
import com.gmail.chickenpowerrr.ranksync.discord.bot.DiscordBot;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.user.update.UserUpdateOnlineStatusEvent;
import net.dv8tion.jda.api.exceptions.ErrorHandler;
import net.dv8tion.jda.api.hooks.EventListener;
import net.dv8tion.jda.api.requests.ErrorResponse;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -36,16 +23,12 @@
public class DiscordEventListeners implements EventListener {

private final DiscordBot bot;
private final int deleteDelay;
private final ScheduledExecutorService executorService;

/**
* @param bot the running Discord Bot
*/
public DiscordEventListeners(Bot<Member, Role> bot) {
this.bot = (DiscordBot) bot;
this.deleteDelay = bot.getDeleteTimer();
this.executorService = Executors.newSingleThreadScheduledExecutor();
}

/**
Expand All @@ -68,44 +51,6 @@ public void onEvent(@NotNull GenericEvent event) {
}
} else if (event instanceof ReadyEvent) {
this.bot.enable(event.getJDA());
} else if (event instanceof MessageReceivedEvent) {
MessageReceivedEvent messageReceivedEvent = (MessageReceivedEvent) event;
if (messageReceivedEvent.isFromGuild()
&& !messageReceivedEvent.getAuthor().isBot()
&& messageReceivedEvent.getGuild().equals(this.bot.getGuild())
&& messageReceivedEvent.isFromType(ChannelType.TEXT)) {
this.bot.getPlayerFactory().getPlayer(messageReceivedEvent.getMember())
.thenAccept(Player::update).exceptionally(throwable -> {
throwable.printStackTrace();
return null;
});

if (messageReceivedEvent.getMessage().getContentStripped().startsWith("!")) {
List<String> commandData = Arrays
.asList(messageReceivedEvent.getMessage().getContentStripped().split(" "));
String commandLabel = commandData.get(0).replaceFirst("!", "").toLowerCase();
Command command = this.bot.getCommandFactory().getCommand(commandLabel);
if (command != null) {
this.bot.getPlayerFactory().getPlayer(messageReceivedEvent.getMember())
.thenAccept(player -> {
String message = command.execute(player,
commandData.size() > 0 ? commandData.subList(1, commandData.size())
: new ArrayList<>());
if (message != null) {
messageReceivedEvent.getTextChannel().sendMessage(message)
.delay(deleteDelay, TimeUnit.SECONDS)
.flatMap(Message::delete)
.flatMap(a -> messageReceivedEvent.getMessage().delete())
.queue(null, new ErrorHandler()
.ignore(ErrorResponse.UNKNOWN_MESSAGE));
}
}).exceptionally(throwable -> {
throwable.printStackTrace();
return null;
});
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
public enum Translation {

LINK_COMMAND_DESCRIPTION,
LINK_COMMAND_ENABLE_PRIVATE_MESSAGES,
LINK_COMMAND_ALREADY_LINKED,
LINK_COMMAND_RIGHTTHERE,
Expand All @@ -19,6 +20,7 @@ public enum Translation {
LINK_COMMAND_PRIVATE,
DATABASE_VERSION_UNKNOWN;

private static final String DEFAULT_LANGUAGE = "english";
@Setter private static LanguageHelper languageHelper;
@Setter private static String language;

Expand All @@ -39,7 +41,7 @@ public enum Translation {
* @return a translation based on the placeholders
*/
public String getTranslation(String... replacements) {
String message = languageHelper.getMessage(language, this.key);
String message = languageHelper.getMessage(language, DEFAULT_LANGUAGE, this.key);

for (int i = 0; i < replacements.length; i += 2) {
message = message.replaceAll("%" + replacements[i] + "%", replacements[i + 1]);
Expand Down
2 changes: 1 addition & 1 deletion manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ranksync</artifactId>
<groupId>com.gmail.chickenpowerrr</groupId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<artifactId>ranksync</artifactId>
<name>RankSync</name>
<packaging>pom</packaging>
<version>1.4.1</version>
<version>1.4.2</version>
<modules>
<module>api</module>
<module>discord</module>
Expand Down
4 changes: 2 additions & 2 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ranksync</artifactId>
<groupId>com.gmail.chickenpowerrr</groupId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -44,7 +44,7 @@
<dependency>
<groupId>net.luckperms</groupId>
<artifactId>api</artifactId>
<version>5.3</version>
<version>5.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum Translation {
DISCORD_LINKINFO,
INVALID_RANK;

private static final String DEFAULT_LANGUAGE = "english";
@Setter private static LanguageHelper languageHelper;
@Setter private static String language;

Expand All @@ -47,7 +48,7 @@ public enum Translation {
* @return a translation based on the placeholders
*/
public String getTranslation(String... replacements) {
String message = languageHelper.getMessage(language, this.key);
String message = languageHelper.getMessage(language, DEFAULT_LANGUAGE, this.key);

for (int i = 0; i < replacements.length; i += 2) {
message = message.replaceAll("%" + replacements[i] + "%", replacements[i + 1]);
Expand Down
1 change: 1 addition & 0 deletions server/src/main/resources/language/dutch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ minecraft-unsync-command-invalid-service = &c%service% &4is geen valide service,
minecraft-unsync-command-usage = &4Gebruik dit commando alsjeblieft op de volgende manier: &c/%command% %services%
minecraft-discord-linkinfo = typ !link in het bot kanaal op Discord
minecraft-invalid-rank = %rank% is geen valide rank op %platform%
discord-link-command-description = Koppel je account
discord-link-command-already-linked = Jouw account is al gesynchroniseerd %name%
discord-link-command-enable-private-messages = %name% zet alsjeblieft je privé berichten aan
discord-link-command-rightthere = Je bericht is daar ^
Expand Down
1 change: 1 addition & 0 deletions server/src/main/resources/language/english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ minecraft-unsync-command-invalid-service = &c%service% &4isn't a valid service.
minecraft-unsync-command-usage = &4Please use that command like this: &c/%command% %services%
minecraft-discord-linkinfo = Type !link in the Discord channel containing the RankSync bot.
minecraft-invalid-rank = %rank% is not a valid rank on %platform%
discord-link-command-description = Link your account
discord-link-command-already-linked = Your account has already been linked, %name%!
discord-link-command-enable-private-messages = %name% please enable your private messages
discord-link-command-rightthere = Your message is right here ^
Expand Down
6 changes: 3 additions & 3 deletions spigot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ranksync</artifactId>
<groupId>com.gmail.chickenpowerrr</groupId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -78,14 +78,14 @@
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>2.2.1</version>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
<version>2.9.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down

0 comments on commit 05643c3

Please sign in to comment.