diff --git a/pom.xml b/pom.xml index e50609bf..771ac27a 100644 --- a/pom.xml +++ b/pom.xml @@ -10,8 +10,8 @@ UTF-8 - 1.11 - 1.11 + 11 + 11 diff --git a/src/main/java/commands/CommandManager.java b/src/main/java/commands/CommandManager.java index e15a3124..ad4f09ac 100644 --- a/src/main/java/commands/CommandManager.java +++ b/src/main/java/commands/CommandManager.java @@ -4,12 +4,15 @@ import commands.classic.*; import commands.config.*; import commands.hidden.SendNudeCommand; -import commands.model.Command; +import commands.model.LegacyCommand; +import commands.model.SlashCommand; +import discord4j.discordjson.json.ApplicationCommandRequest; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.stream.Collectors; /** * Created by steve on 20/05/2017. @@ -18,13 +21,15 @@ public class CommandManager { private static CommandManager instance; - private List commands; - private Map mapCommands; + private final List slashCommands; + private final List commands; + private final Map mapCommands; private CommandManager(){ super(); mapCommands = new ConcurrentHashMap<>(); commands = new CopyOnWriteArrayList<>(); + slashCommands = new CopyOnWriteArrayList<>(); // Basics commands addCommand(new AboutCommand()); @@ -68,16 +73,30 @@ public static CommandManager getInstance(){ return instance; } - public static List getCommands(){ + public static List getSlashCommands(){ + return getInstance().slashCommands; + } + + public static List getSlashCommandRequests(){ + return getInstance().slashCommands.stream() + .map(SlashCommand::getCommandRequest) + .collect(Collectors.toList()); + } + + public static List getCommands(){ return getInstance().commands; } - public static Command getCommand(String name){ + public static LegacyCommand getCommand(String name){ return getInstance().mapCommands.get(name); } - private void addCommand(Command command){ + private void addCommand(LegacyCommand command){ commands.add(command); mapCommands.put(command.getName(), command); + + if (command instanceof SlashCommand) { + slashCommands.add((SlashCommand) command); + } } } diff --git a/src/main/java/commands/admin/StatCommand.java b/src/main/java/commands/admin/StatCommand.java index 5a8a330d..ac940de2 100644 --- a/src/main/java/commands/admin/StatCommand.java +++ b/src/main/java/commands/admin/StatCommand.java @@ -1,6 +1,6 @@ package commands.admin; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import discord4j.common.store.action.read.ReadActions; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Guild; @@ -36,7 +36,7 @@ * Created by steve on 23/12/2017. */ @Slf4j -public class StatCommand extends AbstractCommand { +public class StatCommand extends AbstractLegacyCommand { private static final int GULD_LIMIT = 10; diff --git a/src/main/java/commands/classic/AboutCommand.java b/src/main/java/commands/classic/AboutCommand.java index cc38d1e0..b0979788 100644 --- a/src/main/java/commands/classic/AboutCommand.java +++ b/src/main/java/commands/classic/AboutCommand.java @@ -1,27 +1,33 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; +import commands.model.SlashCommand; import data.Constants; +import discord4j.core.GatewayDiscordClient; +import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.ApplicationInfo; import discord4j.core.object.entity.Message; -import discord4j.core.object.entity.User; +import discord4j.core.spec.EmbedCreateSpec; +import discord4j.discordjson.json.ApplicationCommandRequest; import discord4j.rest.util.Image; import enums.Donator; import enums.Graphist; import enums.Language; +import org.apache.commons.lang3.tuple.Pair; +import reactor.core.publisher.Mono; import util.Translator; +import java.util.Map; import java.util.Optional; import java.util.regex.Matcher; - -import static data.Constants.authorAvatar; -import static data.Constants.authorName; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Created by Songfu on 29/05/2017. */ -public class AboutCommand extends AbstractCommand { +public class AboutCommand extends AbstractLegacyCommand implements SlashCommand { public AboutCommand() { super("about", ""); @@ -29,58 +35,80 @@ public AboutCommand() { @Override public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) { - Optional appInfo = message.getClient().getApplicationInfo().blockOptional(); + message.getChannel() + .flatMap(chan -> chan.createMessage(aboutEmbed(message.getClient(), lg))) + .subscribe(); + } + + @Override + public Mono handle(ChatInputInteractionEvent event) { + return event.reply() + .withEphemeral(true) + .withEmbeds(aboutEmbed(event.getClient(), + Translator.mapLocale(event.getInteraction().getUserLocale()))); + } + + @Override + public ApplicationCommandRequest getCommandRequest() { + return ApplicationCommandRequest.builder() + .name(getName()) + .description(help(Constants.defaultLanguage, "")) + .descriptionLocalizationsOrNull(Stream.of(Language.values()) + .flatMap(lg -> lg.getLocales().stream() + .map(locale -> Pair.of(locale, help(lg, "")))) + .collect(Collectors.toMap(Pair::getLeft, Pair::getRight))) + .build(); + } - if (appInfo.isPresent()) { - message.getChannel().flatMap(chan -> chan.createEmbed(spec -> { + private EmbedCreateSpec aboutEmbed(GatewayDiscordClient client, Language lg){ + Optional appInfo = client.getApplicationInfo().blockOptional(); + StringBuilder donors = new StringBuilder(); + for (Donator donator : Donator.values()) + donors.append(donator.getName()).append(", "); + donors.setLength(donors.length() - 2); - spec.setTitle(Translator.getLabel(lg, "about.title") + return EmbedCreateSpec.builder() + .title(Translator.getLabel(lg, "about.title") .replace("{name}", Constants.name) .replace("{version}", Constants.version)) - .setImage(Constants.changelog) - .setThumbnail(appInfo.get().getIconUrl(Image.Format.PNG).orElse(null)) - .addField(Translator.getLabel(lg, "about.invite.title"), - Translator.getLabel(lg, "about.invite.desc") - .replace("{name}", Constants.name) - .replace("{invite}", Constants.invite), false) - .addField(Translator.getLabel(lg, "about.support.title"), - Translator.getLabel(lg, "about.support.desc") - .replace("{name}", Constants.name) - .replace("{discordInvite}", Constants.discordInvite), false) - .addField(Translator.getLabel(lg, "about.twitter.title"), - Translator.getLabel(lg, "about.twitter.desc") - .replace("{name}", Constants.name) - .replace("{twitter}", Constants.twitterAccount), false) - .addField(Translator.getLabel(lg, "about.opensource.title"), - Translator.getLabel(lg, "about.opensource.desc") - .replace("{git}", Constants.git), false) - .addField(Translator.getLabel(lg, "about.free.title"), - Translator.getLabel(lg, "about.free.desc") - .replace("{paypal}", Constants.paypal), false) - .addField(Translator.getLabel(lg, "about.privacy.title"), - Translator.getLabel(lg, "about.privacy.desc") - .replace("{paypal}", Constants.paypal), false) - .addField(Translator.getLabel(lg, "about.graphist.title"), - Translator.getLabel(lg, "about.graphist.desc") - .replace("{graphist}", Graphist.ELYCANN.toMarkdown()), false); - StringBuilder st = new StringBuilder(); - for(Donator donator : Donator.values()) - st.append(donator.getName()).append(", "); - st.setLength(st.length() - 2); - spec.addField(Translator.getLabel(lg, "about.donators.title"), - st.toString() + ".", false); - })).subscribe(); - } + .image(Constants.changelog) + .thumbnail(appInfo.flatMap(app -> app.getIconUrl(Image.Format.PNG)).orElse("")) + .addField(Translator.getLabel(lg, "about.invite.title"), + Translator.getLabel(lg, "about.invite.desc") + .replace("{name}", Constants.name) + .replace("{invite}", Constants.invite), false) + .addField(Translator.getLabel(lg, "about.support.title"), + Translator.getLabel(lg, "about.support.desc") + .replace("{name}", Constants.name) + .replace("{discordInvite}", Constants.discordInvite), false) + .addField(Translator.getLabel(lg, "about.twitter.title"), + Translator.getLabel(lg, "about.twitter.desc") + .replace("{name}", Constants.name) + .replace("{twitter}", Constants.twitterAccount), false) + .addField(Translator.getLabel(lg, "about.opensource.title"), + Translator.getLabel(lg, "about.opensource.desc") + .replace("{git}", Constants.git), false) + .addField(Translator.getLabel(lg, "about.free.title"), + Translator.getLabel(lg, "about.free.desc") + .replace("{paypal}", Constants.paypal), false) + .addField(Translator.getLabel(lg, "about.privacy.title"), + Translator.getLabel(lg, "about.privacy.desc") + .replace("{paypal}", Constants.paypal), false) + .addField(Translator.getLabel(lg, "about.graphist.title"), + Translator.getLabel(lg, "about.graphist.desc") + .replace("{graphist}", Graphist.ELYCANN.toMarkdown()), false) + .addField(Translator.getLabel(lg, "about.donators.title"), donors + ".", false) + .build(); } @Override - public String help(Language lg, String prefixe) { - return "**" + prefixe + name + "** " + Translator.getLabel(lg, "about.help") + public String help(Language lg, String prefix) { + return "**" + prefix + name + "** " + Translator.getLabel(lg, "about.help") .replace("{name}", Constants.name); } @Override - public String helpDetailed(Language lg, String prefixe) { - return help(lg, prefixe); + public String helpDetailed(Language lg, String prefix) { + return help(lg, prefix); } } diff --git a/src/main/java/commands/classic/AllianceCommand.java b/src/main/java/commands/classic/AllianceCommand.java index 6d92a154..391e1d4b 100644 --- a/src/main/java/commands/classic/AllianceCommand.java +++ b/src/main/java/commands/classic/AllianceCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.Alliance; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Message; @@ -24,7 +24,7 @@ /** * Created by steve on 04/04/2018. */ -public class AllianceCommand extends AbstractCommand { +public class AllianceCommand extends AbstractLegacyCommand { private final static String forPseudo = "text="; private final static String forServer = "alliance_server_id[]="; diff --git a/src/main/java/commands/classic/AlmanaxCommand.java b/src/main/java/commands/classic/AlmanaxCommand.java index 1e4cca89..2faefd6c 100644 --- a/src/main/java/commands/classic/AlmanaxCommand.java +++ b/src/main/java/commands/classic/AlmanaxCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.Almanax; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Message; @@ -18,7 +18,7 @@ /** * Created by steve on 14/07/2016. */ -public class AlmanaxCommand extends AbstractCommand { +public class AlmanaxCommand extends AbstractLegacyCommand { public AlmanaxCommand(){ super("almanax", "(\\s+\\d{2}/\\d{2}/\\d{4}|\\s+\\+\\d)?"); diff --git a/src/main/java/commands/classic/DistanceCommand.java b/src/main/java/commands/classic/DistanceCommand.java index c0127742..1b1ed509 100644 --- a/src/main/java/commands/classic/DistanceCommand.java +++ b/src/main/java/commands/classic/DistanceCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.Coordinate; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Message; @@ -13,7 +13,7 @@ /** * Created by steve on 14/07/2016. */ -public class DistanceCommand extends AbstractCommand { +public class DistanceCommand extends AbstractLegacyCommand { public DistanceCommand(){ super("dist", "\\s+\\[?(-?\\d{1,2})\\s*[,|\\s]\\s*(-?\\d{1,2})\\]?"); diff --git a/src/main/java/commands/classic/DonateCommand.java b/src/main/java/commands/classic/DonateCommand.java index cd96fc0e..511b11a1 100644 --- a/src/main/java/commands/classic/DonateCommand.java +++ b/src/main/java/commands/classic/DonateCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.Constants; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.ApplicationInfo; @@ -22,7 +22,7 @@ /** * Created by Kaysoro on 20/05/2019. */ -public class DonateCommand extends AbstractCommand { +public class DonateCommand extends AbstractLegacyCommand { public DonateCommand() { super("donate", ""); diff --git a/src/main/java/commands/classic/GuildCommand.java b/src/main/java/commands/classic/GuildCommand.java index ff98163b..436b043d 100644 --- a/src/main/java/commands/classic/GuildCommand.java +++ b/src/main/java/commands/classic/GuildCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.DofusGuild; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Message; @@ -24,7 +24,7 @@ /** * Created by steve on 30/03/2018. */ -public class GuildCommand extends AbstractCommand { +public class GuildCommand extends AbstractLegacyCommand { private final static String forPseudo = "text="; private final static String forServer = "guild_server_id[]="; diff --git a/src/main/java/commands/classic/HelpCommand.java b/src/main/java/commands/classic/HelpCommand.java index 03c610fc..b57ab5a4 100644 --- a/src/main/java/commands/classic/HelpCommand.java +++ b/src/main/java/commands/classic/HelpCommand.java @@ -1,8 +1,8 @@ package commands.classic; import commands.CommandManager; -import commands.model.AbstractCommand; -import commands.model.Command; +import commands.model.AbstractLegacyCommand; +import commands.model.LegacyCommand; import data.Constants; import data.Guild; import discord4j.core.event.domain.message.MessageCreateEvent; @@ -20,7 +20,7 @@ /** * Created by steve on 14/07/2016. */ -public class HelpCommand extends AbstractCommand { +public class HelpCommand extends AbstractLegacyCommand { public final static String NAME = "help"; @@ -38,7 +38,7 @@ public void request(MessageCreateEvent event, Message message, Matcher m, Langua List messages = new ArrayList<>(); boolean argumentFound = m.group(1) != null && m.group(1).replaceAll("^\\s+", "").length() > 0; - for(Command command : CommandManager.getCommands()) + for(LegacyCommand command : CommandManager.getCommands()) if (command.isPublic() && ! command.isAdmin() && (!command.isHidden() || argumentFound) && (message.getChannel().block() instanceof PrivateChannel || ! command.isForbidden(Guild.getGuild(message.getGuild().block())))){ diff --git a/src/main/java/commands/classic/InviteCommand.java b/src/main/java/commands/classic/InviteCommand.java index 4ee74bdf..8967df35 100644 --- a/src/main/java/commands/classic/InviteCommand.java +++ b/src/main/java/commands/classic/InviteCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.Constants; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.ApplicationInfo; @@ -19,7 +19,7 @@ /** * Created by Kaysoro on 20/05/2019. */ -public class InviteCommand extends AbstractCommand { +public class InviteCommand extends AbstractLegacyCommand { public InviteCommand() { super("invite", ""); diff --git a/src/main/java/commands/classic/JobCommand.java b/src/main/java/commands/classic/JobCommand.java index 2f7050a8..68f2af1c 100644 --- a/src/main/java/commands/classic/JobCommand.java +++ b/src/main/java/commands/classic/JobCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.Guild; import data.JobUser; import data.ServerDofus; @@ -19,7 +19,6 @@ import java.text.Normalizer; import java.util.*; -import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -27,7 +26,7 @@ /** * Created by steve on 14/07/2016. */ -public class JobCommand extends AbstractCommand { +public class JobCommand extends AbstractLegacyCommand { private final static int MAX_JOB_DISPLAY = 3; diff --git a/src/main/java/commands/classic/MapCommand.java b/src/main/java/commands/classic/MapCommand.java index 69814ca9..06b07fca 100644 --- a/src/main/java/commands/classic/MapCommand.java +++ b/src/main/java/commands/classic/MapCommand.java @@ -1,13 +1,12 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.Constants; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Message; import enums.Language; import util.Translator; -import java.awt.*; import java.util.*; import java.util.List; import java.util.regex.Matcher; @@ -15,7 +14,7 @@ /** * Created by steve on 14/07/2016. */ -public class MapCommand extends AbstractCommand { +public class MapCommand extends AbstractLegacyCommand { private static final int[] decimal = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000}; private static final String[] letters = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"}; diff --git a/src/main/java/commands/classic/PingCommand.java b/src/main/java/commands/classic/PingCommand.java index be2cb263..44c7ec9c 100644 --- a/src/main/java/commands/classic/PingCommand.java +++ b/src/main/java/commands/classic/PingCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Message; import enums.Language; @@ -13,7 +13,7 @@ /** * Created by Kaysoro on 24/05/2019. */ -public class PingCommand extends AbstractCommand { +public class PingCommand extends AbstractLegacyCommand { public PingCommand() { super("ping", ""); diff --git a/src/main/java/commands/classic/PortalCommand.java b/src/main/java/commands/classic/PortalCommand.java index 3732496c..ddb223f0 100644 --- a/src/main/java/commands/classic/PortalCommand.java +++ b/src/main/java/commands/classic/PortalCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.*; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Message; @@ -25,7 +25,7 @@ /** * Created by steve on 14/07/2016. */ -public class PortalCommand extends AbstractCommand { +public class PortalCommand extends AbstractLegacyCommand { private PortalFinder portalFinder; private DiscordException tooMuchPortals; diff --git a/src/main/java/commands/classic/RandomCommand.java b/src/main/java/commands/classic/RandomCommand.java index ba5bf92d..f7a5c2c6 100644 --- a/src/main/java/commands/classic/RandomCommand.java +++ b/src/main/java/commands/classic/RandomCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Message; import enums.Dungeon; @@ -17,7 +17,7 @@ /** * Created by steve on 14/07/2016. */ -public class RandomCommand extends AbstractCommand { +public class RandomCommand extends AbstractLegacyCommand { private final static int DEFAULT_TOLERANCE = 0; private final static int MIN_LEVEL = 10; diff --git a/src/main/java/commands/classic/TutorialCommand.java b/src/main/java/commands/classic/TutorialCommand.java index 899c1892..095c6910 100644 --- a/src/main/java/commands/classic/TutorialCommand.java +++ b/src/main/java/commands/classic/TutorialCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Message; import enums.Language; @@ -25,7 +25,7 @@ /** * Created by steve on 14/07/2016. */ -public class TutorialCommand extends AbstractCommand { +public class TutorialCommand extends AbstractLegacyCommand { private final static String forName = "q="; private final static String filtered = "filter=page"; diff --git a/src/main/java/commands/classic/WhoisCommand.java b/src/main/java/commands/classic/WhoisCommand.java index e008cbb4..a46a5686 100644 --- a/src/main/java/commands/classic/WhoisCommand.java +++ b/src/main/java/commands/classic/WhoisCommand.java @@ -1,6 +1,6 @@ package commands.classic; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.Character; import data.ServerDofus; import discord4j.core.event.domain.message.MessageCreateEvent; @@ -29,7 +29,7 @@ /** * Created by steve on 14/07/2016. */ -public class WhoisCommand extends AbstractCommand { +public class WhoisCommand extends AbstractLegacyCommand { private final static Logger LOG = LoggerFactory.getLogger(WhoisCommand.class); private final static String forPseudo = "text="; diff --git a/src/main/java/commands/config/AlmanaxAutoCommand.java b/src/main/java/commands/config/AlmanaxAutoCommand.java index 8bf34e7e..08340d22 100644 --- a/src/main/java/commands/config/AlmanaxAutoCommand.java +++ b/src/main/java/commands/config/AlmanaxAutoCommand.java @@ -1,6 +1,6 @@ package commands.config; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import discord4j.common.util.Snowflake; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Guild; @@ -15,7 +15,7 @@ /** * Created by steve on 14/07/2016. */ -public class AlmanaxAutoCommand extends AbstractCommand { +public class AlmanaxAutoCommand extends AbstractLegacyCommand { public AlmanaxAutoCommand(){ super("almanax-auto", "(\\s+true|\\s+false|\\s+0|\\s+1|\\s+on|\\s+off)"); diff --git a/src/main/java/commands/config/CommandCommand.java b/src/main/java/commands/config/CommandCommand.java index e81a2f56..d8179164 100644 --- a/src/main/java/commands/config/CommandCommand.java +++ b/src/main/java/commands/config/CommandCommand.java @@ -1,8 +1,8 @@ package commands.config; import commands.CommandManager; -import commands.model.AbstractCommand; -import commands.model.Command; +import commands.model.AbstractLegacyCommand; +import commands.model.LegacyCommand; import data.CommandForbidden; import data.Guild; import discord4j.core.event.domain.message.MessageCreateEvent; @@ -18,7 +18,7 @@ /** * Created by steve on 14/07/2016. */ -public class CommandCommand extends AbstractCommand { +public class CommandCommand extends AbstractLegacyCommand { private DiscordException tooMuchCmds; private DiscordException notFoundCmd; @@ -34,14 +34,14 @@ public CommandCommand(){ public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) { if (isUserHasEnoughRights(message)) { Guild guild = Guild.getGuild(message.getGuild().block()); - List potentialCmds = new ArrayList<>(); + List potentialCmds = new ArrayList<>(); String commandName = m.group(1).trim(); - for (Command command : CommandManager.getCommands()) + for (LegacyCommand command : CommandManager.getCommands()) if (command.isPublic() && !command.isAdmin() && command.getName().contains(commandName)) potentialCmds.add(command); if (potentialCmds.size() == 1){ - Command command = potentialCmds.get(0); + LegacyCommand command = potentialCmds.get(0); String value = m.group(2); if (command instanceof CommandCommand){ diff --git a/src/main/java/commands/config/LanguageCommand.java b/src/main/java/commands/config/LanguageCommand.java index f5f5d369..3a2a4491 100644 --- a/src/main/java/commands/config/LanguageCommand.java +++ b/src/main/java/commands/config/LanguageCommand.java @@ -1,6 +1,6 @@ package commands.config; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.ChannelLanguage; import data.Guild; import discord4j.core.event.domain.message.MessageCreateEvent; @@ -22,7 +22,7 @@ /** * Created by steve on 14/07/2016. */ -public class LanguageCommand extends AbstractCommand { +public class LanguageCommand extends AbstractLegacyCommand { private DiscordException tooMuchLangs; private DiscordException notFoundLang; diff --git a/src/main/java/commands/config/PrefixCommand.java b/src/main/java/commands/config/PrefixCommand.java index c94d4474..d25511ec 100644 --- a/src/main/java/commands/config/PrefixCommand.java +++ b/src/main/java/commands/config/PrefixCommand.java @@ -1,6 +1,6 @@ package commands.config; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.Constants; import data.Guild; import discord4j.core.event.domain.message.MessageCreateEvent; @@ -17,7 +17,7 @@ /** * Created by steve on 14/07/2016. */ -public class PrefixCommand extends AbstractCommand { +public class PrefixCommand extends AbstractLegacyCommand { private exceptions.DiscordException prefixeOutOfBounds; diff --git a/src/main/java/commands/config/RSSCommand.java b/src/main/java/commands/config/RSSCommand.java index 69f3149f..e1bb5ff8 100644 --- a/src/main/java/commands/config/RSSCommand.java +++ b/src/main/java/commands/config/RSSCommand.java @@ -1,6 +1,6 @@ package commands.config; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.Constants; import discord4j.common.util.Snowflake; import discord4j.core.event.domain.message.MessageCreateEvent; @@ -17,7 +17,7 @@ /** * Created by steve on 14/07/2016. */ -public class RSSCommand extends AbstractCommand { +public class RSSCommand extends AbstractLegacyCommand { private DiscordException rssFound; private DiscordException rssNotFound; diff --git a/src/main/java/commands/config/ServerCommand.java b/src/main/java/commands/config/ServerCommand.java index 12b069bf..427f8888 100644 --- a/src/main/java/commands/config/ServerCommand.java +++ b/src/main/java/commands/config/ServerCommand.java @@ -1,6 +1,6 @@ package commands.config; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import data.*; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Message; @@ -19,7 +19,7 @@ /** * Created by steve on 14/07/2016. */ -public class ServerCommand extends AbstractCommand { +public class ServerCommand extends AbstractLegacyCommand { public ServerCommand(){ super("server","(\\s+.+)?"); diff --git a/src/main/java/commands/config/TwitterCommand.java b/src/main/java/commands/config/TwitterCommand.java index e70ee671..6c247d58 100644 --- a/src/main/java/commands/config/TwitterCommand.java +++ b/src/main/java/commands/config/TwitterCommand.java @@ -1,6 +1,6 @@ package commands.config; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import discord4j.common.util.Snowflake; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Guild; @@ -16,7 +16,7 @@ /** * Created by steve on 14/07/2016. */ -public class TwitterCommand extends AbstractCommand { +public class TwitterCommand extends AbstractLegacyCommand { private DiscordException twitterFound; private DiscordException twitterNotFound; diff --git a/src/main/java/commands/hidden/SendNudeCommand.java b/src/main/java/commands/hidden/SendNudeCommand.java index 55388e4a..5d5f7298 100644 --- a/src/main/java/commands/hidden/SendNudeCommand.java +++ b/src/main/java/commands/hidden/SendNudeCommand.java @@ -1,6 +1,6 @@ package commands.hidden; -import commands.model.AbstractCommand; +import commands.model.AbstractLegacyCommand; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.Message; import discord4j.core.object.entity.channel.PrivateChannel; @@ -10,14 +10,13 @@ import exceptions.BasicDiscordException; import util.Translator; -import java.awt.*; import java.util.Random; import java.util.regex.Matcher; /** * Created by Kaysoro on 07/01/2019. */ -public class SendNudeCommand extends AbstractCommand { +public class SendNudeCommand extends AbstractLegacyCommand { private static final Random RANDOM = new Random(); diff --git a/src/main/java/commands/model/AbstractCommand.java b/src/main/java/commands/model/AbstractLegacyCommand.java similarity index 97% rename from src/main/java/commands/model/AbstractCommand.java rename to src/main/java/commands/model/AbstractLegacyCommand.java index dd4e04fc..2b34e647 100644 --- a/src/main/java/commands/model/AbstractCommand.java +++ b/src/main/java/commands/model/AbstractLegacyCommand.java @@ -25,9 +25,9 @@ /** * Created by steve on 14/07/2016. */ -public abstract class AbstractCommand implements Command { +public abstract class AbstractLegacyCommand implements LegacyCommand { - private final static Logger LOG = LoggerFactory.getLogger(AbstractCommand.class); + private final static Logger LOG = LoggerFactory.getLogger(AbstractLegacyCommand.class); protected String name; protected String pattern; @@ -38,7 +38,7 @@ public abstract class AbstractCommand implements Command { private boolean isHidden; - protected AbstractCommand(String name, String pattern){ + protected AbstractLegacyCommand(String name, String pattern){ super(); this.name = name; this.pattern = pattern; diff --git a/src/main/java/commands/model/DofusRequestCommand.java b/src/main/java/commands/model/DofusRequestCommand.java index 7e0a7615..607437e0 100644 --- a/src/main/java/commands/model/DofusRequestCommand.java +++ b/src/main/java/commands/model/DofusRequestCommand.java @@ -15,7 +15,7 @@ import java.util.ArrayList; import java.util.List; -public abstract class DofusRequestCommand extends AbstractCommand { +public abstract class DofusRequestCommand extends AbstractLegacyCommand { protected DofusRequestCommand(String name, String pattern) { super(name, pattern); diff --git a/src/main/java/commands/model/FetchCommand.java b/src/main/java/commands/model/FetchCommand.java index 51c8f188..27f7a6a5 100644 --- a/src/main/java/commands/model/FetchCommand.java +++ b/src/main/java/commands/model/FetchCommand.java @@ -7,7 +7,7 @@ import exceptions.TooMuchDiscordException; import java.util.List; -public abstract class FetchCommand extends AbstractCommand { +public abstract class FetchCommand extends AbstractLegacyCommand { protected DiscordException tooMuchServers; protected DiscordException notFoundServer; diff --git a/src/main/java/commands/model/Command.java b/src/main/java/commands/model/LegacyCommand.java similarity index 98% rename from src/main/java/commands/model/Command.java rename to src/main/java/commands/model/LegacyCommand.java index 85d92a2b..4923eea1 100644 --- a/src/main/java/commands/model/Command.java +++ b/src/main/java/commands/model/LegacyCommand.java @@ -10,7 +10,7 @@ /** * Created by steve on 14/07/2016. */ -public interface Command { +public interface LegacyCommand { String getName(); String getPattern(); Matcher getMatcher(Message message); diff --git a/src/main/java/commands/model/SlashCommand.java b/src/main/java/commands/model/SlashCommand.java new file mode 100644 index 00000000..7d82b8b2 --- /dev/null +++ b/src/main/java/commands/model/SlashCommand.java @@ -0,0 +1,14 @@ +package commands.model; + +import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; +import discord4j.discordjson.json.ApplicationCommandRequest; +import reactor.core.publisher.Mono; + +public interface SlashCommand { + + String getName(); + + Mono handle(ChatInputInteractionEvent event); + + ApplicationCommandRequest getCommandRequest(); +} diff --git a/src/main/java/data/CommandForbidden.java b/src/main/java/data/CommandForbidden.java index 5f1bcaa9..1c195867 100644 --- a/src/main/java/data/CommandForbidden.java +++ b/src/main/java/data/CommandForbidden.java @@ -1,6 +1,6 @@ package data; -import commands.model.Command; +import commands.model.LegacyCommand; import commands.CommandManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,15 +20,15 @@ public class CommandForbidden { private final static Logger LOG = LoggerFactory.getLogger(CommandForbidden.class); - private Command command; + private LegacyCommand command; private Guild guild; private boolean isSaved; - public CommandForbidden(Command command, Guild guild) { + public CommandForbidden(LegacyCommand command, Guild guild) { this(command, guild, false); } - public CommandForbidden(Command command, Guild guild, boolean isSaved) { + public CommandForbidden(LegacyCommand command, Guild guild, boolean isSaved) { this.command = command; this.guild = guild; this.isSaved = isSaved; @@ -102,7 +102,7 @@ public synchronized void removeToDatabase() { } } - public Command getCommand(){ + public LegacyCommand getCommand(){ return command; } diff --git a/src/main/java/data/Constants.java b/src/main/java/data/Constants.java index 48b7c69d..850a4415 100644 --- a/src/main/java/data/Constants.java +++ b/src/main/java/data/Constants.java @@ -17,7 +17,7 @@ public class Constants { /** * Application version */ - public final static String version = "1.7.0"; + public final static String version = "1.8.0"; /** * Changelog diff --git a/src/main/java/enums/Language.java b/src/main/java/enums/Language.java index d84bc380..afdcc20c 100644 --- a/src/main/java/enums/Language.java +++ b/src/main/java/enums/Language.java @@ -1,18 +1,25 @@ package enums; +import java.util.List; + /** * Created by steve on 07/06/2017. */ public enum Language { - FR("Français", "FR"), EN("English", "EN"), ES("Español", "ES"); + FR("Français", "FR", List.of("fr")), + EN("English", "EN", List.of("en-US", "en-GB")), + ES("Español", "ES", List.of("es-ES")); private String name; private String abrev; - Language(String name, String abrev){ + private List locale; + + Language(String name, String abrev, List locale){ this.name = name; this.abrev = abrev; + this.locale = locale; } public String getName() { @@ -23,6 +30,10 @@ public String getAbrev() { return abrev; } + public List getLocales() { + return locale; + } + @Override public String toString(){ return name; diff --git a/src/main/java/exceptions/AdvancedDiscordException.java b/src/main/java/exceptions/AdvancedDiscordException.java index 13caf9e0..812496df 100644 --- a/src/main/java/exceptions/AdvancedDiscordException.java +++ b/src/main/java/exceptions/AdvancedDiscordException.java @@ -1,6 +1,6 @@ package exceptions; -import commands.model.Command; +import commands.model.LegacyCommand; import discord4j.core.object.entity.Message; import enums.Language; import org.slf4j.Logger; @@ -31,7 +31,7 @@ public AdvancedDiscordException(String message, String[] parameters, Boolean[] t } @Override - public void throwException(Message message, Command command, Language lg, Object... arguments) { + public void throwException(Message message, LegacyCommand command, Language lg, Object... arguments) { String content = Translator.getLabel(lg, messageKey); for(int i = 0; i < parameters.length; i++) if (translatable.length > i && translatable[i]) diff --git a/src/main/java/exceptions/BadUseCommandDiscordException.java b/src/main/java/exceptions/BadUseCommandDiscordException.java index c3c4fb1f..6b3d727c 100644 --- a/src/main/java/exceptions/BadUseCommandDiscordException.java +++ b/src/main/java/exceptions/BadUseCommandDiscordException.java @@ -1,7 +1,7 @@ package exceptions; -import commands.model.AbstractCommand; -import commands.model.Command; +import commands.model.AbstractLegacyCommand; +import commands.model.LegacyCommand; import commands.classic.HelpCommand; import discord4j.core.object.entity.Message; import enums.Language; @@ -15,11 +15,11 @@ public class BadUseCommandDiscordException implements DiscordException { @Override - public void throwException(Message message, Command command, Language lg, Object... arguments) { + public void throwException(Message message, LegacyCommand command, Language lg, Object... arguments) { message.getAuthor().ifPresent(author -> message.getChannel().flatMap(chan -> chan .createMessage(Translator.getLabel(lg, "exception.bad_use_command") .replace("{author}", Matcher.quoteReplacement(author.getMention())) - .replaceAll("\\{prefix}", Matcher.quoteReplacement(AbstractCommand.getPrefix(message))) + .replaceAll("\\{prefix}", Matcher.quoteReplacement(AbstractLegacyCommand.getPrefix(message))) .replaceAll("\\{cmd.name}", command.getName()) .replace("{HelpCmd.name}", HelpCommand.NAME))) .subscribe()); diff --git a/src/main/java/exceptions/BasicDiscordException.java b/src/main/java/exceptions/BasicDiscordException.java index 357d49d6..f13afab8 100644 --- a/src/main/java/exceptions/BasicDiscordException.java +++ b/src/main/java/exceptions/BasicDiscordException.java @@ -1,6 +1,6 @@ package exceptions; -import commands.model.Command; +import commands.model.LegacyCommand; import discord4j.core.object.entity.Message; import enums.Language; import org.slf4j.Logger; @@ -39,7 +39,7 @@ private BasicDiscordException(String message){ this.messageKey = message; } @Override - public void throwException(Message message, Command command, Language lg, Object... arguments) { + public void throwException(Message message, LegacyCommand command, Language lg, Object... arguments) { message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, messageKey))).subscribe(); } } diff --git a/src/main/java/exceptions/DiscordException.java b/src/main/java/exceptions/DiscordException.java index dd3db5f0..bc80ceba 100644 --- a/src/main/java/exceptions/DiscordException.java +++ b/src/main/java/exceptions/DiscordException.java @@ -1,6 +1,6 @@ package exceptions; -import commands.model.Command; +import commands.model.LegacyCommand; import discord4j.core.object.entity.Message; import enums.Language; @@ -9,5 +9,5 @@ */ public interface DiscordException { - void throwException(Message message, Command command, Language lg, Object... arguments); + void throwException(Message message, LegacyCommand command, Language lg, Object... arguments); } diff --git a/src/main/java/exceptions/ExceptionManager.java b/src/main/java/exceptions/ExceptionManager.java index 44178826..f9e2a1ab 100644 --- a/src/main/java/exceptions/ExceptionManager.java +++ b/src/main/java/exceptions/ExceptionManager.java @@ -1,6 +1,6 @@ package exceptions; -import commands.model.Command; +import commands.model.LegacyCommand; import discord4j.core.object.entity.Message; import enums.Language; import org.jsoup.HttpStatusException; @@ -26,7 +26,7 @@ public abstract class ExceptionManager { private static DiscordException gameWebsite503 = new AdvancedDiscordException("exception.advanced.game_website_503", new String[]{"game.url"}, new Boolean[]{true}); - public static void manageIOException(Exception e, Message message, Command command, Language lg, DiscordException notFound){ + public static void manageIOException(Exception e, Message message, LegacyCommand command, Language lg, DiscordException notFound){ // First we try parsing the exception message to see if it contains the response code Matcher exMsgStatusCodeMatcher = Pattern.compile("^Server returned HTTP response code: (\\d+)") .matcher(e.getMessage()); @@ -75,7 +75,7 @@ public static void manageSilentlyIOException(Exception e){ } - public static void manageException(Exception e, Message message, Command command, Language lg){ + public static void manageException(Exception e, Message message, LegacyCommand command, Language lg){ LOG.error("manageException", e); BasicDiscordException.UNKNOWN_ERROR.throwException(message, command, lg); } diff --git a/src/main/java/exceptions/NotFoundDiscordException.java b/src/main/java/exceptions/NotFoundDiscordException.java index ebe7c78b..8691673c 100644 --- a/src/main/java/exceptions/NotFoundDiscordException.java +++ b/src/main/java/exceptions/NotFoundDiscordException.java @@ -4,7 +4,7 @@ import commands.classic.GuildCommand; import commands.classic.ItemCommand; import commands.classic.WhoisCommand; -import commands.model.Command; +import commands.model.LegacyCommand; import discord4j.core.object.entity.Message; import enums.AnkamaBug; import enums.Language; @@ -22,7 +22,7 @@ public NotFoundDiscordException(String objectKey){ } @Override - public void throwException(Message message, Command command, Language lg, Object... arguments) { + public void throwException(Message message, LegacyCommand command, Language lg, Object... arguments) { AnkamaBug bug = null; if (command instanceof WhoisCommand) bug = AnkamaBug.CHARACTER_NOT_FOUND; diff --git a/src/main/java/exceptions/TooMuchDiscordException.java b/src/main/java/exceptions/TooMuchDiscordException.java index 14780230..3018849d 100644 --- a/src/main/java/exceptions/TooMuchDiscordException.java +++ b/src/main/java/exceptions/TooMuchDiscordException.java @@ -3,7 +3,7 @@ import commands.classic.AllianceCommand; import commands.classic.GuildCommand; import commands.classic.WhoisCommand; -import commands.model.Command; +import commands.model.LegacyCommand; import discord4j.core.object.entity.Message; import enums.AnkamaBug; import enums.Language; @@ -31,7 +31,7 @@ public TooMuchDiscordException(String objectKey, boolean isTranslatable){ } @Override - public void throwException(Message message, Command command, Language lg, Object... arguments) { + public void throwException(Message message, LegacyCommand command, Language lg, Object... arguments) { AnkamaBug bug = null; String gender = Translator.getLabel(lg, "exception.object." + objectKey + ".gender"); diff --git a/src/main/java/exceptions/WrongBotUsedDiscordException.java b/src/main/java/exceptions/WrongBotUsedDiscordException.java index 60afa393..05144256 100644 --- a/src/main/java/exceptions/WrongBotUsedDiscordException.java +++ b/src/main/java/exceptions/WrongBotUsedDiscordException.java @@ -1,13 +1,12 @@ package exceptions; -import commands.model.Command; +import commands.model.LegacyCommand; import data.Constants; import data.ServerDofus; import discord4j.core.object.entity.Message; import enums.Language; import util.Translator; -import java.awt.*; import java.util.List; import java.util.stream.Collectors; @@ -17,7 +16,7 @@ public class WrongBotUsedDiscordException implements DiscordException { @Override - public void throwException(Message message, Command command, Language lg, Object... arguments) { + public void throwException(Message message, LegacyCommand command, Language lg, Object... arguments) { StringBuilder st = new StringBuilder(); List servers = (List) arguments[0]; servers.stream() diff --git a/src/main/java/listeners/MessageListener.java b/src/main/java/listeners/MessageListener.java index 10ee8e35..920f4b19 100644 --- a/src/main/java/listeners/MessageListener.java +++ b/src/main/java/listeners/MessageListener.java @@ -1,8 +1,8 @@ package listeners; import commands.*; -import commands.model.AbstractCommand; -import commands.model.Command; +import commands.model.AbstractLegacyCommand; +import commands.model.LegacyCommand; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.object.entity.User; import discord4j.core.object.entity.channel.MessageChannel; @@ -28,19 +28,19 @@ public Mono onReady(MessageCreateEvent event) { return event.getMessage().getChannel() .doOnSuccess(channel -> { Language lg = Translator.getLanguageFrom(channel); - String prefixe = AbstractCommand.getPrefix(event.getMessage()); + String prefixe = AbstractLegacyCommand.getPrefix(event.getMessage()); // If the authorId is a bot, message get ignored if (! event.getMessage().getAuthor().map(User::isBot).orElse(true)) { - List commandsAvailable = new ArrayList<>(); + List commandsAvailable = new ArrayList<>(); - for (Command command : CommandManager.getCommands()) + for (LegacyCommand command : CommandManager.getCommands()) if (event.getMessage().getContent().startsWith(prefixe + command.getName())) commandsAvailable.add(command); if (!commandsAvailable.isEmpty()){ commandsAvailable.sort((cmd1, cmd2) -> cmd2.getName().length() - cmd1.getName().length()); - Command command = commandsAvailable.get(0); + LegacyCommand command = commandsAvailable.get(0); try { command.request(event, event.getMessage()); diff --git a/src/main/java/listeners/SlashCommandListener.java b/src/main/java/listeners/SlashCommandListener.java new file mode 100644 index 00000000..39777324 --- /dev/null +++ b/src/main/java/listeners/SlashCommandListener.java @@ -0,0 +1,16 @@ +package listeners; + +import commands.CommandManager; +import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +public class SlashCommandListener { + + public Mono onReady(ChatInputInteractionEvent event) { + return Flux.fromIterable(CommandManager.getSlashCommands()) + .filter(command -> command.getName().equals(event.getCommandName())) + .next() + .flatMap(command -> command.handle(event)); + } +} diff --git a/src/main/java/util/ClientConfig.java b/src/main/java/util/ClientConfig.java index 3b1e9bf9..26f61822 100644 --- a/src/main/java/util/ClientConfig.java +++ b/src/main/java/util/ClientConfig.java @@ -1,23 +1,21 @@ package util; +import commands.CommandManager; import data.Constants; import discord4j.core.DiscordClient; import discord4j.core.GatewayDiscordClient; import discord4j.core.event.domain.guild.GuildCreateEvent; import discord4j.core.event.domain.guild.GuildDeleteEvent; import discord4j.core.event.domain.guild.GuildUpdateEvent; +import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; import discord4j.core.event.domain.lifecycle.ReadyEvent; import discord4j.core.event.domain.message.MessageCreateEvent; -import discord4j.core.object.presence.Activity; import discord4j.core.object.presence.ClientActivity; import discord4j.core.object.presence.ClientPresence; -import discord4j.core.object.presence.Presence; import discord4j.core.shard.MemberRequestFilter; import discord4j.gateway.intent.Intent; import discord4j.gateway.intent.IntentSet; -import finders.AlmanaxCalendar; -import finders.RSSFinder; -import finders.TwitterFinder; +import discord4j.rest.RestClient; import io.sentry.Sentry; import listeners.*; import org.slf4j.Logger; @@ -29,6 +27,7 @@ import java.io.*; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.Properties; /** @@ -55,7 +54,7 @@ private ClientConfig(String path){ Properties prop = new Properties(); String config = path + File.separator + FILENAME; - try (FileInputStream file = new FileInputStream(URLDecoder.decode(config, "UTF-8"))){ + try (FileInputStream file = new FileInputStream(URLDecoder.decode(config, StandardCharsets.UTF_8))){ prop.load(file); DOFUS_PORTALS_URL = prop.getProperty("dofus_portals.url"); @@ -114,6 +113,8 @@ public static DiscordClient DISCORD() { } public void loginDiscord(){ + registerSlashCommands(DISCORD()).block(); + DISCORD().gateway() .setEnabledIntents(IntentSet.of( Intent.GUILDS, @@ -121,14 +122,15 @@ public void loginDiscord(){ Intent.GUILD_MESSAGES, Intent.GUILD_MESSAGE_REACTIONS, Intent.DIRECT_MESSAGES)) - .setInitialPresence(ignored -> ClientPresence.online(ClientActivity.watching(Constants.discordInvite))) + .setInitialPresence($ -> ClientPresence.online(ClientActivity.watching(Constants.discordInvite))) .setMemberRequestFilter(MemberRequestFilter.none()) .withGateway(client -> Mono.when( readyListener(client), guildCreateListener(client), guildUpdateListener(client), guildDeleteListener(client), - commandListener(client))) + legacyCommandListener(client), + slashCommandListener(client))) .block(); } @@ -154,13 +156,30 @@ public static synchronized ClientConfig getInstance(String path){ return instance; } - private Mono commandListener(GatewayDiscordClient client){ + private Mono registerSlashCommands(RestClient client){ + return client.getApplicationId() + .flatMap(id -> client.getApplicationService() + .bulkOverwriteGlobalApplicationCommand(id, CommandManager.getSlashCommandRequests()) + .doOnNext(cmd -> LOG.info("Successfully registered Global Command " + cmd.name())) + .collectList() + .then() + .doOnError(e -> LOG.error("Failed to register global commands", e))); + } + + private Mono legacyCommandListener(GatewayDiscordClient client){ final MessageListener listener = new MessageListener(); return client.getEventDispatcher().on(MessageCreateEvent.class) .flatMap(listener::onReady) .then(); } + private Mono slashCommandListener(GatewayDiscordClient client){ + final SlashCommandListener listener = new SlashCommandListener(); + return client.getEventDispatcher().on(ChatInputInteractionEvent.class) + .flatMap(listener::onReady) + .then(); + } + private Mono guildCreateListener(GatewayDiscordClient client){ final GuildCreateListener listener = new GuildCreateListener(); return client.getEventDispatcher().on(GuildCreateEvent.class) diff --git a/src/main/java/util/Translator.java b/src/main/java/util/Translator.java index e2113f6e..fb9c033f 100644 --- a/src/main/java/util/Translator.java +++ b/src/main/java/util/Translator.java @@ -15,7 +15,6 @@ import discord4j.core.object.entity.Message; import discord4j.core.object.entity.channel.GuildMessageChannel; import discord4j.core.object.entity.channel.MessageChannel; -import discord4j.core.object.entity.channel.TextChannel; import discord4j.discordjson.json.MessageData; import discord4j.rest.entity.RestChannel; import enums.Language; @@ -28,6 +27,7 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Created by steve on 06/06/2017. @@ -86,6 +86,13 @@ public static Language getLanguageFrom(RestChannel channel) { return result; } + public static Language mapLocale(String locale){ + return Stream.of(Language.values()) + .filter(lg -> lg.getLocales().contains(locale)) + .findFirst() + .orElse(Constants.defaultLanguage); + } + /** * Génère une liste de source formatée à partir d'un salon textuel * @param channel Salon d'origine diff --git a/src/test/java/CommandPatternTest.java b/src/test/java/CommandPatternTest.java index c9372245..e06cdf87 100644 --- a/src/test/java/CommandPatternTest.java +++ b/src/test/java/CommandPatternTest.java @@ -1,7 +1,7 @@ import commands.admin.*; import commands.classic.*; import commands.config.*; -import commands.model.Command; +import commands.model.LegacyCommand; import data.Constants; import org.junit.jupiter.api.Test; @@ -19,7 +19,7 @@ class CommandPatternTest { @Test void testAlmanaxCommand(){ - Command cmd = new AlmanaxCommand(); + LegacyCommand cmd = new AlmanaxCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "almanax").find()); @@ -29,7 +29,7 @@ void testAlmanaxCommand(){ @Test void testAlmanaxAutoCommand(){ - Command cmd = new AlmanaxAutoCommand(); + LegacyCommand cmd = new AlmanaxAutoCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "almanax-auto true").find()); @@ -42,7 +42,7 @@ void testAlmanaxAutoCommand(){ @Test void testAlignmentCommand(){ - Command cmd = new AlignmentCommand(); + LegacyCommand cmd = new AlignmentCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "align").find()); @@ -57,7 +57,7 @@ void testAlignmentCommand(){ @Test void testAllianceCommand(){ - Command cmd = new AllianceCommand(); + LegacyCommand cmd = new AllianceCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "alliance La Fratrie des Oublies").find()); @@ -69,7 +69,7 @@ void testAllianceCommand(){ @Test void testAboutCommand(){ - Command cmd = new AboutCommand(); + LegacyCommand cmd = new AboutCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "about").find()); @@ -77,7 +77,7 @@ void testAboutCommand(){ @Test void testCommandCommand(){ - Command cmd = new CommandCommand(); + LegacyCommand cmd = new CommandCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "cmd CommandForbidden true").find()); @@ -93,7 +93,7 @@ void testCommandCommand(){ @Test void testDonateCommand(){ - Command cmd = new DonateCommand(); + LegacyCommand cmd = new DonateCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "donate").find()); @@ -101,7 +101,7 @@ void testDonateCommand(){ @Test void testDistCommand(){ - Command cmd = new DistanceCommand(); + LegacyCommand cmd = new DistanceCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "dist [20,20]").find()); @@ -113,7 +113,7 @@ void testDistCommand(){ @Test void testGuildCommand(){ - Command cmd = new GuildCommand(); + LegacyCommand cmd = new GuildCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "guild La Feuille Verte").find()); @@ -125,7 +125,7 @@ void testGuildCommand(){ @Test void testHelpCommand(){ - Command cmd = new HelpCommand(); + LegacyCommand cmd = new HelpCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "help").find()); @@ -137,7 +137,7 @@ void testHelpCommand(){ @Test void testInviteCommand(){ - Command cmd = new InviteCommand(); + LegacyCommand cmd = new InviteCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "invite").find()); @@ -145,7 +145,7 @@ void testInviteCommand(){ @Test void testItemCommand(){ - Command cmd = new ItemCommand(); + LegacyCommand cmd = new ItemCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "item test").find()); @@ -156,7 +156,7 @@ void testItemCommand(){ @Test void testJobCommand(){ - Command cmd = new JobCommand(); + LegacyCommand cmd = new JobCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "job").find()); @@ -169,7 +169,7 @@ void testJobCommand(){ @Test void testLangCommand(){ - Command cmd = new LanguageCommand(); + LegacyCommand cmd = new LanguageCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "lang FR").find()); @@ -179,7 +179,7 @@ void testLangCommand(){ @Test void testMapCommand(){ - Command cmd = new MapCommand(); + LegacyCommand cmd = new MapCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "map").find()); @@ -192,7 +192,7 @@ void testMapCommand(){ @Test void testMonsterCommand(){ - Command cmd = new MonsterCommand(); + LegacyCommand cmd = new MonsterCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "monster test").find()); @@ -203,7 +203,7 @@ void testMonsterCommand(){ @Test void testPortalCommand(){ - Command cmd = new PortalCommand(); + LegacyCommand cmd = new PortalCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "pos").find()); @@ -213,7 +213,7 @@ void testPortalCommand(){ @Test void testTutorialCommand(){ - Command cmd = new TutorialCommand(); + LegacyCommand cmd = new TutorialCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "tuto test").find()); @@ -223,7 +223,7 @@ void testTutorialCommand(){ @Test void testTwitterCommand(){ - Command cmd = new TwitterCommand(); + LegacyCommand cmd = new TwitterCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "twitter true").find()); @@ -237,7 +237,7 @@ void testTwitterCommand(){ @Test void testRSSCommand(){ - Command cmd = new RSSCommand(); + LegacyCommand cmd = new RSSCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "rss true").find()); @@ -251,7 +251,7 @@ void testRSSCommand(){ @Test void testSetCommand(){ - Command cmd = new SetCommand(); + LegacyCommand cmd = new SetCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "set test").find()); @@ -262,7 +262,7 @@ void testSetCommand(){ @Test void testServerCommand(){ - Command cmd = new ServerCommand(); + LegacyCommand cmd = new ServerCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "server").find()); @@ -275,7 +275,7 @@ void testServerCommand(){ @Test void testResourceCommand(){ - Command cmd = new ResourceCommand(); + LegacyCommand cmd = new ResourceCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "resource test").find()); @@ -286,7 +286,7 @@ void testResourceCommand(){ @Test void testRandomCommand(){ - Command cmd = new RandomCommand(); + LegacyCommand cmd = new RandomCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "rdm").find()); @@ -298,7 +298,7 @@ void testRandomCommand(){ @Test void testWhoisCommand(){ - Command cmd = new WhoisCommand(); + LegacyCommand cmd = new WhoisCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "whois test").find()); @@ -311,7 +311,7 @@ void testWhoisCommand(){ @Test void testPingCommand(){ - Command cmd = new PingCommand(); + LegacyCommand cmd = new PingCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "ping").find()); @@ -319,7 +319,7 @@ void testPingCommand(){ @Test void testPrefixeCommand(){ - Command cmd = new PrefixCommand(); + LegacyCommand cmd = new PrefixCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertFalse(pattern.matcher(Constants.prefixCommand + "prefix").find()); @@ -329,7 +329,7 @@ void testPrefixeCommand(){ @Test void testStatCommand(){ - Command cmd = new StatCommand(); + LegacyCommand cmd = new StatCommand(); Pattern pattern = Pattern.compile("^" + Constants.prefixCommand + cmd.getName() + cmd.getPattern() + "$"); assertTrue(pattern.matcher(Constants.prefixCommand + "stats").find());