1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-09-19 21:04:02 +00:00

Add about command, add friendly names to ModuleKey

This commit is contained in:
sciwhiz12 2020-11-28 17:35:24 +08:00
parent d214af3eca
commit eebb101603
Signed by: sciwhiz12
GPG Key ID: 622CF446534317E1
8 changed files with 74 additions and 3 deletions

View File

@ -3,16 +3,19 @@ package sciwhiz12.janitor.api.module;
import com.google.common.base.Preconditions;
import java.util.Objects;
import javax.annotation.Nullable;
public class ModuleKey<M extends Module> {
private final String moduleID;
private final String moduleName;
private final Class<M> type;
public ModuleKey(String storageID, Class<M> type) {
public ModuleKey(String storageID, @Nullable String moduleName, Class<M> type) {
Preconditions.checkNotNull(storageID, "Module ID must not be null");
Preconditions.checkArgument(!storageID.isBlank(), "Module ID must not be empty or blank");
Preconditions.checkNotNull(type, "Class type must not be null");
this.moduleID = storageID;
this.moduleName = Objects.requireNonNullElse(moduleName, moduleID);
this.type = type;
}
@ -20,6 +23,10 @@ public class ModuleKey<M extends Module> {
return moduleID;
}
public String getModuleName() {
return moduleName;
}
public Class<M> getType() {
return type;
}

View File

@ -44,6 +44,7 @@ public final class MessageHelper {
.with(head + ".name", user::getName)
.with(head + ".discriminator", user::getDiscriminator)
.with(head + ".tag", user::getAsTag)
.with(head + ".avatar", user::getAvatarUrl)
.with(head + ".flags", user.getFlags()::toString);
}

View File

@ -3,6 +3,8 @@ package sciwhiz12.janitor.commands;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.dv8tion.jda.api.events.GenericEvent;
@ -13,6 +15,7 @@ import sciwhiz12.janitor.JanitorBotImpl;
import sciwhiz12.janitor.api.command.Command;
import sciwhiz12.janitor.api.command.CommandRegistry;
import sciwhiz12.janitor.api.config.CoreConfigs;
import sciwhiz12.janitor.commands.bot.AboutCommand;
import sciwhiz12.janitor.commands.bot.ShutdownCommand;
import sciwhiz12.janitor.commands.misc.HelloCommand;
import sciwhiz12.janitor.commands.misc.OKCommand;
@ -25,6 +28,8 @@ import java.util.function.Function;
import static sciwhiz12.janitor.api.Logging.COMMANDS;
import static sciwhiz12.janitor.api.Logging.JANITOR;
import static sciwhiz12.janitor.api.utils.CommandHelper.argument;
import static sciwhiz12.janitor.api.utils.CommandHelper.literal;
public class CommandRegistryImpl implements CommandRegistry, EventListener {
private final JanitorBotImpl bot;
@ -40,6 +45,7 @@ public class CommandRegistryImpl implements CommandRegistry, EventListener {
addCommand(OKCommand::new);
addCommand(HelloCommand::new);
addCommand(ShutdownCommand::new);
addCommand(AboutCommand::new);
addCommand((reg) -> new CmdListCommand((CommandRegistryImpl) reg));
}

View File

@ -0,0 +1,43 @@
package sciwhiz12.janitor.commands.bot;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import sciwhiz12.janitor.JanitorBotImpl;
import sciwhiz12.janitor.api.command.BaseCommand;
import sciwhiz12.janitor.api.command.CommandRegistry;
import sciwhiz12.janitor.api.module.ModuleKey;
import sciwhiz12.janitor.api.utils.MessageHelper;
import java.util.Objects;
import java.util.stream.Collectors;
import static sciwhiz12.janitor.api.config.CoreConfigs.COMMAND_PREFIX;
import static sciwhiz12.janitor.api.utils.CommandHelper.literal;
public class AboutCommand extends BaseCommand {
public AboutCommand(CommandRegistry registry) {
super(registry);
}
@Override
public LiteralArgumentBuilder<MessageReceivedEvent> getNode() {
return literal("about")
.executes(ctx -> {
Package pkg = JanitorBotImpl.class.getPackage();
messages().getRegularMessage("general/about")
.with("bot.name", () -> Objects.requireNonNullElse(pkg.getImplementationTitle(), "Janitor"))
.with("bot.version", () -> Objects.requireNonNullElse(pkg.getImplementationVersion(), "in-dev"))
.with("bot.guild_count", () -> String.valueOf(getBot().getDiscord().getGuilds().size()))
.with("bot.modules.count", () -> String.valueOf(getBot().getModuleManager().getActiveModules().size()))
.with("bot.modules.names",
() -> getBot().getModuleManager().getActiveModules().stream()
.map(ModuleKey::getModuleName)
.collect(Collectors.joining(", "))
)
.apply(MessageHelper.member("bot.member", ctx.getSource().getGuild().getSelfMember()))
.with("guild.command_prefix", () -> config(ctx.getSource()).forGuild(COMMAND_PREFIX))
.send(getBot(), ctx.getSource().getChannel()).queue();
return 1;
});
}
}

View File

@ -19,6 +19,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.annotation.Nullable;
public class RegularMessageBuilder implements RegularMessage.Builder<RegularMessageBuilder> {
private final RegularMessage message;
@ -74,7 +75,7 @@ public class RegularMessageBuilder implements RegularMessage.Builder<RegularMess
return builder;
}
private static int parseColor(String str) {
private static int parseColor(@Nullable String str) {
if (Strings.isNullOrEmpty(str)) return Role.DEFAULT_COLOR_RAW;
if (str.startsWith("0x")) {
// noinspection UnstableApiUsage

View File

@ -0,0 +1,12 @@
{
"title": "${bot.member.name}",
"thumbnail": "${bot.member.avatar}",
"description": "A general and extensible housekeeping bot. Created by SciWhiz12.\n_Use `${guild.command_prefix}commands` to get the list of enabled commands in this guild._\n\nRunning **${bot.name}**, version **${bot.version}**. Serving **${bot.guild_count}** guilds.",
"fields": [
{
"name": "Active Modules (${bot.modules.count})",
"value": "${bot.modules.names}",
"inline": true
}
]
}

View File

@ -1,4 +1,5 @@
[
"general/about",
"general/commands_listing",
"general/error/ambiguous_member",
"general/error/guild_only_command",

View File

@ -10,7 +10,7 @@ import sciwhiz12.janitor.moderation.warns.WarningStorage;
public interface ModerationModule extends Module {
Logger LOGGER = LoggerFactory.getLogger("janitor.moderation");
ModuleKey<ModerationModule> ID = new ModuleKey<>("moderation", ModerationModule.class);
ModuleKey<ModerationModule> ID = new ModuleKey<>("moderation", "Moderation", ModerationModule.class);
NoteStorage getNotes(Guild guild);