From b34b628613e85c3316f94727afb4be122cb3dee3 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 27 Jun 2024 19:22:17 -0500 Subject: [PATCH 01/10] Add ServerIconCommand --- .../impl/server/ServerIconCommand.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/cc/fascinated/bat/command/impl/server/ServerIconCommand.java diff --git a/src/main/java/cc/fascinated/bat/command/impl/server/ServerIconCommand.java b/src/main/java/cc/fascinated/bat/command/impl/server/ServerIconCommand.java new file mode 100644 index 0000000..0dae6ee --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/server/ServerIconCommand.java @@ -0,0 +1,27 @@ +package cc.fascinated.bat.command.impl.server; + +import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.CommandInfo; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import lombok.NonNull; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component +@CommandInfo(name = "servericon", description = "View the icon of the server!") +public class ServerIconCommand extends BatCommand { + @Override + public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { + EmbedBuilder embed = EmbedUtils.genericEmbed().setAuthor("Server icon for " + guild.getName()); + embed.setImage(guild.getDiscordGuild().getIconUrl()); + interaction.replyEmbeds(embed.build()).queue(); + } +} \ No newline at end of file From 306edf7017dcabe3c40954daa6b65454e790bec9 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 27 Jun 2024 19:25:33 -0500 Subject: [PATCH 02/10] Add MemberCountCommand --- .../impl/server/MemberCountCommand.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/java/cc/fascinated/bat/command/impl/server/MemberCountCommand.java diff --git a/src/main/java/cc/fascinated/bat/command/impl/server/MemberCountCommand.java b/src/main/java/cc/fascinated/bat/command/impl/server/MemberCountCommand.java new file mode 100644 index 0000000..43f9d4e --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/server/MemberCountCommand.java @@ -0,0 +1,29 @@ +package cc.fascinated.bat.command.impl.server; + +import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.CommandInfo; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import lombok.NonNull; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component +@CommandInfo(name = "membercount", description = "View the member count of the server!") +public class MemberCountCommand extends BatCommand { + @Override + public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { + EmbedBuilder embed = EmbedUtils.genericEmbed().setAuthor("Member Count"); + Guild discordGuild = guild.getDiscordGuild(); + embed.setDescription(discordGuild.getName() + " has a total of " + discordGuild.getMembers().size() + " members."); + interaction.replyEmbeds(embed.build()).queue(); + } +} \ No newline at end of file From 886755cd408191619444ef107ccdc17f1a2ed7ef Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 27 Jun 2024 19:57:42 -0500 Subject: [PATCH 03/10] Add BannerCommand and add guild + user sub commands to AvatarCommand --- .../bat/command/impl/HelpCommand.java | 1 - .../command/impl/avatar/AvatarCommand.java | 21 +++++++ .../command/impl/avatar/GuildSubCommand.java | 39 +++++++++++++ .../UserSubCommand.java} | 23 ++++---- .../command/impl/banner/BannerCommand.java | 21 +++++++ .../command/impl/banner/GuildSubCommand.java | 39 +++++++++++++ .../command/impl/banner/UserSubCommand.java | 56 +++++++++++++++++++ .../command/impl/server/PremiumCommand.java | 2 - .../cc/fascinated/bat/model/BatGuild.java | 2 - .../fascinated/bat/service/UserService.java | 1 - 10 files changed, 189 insertions(+), 16 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/command/impl/avatar/AvatarCommand.java create mode 100644 src/main/java/cc/fascinated/bat/command/impl/avatar/GuildSubCommand.java rename src/main/java/cc/fascinated/bat/command/impl/{AvatarCommand.java => avatar/UserSubCommand.java} (66%) create mode 100644 src/main/java/cc/fascinated/bat/command/impl/banner/BannerCommand.java create mode 100644 src/main/java/cc/fascinated/bat/command/impl/banner/GuildSubCommand.java create mode 100644 src/main/java/cc/fascinated/bat/command/impl/banner/UserSubCommand.java diff --git a/src/main/java/cc/fascinated/bat/command/impl/HelpCommand.java b/src/main/java/cc/fascinated/bat/command/impl/HelpCommand.java index de68216..7103939 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/HelpCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/HelpCommand.java @@ -28,7 +28,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Map; diff --git a/src/main/java/cc/fascinated/bat/command/impl/avatar/AvatarCommand.java b/src/main/java/cc/fascinated/bat/command/impl/avatar/AvatarCommand.java new file mode 100644 index 0000000..3af8dac --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/avatar/AvatarCommand.java @@ -0,0 +1,21 @@ +package cc.fascinated.bat.command.impl.avatar; + +import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.CommandInfo; +import lombok.NonNull; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component +@CommandInfo(name = "avatar", description = "View the avatar of the guild or a user") +public class AvatarCommand extends BatCommand { + @Autowired + public AvatarCommand(@NonNull ApplicationContext context) { + super.addSubCommand(context.getBean(GuildSubCommand.class)); + super.addSubCommand(context.getBean(UserSubCommand.class)); + } +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/command/impl/avatar/GuildSubCommand.java b/src/main/java/cc/fascinated/bat/command/impl/avatar/GuildSubCommand.java new file mode 100644 index 0000000..032b5e3 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/avatar/GuildSubCommand.java @@ -0,0 +1,39 @@ +package cc.fascinated.bat.command.impl.avatar; + +import cc.fascinated.bat.command.BatSubCommand; +import cc.fascinated.bat.command.CommandInfo; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import lombok.NonNull; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import net.dv8tion.jda.api.utils.ImageProxy; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component("avatar:guild.sub") +@CommandInfo(name = "guild", description = "View the avatar of the guild") +public class GuildSubCommand extends BatSubCommand { + @Override + public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { + ImageProxy icon = guild.getDiscordGuild().getIcon(); + + if (icon == null) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("%s does not have an avatar!".formatted(guild.getName())) + .build()) + .queue(); + return; + } + + interaction.replyEmbeds(EmbedUtils.genericEmbed() + .setAuthor("%s's Avatar".formatted(guild.getName()), null, guild.getDiscordGuild().getIconUrl()) + .setImage(icon.getUrl(4096)) + .build() + ).queue(); + } +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/command/impl/AvatarCommand.java b/src/main/java/cc/fascinated/bat/command/impl/avatar/UserSubCommand.java similarity index 66% rename from src/main/java/cc/fascinated/bat/command/impl/AvatarCommand.java rename to src/main/java/cc/fascinated/bat/command/impl/avatar/UserSubCommand.java index 49f568b..3a5cc99 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/AvatarCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/avatar/UserSubCommand.java @@ -1,6 +1,6 @@ -package cc.fascinated.bat.command.impl; +package cc.fascinated.bat.command.impl.avatar; -import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.model.BatGuild; @@ -15,20 +15,23 @@ import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import org.springframework.stereotype.Component; /** - * @author Fascinated (fascinated7) + * @author Nick (okNick) */ -@Component -@CommandInfo(name = "avatar", description = "Get the avatar of a user", guildOnly = false) -public class AvatarCommand extends BatCommand { - public AvatarCommand() { - super.addOption(OptionType.USER, "user", "The user to get the avatar of", true); +@Component("avatar:user.sub") +@CommandInfo(name = "user", description = "View the avatar of a user", guildOnly = false) +public class UserSubCommand extends BatSubCommand { + public UserSubCommand() { + super.addOption(OptionType.USER, "user", "The user to view the avatar of", true); } @Override public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { OptionMapping userOption = interaction.getOption("user"); if (userOption == null) { - interaction.reply("You must provide a user to get the avatar of!").queue(); + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("You must provide a user to view the avatar of!") + .build()) + .queue(); return; } @@ -39,4 +42,4 @@ public class AvatarCommand extends BatCommand { .build() ).queue(); } -} +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/command/impl/banner/BannerCommand.java b/src/main/java/cc/fascinated/bat/command/impl/banner/BannerCommand.java new file mode 100644 index 0000000..6603ea0 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/banner/BannerCommand.java @@ -0,0 +1,21 @@ +package cc.fascinated.bat.command.impl.banner; + +import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.CommandInfo; +import lombok.NonNull; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component +@CommandInfo(name = "banner", description = "View the banner of the guild or a user") +public class BannerCommand extends BatCommand { + @Autowired + public BannerCommand(@NonNull ApplicationContext context) { + super.addSubCommand(context.getBean(GuildSubCommand.class)); + super.addSubCommand(context.getBean(UserSubCommand.class)); + } +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/command/impl/banner/GuildSubCommand.java b/src/main/java/cc/fascinated/bat/command/impl/banner/GuildSubCommand.java new file mode 100644 index 0000000..617e487 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/banner/GuildSubCommand.java @@ -0,0 +1,39 @@ +package cc.fascinated.bat.command.impl.banner; + +import cc.fascinated.bat.command.BatSubCommand; +import cc.fascinated.bat.command.CommandInfo; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import lombok.NonNull; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import net.dv8tion.jda.api.utils.ImageProxy; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component("banner:guild.sub") +@CommandInfo(name = "guild", description = "View the banner of the guild") +public class GuildSubCommand extends BatSubCommand { + @Override + public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { + ImageProxy banner = guild.getDiscordGuild().getBanner(); + + if (banner == null) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("%s does not have a banner!".formatted(guild.getName())) + .build()) + .queue(); + return; + } + + interaction.replyEmbeds(EmbedUtils.genericEmbed() + .setAuthor("%s's Banner".formatted(guild.getName())) + .setImage(banner.getUrl(512)) + .build() + ).queue(); + } +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/command/impl/banner/UserSubCommand.java b/src/main/java/cc/fascinated/bat/command/impl/banner/UserSubCommand.java new file mode 100644 index 0000000..08dfc65 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/banner/UserSubCommand.java @@ -0,0 +1,56 @@ +package cc.fascinated.bat.command.impl.banner; + +import cc.fascinated.bat.command.BatSubCommand; +import cc.fascinated.bat.command.CommandInfo; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import lombok.NonNull; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import net.dv8tion.jda.api.utils.ImageProxy; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component("banner:user.sub") +@CommandInfo(name = "user", description = "View the banner of a user", guildOnly = false) +public class UserSubCommand extends BatSubCommand { + public UserSubCommand() { + super.addOption(OptionType.USER, "user", "The user to view the banner of", true); + } + + @Override + public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { + OptionMapping userOption = interaction.getOption("user"); + if (userOption == null) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("You must provide a user to view the banner of!") + .build()) + .queue(); + return; + } + + User target = userOption.getAsUser(); + ImageProxy banner = target.retrieveProfile().complete().getBanner(); + + if (banner == null) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("%s does not have a banner!".formatted(target.getName())) + .build()) + .queue(); + return; + } + + interaction.replyEmbeds(EmbedUtils.genericEmbed() + .setAuthor("%s's Banner".formatted(target.getName())) + .setImage(banner.getUrl(512)) + .build() + ).queue(); + } +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/command/impl/server/PremiumCommand.java b/src/main/java/cc/fascinated/bat/command/impl/server/PremiumCommand.java index 3b6f13d..6939f19 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/server/PremiumCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/server/PremiumCommand.java @@ -3,14 +3,12 @@ package cc.fascinated.bat.command.impl.server; import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.common.EmbedUtils; -import cc.fascinated.bat.common.TimeUtils; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import lombok.NonNull; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import org.springframework.stereotype.Component; diff --git a/src/main/java/cc/fascinated/bat/model/BatGuild.java b/src/main/java/cc/fascinated/bat/model/BatGuild.java index fe8b769..aca4f48 100644 --- a/src/main/java/cc/fascinated/bat/model/BatGuild.java +++ b/src/main/java/cc/fascinated/bat/model/BatGuild.java @@ -2,12 +2,10 @@ package cc.fascinated.bat.model; import cc.fascinated.bat.common.ProfileHolder; import cc.fascinated.bat.service.DiscordService; -import jakarta.annotation.PostConstruct; import lombok.*; import net.dv8tion.jda.api.entities.Guild; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; -import org.springframework.stereotype.Component; import java.util.Calendar; import java.util.Date; diff --git a/src/main/java/cc/fascinated/bat/service/UserService.java b/src/main/java/cc/fascinated/bat/service/UserService.java index 1297c7d..92ace4a 100644 --- a/src/main/java/cc/fascinated/bat/service/UserService.java +++ b/src/main/java/cc/fascinated/bat/service/UserService.java @@ -10,7 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Service; -import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.concurrent.TimeUnit; From 34d92d7bf89804912254bf5fc459534c5eeaa899 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 27 Jun 2024 19:58:09 -0500 Subject: [PATCH 04/10] Ember -> Bat --- src/main/java/cc/fascinated/bat/BatApplication.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/cc/fascinated/bat/BatApplication.java b/src/main/java/cc/fascinated/bat/BatApplication.java index 8782999..e79af7e 100644 --- a/src/main/java/cc/fascinated/bat/BatApplication.java +++ b/src/main/java/cc/fascinated/bat/BatApplication.java @@ -16,7 +16,7 @@ import java.util.Objects; @EnableScheduling @SpringBootApplication -@Log4j2(topic = "Ember") +@Log4j2(topic = "Bat") public class BatApplication { public static Gson GSON = new GsonBuilder().create(); From bd3774fab00fb421612ccddc941e43ea40375b0f Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 27 Jun 2024 19:59:59 -0500 Subject: [PATCH 05/10] Remove ServerIconCommand --- .../impl/server/ServerIconCommand.java | 27 ------------------- 1 file changed, 27 deletions(-) delete mode 100644 src/main/java/cc/fascinated/bat/command/impl/server/ServerIconCommand.java diff --git a/src/main/java/cc/fascinated/bat/command/impl/server/ServerIconCommand.java b/src/main/java/cc/fascinated/bat/command/impl/server/ServerIconCommand.java deleted file mode 100644 index 0dae6ee..0000000 --- a/src/main/java/cc/fascinated/bat/command/impl/server/ServerIconCommand.java +++ /dev/null @@ -1,27 +0,0 @@ -package cc.fascinated.bat.command.impl.server; - -import cc.fascinated.bat.command.BatCommand; -import cc.fascinated.bat.command.CommandInfo; -import cc.fascinated.bat.common.EmbedUtils; -import cc.fascinated.bat.model.BatGuild; -import cc.fascinated.bat.model.BatUser; -import lombok.NonNull; -import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; -import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; -import org.springframework.stereotype.Component; - -/** - * @author Nick (okNick) - */ -@Component -@CommandInfo(name = "servericon", description = "View the icon of the server!") -public class ServerIconCommand extends BatCommand { - @Override - public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { - EmbedBuilder embed = EmbedUtils.genericEmbed().setAuthor("Server icon for " + guild.getName()); - embed.setImage(guild.getDiscordGuild().getIconUrl()); - interaction.replyEmbeds(embed.build()).queue(); - } -} \ No newline at end of file From 9a1d011ff2853c05181e838d507842220c50729b Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 27 Jun 2024 20:41:38 -0500 Subject: [PATCH 06/10] Add ChannelCommand with topic sub commands --- .../impl/server/channel/ChannelCommand.java | 22 ++++++++ .../server/channel/RemoveTopicSubCommand.java | 53 ++++++++++++++++++ .../server/channel/SetTopicSubCommand.java | 55 +++++++++++++++++++ .../server/channel/ViewTopicSubCommand.java | 52 ++++++++++++++++++ .../bat/service/CommandService.java | 11 ++-- 5 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/command/impl/server/channel/ChannelCommand.java create mode 100644 src/main/java/cc/fascinated/bat/command/impl/server/channel/RemoveTopicSubCommand.java create mode 100644 src/main/java/cc/fascinated/bat/command/impl/server/channel/SetTopicSubCommand.java create mode 100644 src/main/java/cc/fascinated/bat/command/impl/server/channel/ViewTopicSubCommand.java diff --git a/src/main/java/cc/fascinated/bat/command/impl/server/channel/ChannelCommand.java b/src/main/java/cc/fascinated/bat/command/impl/server/channel/ChannelCommand.java new file mode 100644 index 0000000..65f5193 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/server/channel/ChannelCommand.java @@ -0,0 +1,22 @@ +package cc.fascinated.bat.command.impl.server.channel; + +import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.CommandInfo; +import lombok.NonNull; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component +@CommandInfo(name = "channel", description = "View or set information about a channel") +public class ChannelCommand extends BatCommand { + @Autowired + public ChannelCommand(@NonNull ApplicationContext context) { + super.addSubCommand(context.getBean(ViewTopicSubCommand.class)); + super.addSubCommand(context.getBean(SetTopicSubCommand.class)); + super.addSubCommand(context.getBean(RemoveTopicSubCommand.class)); + } +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/command/impl/server/channel/RemoveTopicSubCommand.java b/src/main/java/cc/fascinated/bat/command/impl/server/channel/RemoveTopicSubCommand.java new file mode 100644 index 0000000..9735985 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/server/channel/RemoveTopicSubCommand.java @@ -0,0 +1,53 @@ +package cc.fascinated.bat.command.impl.server.channel; + +import cc.fascinated.bat.command.BatSubCommand; +import cc.fascinated.bat.command.CommandInfo; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import lombok.NonNull; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.channel.Channel; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component +@CommandInfo(name = "removetopic", description = "Remove the topic of a channel", requiredPermissions = Permission.MANAGE_CHANNEL) +public class RemoveTopicSubCommand extends BatSubCommand { + public RemoveTopicSubCommand() { + super.addOption(OptionType.CHANNEL, "channel", "The channel to remove the topic of", false); + } + + @Override + public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { + Channel target = interaction.getOption("channel") == null ? channel : interaction.getOption("channel").getAsChannel(); + if (!(target instanceof TextChannel textChannel)) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("<#%s> is not a text channel!".formatted(target.getId())) + .build()) + .queue(); + return; + } + + if (textChannel.getTopic() == null) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("<#%s> does not have a topic!".formatted(textChannel.getId())) + .build()) + .queue(); + return; + } + + textChannel.getManager().setTopic(null).queue(); + interaction.replyEmbeds(EmbedUtils.successEmbed() + .setDescription("Successfully removed the topic of <#%s>".formatted(textChannel.getId())) + .build() + ).queue(); + } +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/command/impl/server/channel/SetTopicSubCommand.java b/src/main/java/cc/fascinated/bat/command/impl/server/channel/SetTopicSubCommand.java new file mode 100644 index 0000000..7a86419 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/server/channel/SetTopicSubCommand.java @@ -0,0 +1,55 @@ +package cc.fascinated.bat.command.impl.server.channel; + +import cc.fascinated.bat.command.BatSubCommand; +import cc.fascinated.bat.command.CommandInfo; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import lombok.NonNull; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.channel.Channel; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component +@CommandInfo(name = "settopic", description = "Set the topic of a channel", requiredPermissions = Permission.MANAGE_CHANNEL) +public class SetTopicSubCommand extends BatSubCommand { + public SetTopicSubCommand() { + super.addOption(OptionType.STRING, "topic", "The topic to set", true); + super.addOption(OptionType.CHANNEL, "channel", "The channel to set the topic of", false); + } + + @Override + public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { + Channel target = interaction.getOption("channel") == null ? channel : interaction.getOption("channel").getAsChannel(); + if (!(target instanceof TextChannel textChannel)) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("<#%s> is not a text channel!".formatted(target.getId())) + .build()) + .queue(); + return; + } + + String topic = interaction.getOption("topic").getAsString(); + if (topic.length() > 1024) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("The topic must be less than 1024 characters!") + .build()) + .queue(); + return; + } + + textChannel.getManager().setTopic(topic).queue(); + interaction.replyEmbeds(EmbedUtils.successEmbed() + .setDescription("Successfully set the topic of <#%s> to: \"%s\"".formatted(textChannel.getId(), topic)) + .build() + ).queue(); + } +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/command/impl/server/channel/ViewTopicSubCommand.java b/src/main/java/cc/fascinated/bat/command/impl/server/channel/ViewTopicSubCommand.java new file mode 100644 index 0000000..c8d6ccd --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/server/channel/ViewTopicSubCommand.java @@ -0,0 +1,52 @@ +package cc.fascinated.bat.command.impl.server.channel; + +import cc.fascinated.bat.command.BatSubCommand; +import cc.fascinated.bat.command.CommandInfo; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import lombok.NonNull; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.channel.Channel; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component +@CommandInfo(name = "viewtopic", description = "View the topic of a channel") +public class ViewTopicSubCommand extends BatSubCommand { + public ViewTopicSubCommand() { + super.addOption(OptionType.CHANNEL, "channel", "The channel to view the topic of", false); + } + + @Override + public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { + Channel target = interaction.getOption("channel") == null ? channel : interaction.getOption("channel").getAsChannel(); + if (!(target instanceof TextChannel textChannel)) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("<#%s> is not a text channel!".formatted(target.getId())) + .build()) + .queue(); + return; + } + + String topic = textChannel.getTopic(); + if (topic == null) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("<#%s> does not have a topic!".formatted(textChannel.getId())) + .build()) + .queue(); + return; + } + + interaction.replyEmbeds(EmbedUtils.genericEmbed() + .setDescription("The topic of <#%s> is: \"%s\"".formatted(textChannel.getId(), topic)) + .build() + ).queue(); + } +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/service/CommandService.java b/src/main/java/cc/fascinated/bat/service/CommandService.java index 6894ab1..9ad85fe 100644 --- a/src/main/java/cc/fascinated/bat/service/CommandService.java +++ b/src/main/java/cc/fascinated/bat/service/CommandService.java @@ -1,7 +1,10 @@ package cc.fascinated.bat.service; import cc.fascinated.bat.Consts; -import cc.fascinated.bat.command.*; +import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.BatCommandExecutor; +import cc.fascinated.bat.command.BatSubCommand; +import cc.fascinated.bat.command.Category; import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; @@ -75,10 +78,10 @@ public class CommandService extends ListenerAdapter { // Unregister all commands that Discord has but we don't jda.retrieveCommands().complete().forEach(command -> { - CommandInfo commandInfo = commands.get(command.getName()).getCommandInfo(); - if (commands.containsKey(command.getName()) && (commandInfo.category().isHidden() || commandInfo.botOwnerOnly())) { + BatCommand batCommand = commands.get(command.getName()); + if (batCommand == null) { jda.deleteCommandById(command.getId()).complete(); // Unregister the command on Discord - log.info("Unregistered hidden command \"{}\" from Discord", command.getName()); + log.info("Unregistered unknown command \"{}\" from Discord", command.getName()); return; } if (commands.containsKey(command.getName())) { From 409e71a0aab1857cd2b3efb99768155a2ced45e5 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 27 Jun 2024 21:04:21 -0500 Subject: [PATCH 07/10] Add FoxCommand --- .../bat/command/impl/fun/FoxCommand.java | 36 +++++++++++++++++++ .../model/token/randomfox/RandomFoxToken.java | 21 +++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/main/java/cc/fascinated/bat/command/impl/fun/FoxCommand.java create mode 100644 src/main/java/cc/fascinated/bat/model/token/randomfox/RandomFoxToken.java diff --git a/src/main/java/cc/fascinated/bat/command/impl/fun/FoxCommand.java b/src/main/java/cc/fascinated/bat/command/impl/fun/FoxCommand.java new file mode 100644 index 0000000..4da33d9 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/fun/FoxCommand.java @@ -0,0 +1,36 @@ +package cc.fascinated.bat.command.impl.fun; + +import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.Category; +import cc.fascinated.bat.command.CommandInfo; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.common.WebRequest; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import cc.fascinated.bat.model.token.randomfox.RandomFoxToken; +import lombok.NonNull; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import org.springframework.stereotype.Component; + +/** + * @author Nick (okNick) + */ +@Component +@CommandInfo(name = "fox", description = "Get a random fox image", category = Category.FUN, guildOnly = false) +public class FoxCommand extends BatCommand { + @Override + public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { + RandomFoxToken responseEntity = WebRequest.getAsEntity("https://randomfox.ca/floof/", RandomFoxToken.class); + if (responseEntity == null) { + interaction.reply("Failed to get a fox image!").queue(); + return; + } + + interaction.replyEmbeds(EmbedUtils.genericEmbed() + .setAuthor("Here's a random fox image!") + .setImage(responseEntity.getImage()) + .build()).queue(); + } +} diff --git a/src/main/java/cc/fascinated/bat/model/token/randomfox/RandomFoxToken.java b/src/main/java/cc/fascinated/bat/model/token/randomfox/RandomFoxToken.java new file mode 100644 index 0000000..c59b169 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/model/token/randomfox/RandomFoxToken.java @@ -0,0 +1,21 @@ +package cc.fascinated.bat.model.token.randomfox; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author Nick (okNick) + */ +@Getter +@AllArgsConstructor +public class RandomFoxToken { + /** + * The URL of the raw image. + */ + private String image; + + /** + * The link of the image + other web info. + */ + private String link; +} \ No newline at end of file From d54d2ffe0dda0d911284afeee7b4e8bf8cb3522c Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 27 Jun 2024 21:11:37 -0500 Subject: [PATCH 08/10] wording --- .../bat/command/impl/server/channel/SetTopicSubCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/cc/fascinated/bat/command/impl/server/channel/SetTopicSubCommand.java b/src/main/java/cc/fascinated/bat/command/impl/server/channel/SetTopicSubCommand.java index 7a86419..23c3467 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/server/channel/SetTopicSubCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/server/channel/SetTopicSubCommand.java @@ -40,7 +40,7 @@ public class SetTopicSubCommand extends BatSubCommand { String topic = interaction.getOption("topic").getAsString(); if (topic.length() > 1024) { interaction.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription("The topic must be less than 1024 characters!") + .setDescription("The topic must be 1024 characters or less!") .build()) .queue(); return; From d088e2bf9530b4767ce99e94f2c618cf00fb62dd Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 27 Jun 2024 21:15:27 -0500 Subject: [PATCH 09/10] Add category to ChannelCommand --- .../bat/command/impl/server/channel/ChannelCommand.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/cc/fascinated/bat/command/impl/server/channel/ChannelCommand.java b/src/main/java/cc/fascinated/bat/command/impl/server/channel/ChannelCommand.java index 65f5193..dae9dd6 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/server/channel/ChannelCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/server/channel/ChannelCommand.java @@ -1,6 +1,7 @@ package cc.fascinated.bat.command.impl.server.channel; import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.Category; import cc.fascinated.bat.command.CommandInfo; import lombok.NonNull; import org.springframework.beans.factory.annotation.Autowired; @@ -11,7 +12,7 @@ import org.springframework.stereotype.Component; * @author Nick (okNick) */ @Component -@CommandInfo(name = "channel", description = "View or set information about a channel") +@CommandInfo(name = "channel", description = "View or set information about a channel", category = Category.SERVER) public class ChannelCommand extends BatCommand { @Autowired public ChannelCommand(@NonNull ApplicationContext context) { From 3af151d89a8dc28c617f9f6c0f7cb2e9c80102c1 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 27 Jun 2024 21:24:55 -0500 Subject: [PATCH 10/10] Revert quick fix --- .../cc/fascinated/bat/service/CommandService.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/service/CommandService.java b/src/main/java/cc/fascinated/bat/service/CommandService.java index 9ad85fe..c54ba1d 100644 --- a/src/main/java/cc/fascinated/bat/service/CommandService.java +++ b/src/main/java/cc/fascinated/bat/service/CommandService.java @@ -1,10 +1,7 @@ package cc.fascinated.bat.service; import cc.fascinated.bat.Consts; -import cc.fascinated.bat.command.BatCommand; -import cc.fascinated.bat.command.BatCommandExecutor; -import cc.fascinated.bat.command.BatSubCommand; -import cc.fascinated.bat.command.Category; +import cc.fascinated.bat.command.*; import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; @@ -78,10 +75,10 @@ public class CommandService extends ListenerAdapter { // Unregister all commands that Discord has but we don't jda.retrieveCommands().complete().forEach(command -> { - BatCommand batCommand = commands.get(command.getName()); - if (batCommand == null) { + CommandInfo commandInfo = commands.get(command.getName()).getCommandInfo(); + if (commands.containsKey(command.getName()) && (commandInfo.category().isHidden() || commandInfo.botOwnerOnly())) { jda.deleteCommandById(command.getId()).complete(); // Unregister the command on Discord - log.info("Unregistered unknown command \"{}\" from Discord", command.getName()); + log.info("Unregistered hidden command \"{}\" from Discord", command.getName()); return; } if (commands.containsKey(command.getName())) { @@ -211,4 +208,4 @@ public class CommandService extends ListenerAdapter { .queue(); } } -} +} \ No newline at end of file