Skip to content

Commit

Permalink
/about added
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaysoro committed Nov 15, 2022
1 parent c53674d commit a78af4e
Show file tree
Hide file tree
Showing 47 changed files with 300 additions and 190 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.11</maven.compiler.source>
<maven.compiler.target>1.11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<build>
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/commands/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,13 +21,15 @@ public class CommandManager {

private static CommandManager instance;

private List<Command> commands;
private Map<String, Command> mapCommands;
private final List<SlashCommand> slashCommands;
private final List<LegacyCommand> commands;
private final Map<String, LegacyCommand> mapCommands;

private CommandManager(){
super();
mapCommands = new ConcurrentHashMap<>();
commands = new CopyOnWriteArrayList<>();
slashCommands = new CopyOnWriteArrayList<>();

// Basics commands
addCommand(new AboutCommand());
Expand Down Expand Up @@ -68,16 +73,30 @@ public static CommandManager getInstance(){
return instance;
}

public static List<Command> getCommands(){
public static List<SlashCommand> getSlashCommands(){
return getInstance().slashCommands;
}

public static List<ApplicationCommandRequest> getSlashCommandRequests(){
return getInstance().slashCommands.stream()
.map(SlashCommand::getCommandRequest)
.collect(Collectors.toList());
}

public static List<LegacyCommand> 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);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/commands/admin/StatCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand Down
124 changes: 76 additions & 48 deletions src/main/java/commands/classic/AboutCommand.java
Original file line number Diff line number Diff line change
@@ -1,86 +1,114 @@
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", "");
}

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
Optional<ApplicationInfo> appInfo = message.getClient().getApplicationInfo().blockOptional();
message.getChannel()
.flatMap(chan -> chan.createMessage(aboutEmbed(message.getClient(), lg)))
.subscribe();
}

@Override
public Mono<Void> 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<ApplicationInfo> 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);
}
}
4 changes: 2 additions & 2 deletions src/main/java/commands/classic/AllianceCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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[]=";
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/commands/classic/AlmanaxCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)?");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/commands/classic/DistanceCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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})\\]?");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/commands/classic/DonateCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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", "");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/commands/classic/GuildCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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[]=";
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/commands/classic/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";

Expand All @@ -38,7 +38,7 @@ public void request(MessageCreateEvent event, Message message, Matcher m, Langua
List<String> 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())))){
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/commands/classic/InviteCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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", "");
Expand Down
Loading

0 comments on commit a78af4e

Please sign in to comment.