From 00314934bd4cbf8e045672f731b1be5bfe8296a0 Mon Sep 17 00:00:00 2001 From: Chew Date: Sun, 23 May 2021 21:43:12 -0500 Subject: [PATCH] Slash commands! --- pom.xml | 24 +++++++--- src/main/java/pw/chew/chanserv/ChanServ.java | 24 +++++++++- .../chew/chanserv/commands/AdminCommand.java | 33 ++++++++------ .../chew/chanserv/commands/HalfopCommand.java | 33 ++++++++------ .../chew/chanserv/commands/JoinCommand.java | 41 ++++++++++------- .../chew/chanserv/commands/KickCommand.java | 30 ++++++++----- .../chew/chanserv/commands/LeaveCommand.java | 11 ++--- .../commands/ListCommunitiesCommand.java | 30 ++++++------- .../chew/chanserv/commands/ModeCommand.java | 45 +++++++++++++------ .../pw/chew/chanserv/commands/OpCommand.java | 33 ++++++++------ .../chew/chanserv/commands/OwnerCommand.java | 33 ++++++++------ .../chanserv/commands/RandomColorCommand.java | 17 ++++--- .../chew/chanserv/commands/RankCommand.java | 11 ++--- .../chew/chanserv/commands/ReasonCommand.java | 35 ++++++++------- .../chanserv/commands/ShutdownCommand.java | 23 +++++++--- .../chew/chanserv/commands/TopicCommand.java | 27 +++++++---- .../chew/chanserv/commands/UmodeCommand.java | 13 +++--- .../chew/chanserv/commands/VoiceCommand.java | 31 +++++++------ .../chew/chanserv/util/AuditLogManager.java | 2 +- 19 files changed, 314 insertions(+), 182 deletions(-) diff --git a/pom.xml b/pom.xml index 87c548d..8c3cc85 100644 --- a/pom.xml +++ b/pom.xml @@ -31,22 +31,32 @@ chew-jenkins https://jenkins.chew.pw/plugin/repository/everything/ + + chew-m2 + https://m2.chew.pro/snapshots/ + - net.dv8tion - JDA - 4.2.0_204 + com.github.dv8fromtheworld + jda + feature~slash-commands-v3.7.1-g5951675-657 compile + + + club.minnced + opus-java + + - + - com.jagrosh - jda-utilities - 3.0.3 + pw.chew + jda-chewtils + 1.20-SNAPSHOT compile pom diff --git a/src/main/java/pw/chew/chanserv/ChanServ.java b/src/main/java/pw/chew/chanserv/ChanServ.java index 06c6750..5d7f719 100644 --- a/src/main/java/pw/chew/chanserv/ChanServ.java +++ b/src/main/java/pw/chew/chanserv/ChanServ.java @@ -18,7 +18,9 @@ import com.jagrosh.jdautilities.command.Command; import com.jagrosh.jdautilities.command.CommandClientBuilder; +import com.jagrosh.jdautilities.command.SlashCommand; import com.jagrosh.jdautilities.commons.waiter.EventWaiter; +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; @@ -47,6 +49,8 @@ import java.util.Set; public class ChanServ { + private static JDA jda; + public static void main(String[] args) throws IOException, LoginException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { // Load properties into the PropertiesManager Properties prop = new Properties(); @@ -65,9 +69,10 @@ public static void main(String[] args) throws IOException, LoginException, Invoc client.useHelpBuilder(false); client.addCommands(getCommands()); + client.addSlashCommands(getSlashCommands()); // Register JDA - JDABuilder.createDefault(PropertiesManager.getToken()) + jda = JDABuilder.createDefault(PropertiesManager.getToken()) .setChunkingFilter(ChunkingFilter.ALL) .setMemberCachePolicy(MemberCachePolicy.ALL) .enableIntents(GatewayIntent.GUILD_MEMBERS) @@ -88,6 +93,10 @@ public static void main(String[] args) throws IOException, LoginException, Invoc .build(); } + public static JDA getJDA() { + return jda; + } + private static Command[] getCommands() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { Reflections reflections = new Reflections("pw.chew.chanserv.commands"); Set> subTypes = reflections.getSubTypesOf(Command.class); @@ -100,4 +109,17 @@ private static Command[] getCommands() throws IllegalAccessException, Instantiat return commands.toArray(new Command[0]); } + + private static SlashCommand[] getSlashCommands() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { + Reflections reflections = new Reflections("pw.chew.chanserv.commands"); + Set> subTypes = reflections.getSubTypesOf(SlashCommand.class); + List commands = new ArrayList<>(); + + for (Class theClass : subTypes) { + commands.add(theClass.getDeclaredConstructor().newInstance()); + LoggerFactory.getLogger(theClass).debug("Loaded Successfully!"); + } + + return commands.toArray(new SlashCommand[0]); + } } diff --git a/src/main/java/pw/chew/chanserv/commands/AdminCommand.java b/src/main/java/pw/chew/chanserv/commands/AdminCommand.java index a07ecc1..79f4bd0 100644 --- a/src/main/java/pw/chew/chanserv/commands/AdminCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/AdminCommand.java @@ -1,23 +1,34 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; import pw.chew.chanserv.util.AuditLogManager; import pw.chew.chanserv.util.MemberHelper; import pw.chew.chanserv.util.Roles; import java.awt.Color; +import java.util.ArrayList; +import java.util.List; -public class AdminCommand extends Command { +public class AdminCommand extends SlashCommand { public AdminCommand() { this.name = "admin"; - this.guildOnly = false; + this.guildOnly = true; + this.guildId = "134445052805120001"; + + List data = new ArrayList<>(); + data.add( + new OptionData(OptionType.USER, "user", "The user to promote to Admin").setRequired(true) + ); + this.options = data; } @Override - protected void execute(CommandEvent event) { + protected void execute(SlashCommandEvent event) { if (MemberHelper.getRank(event.getMember()).getPriority() < 5) { event.reply( new EmbedBuilder() @@ -25,22 +36,18 @@ protected void execute(CommandEvent event) { .setDescription("You do not have the proper user modes to do this! You must have +q (Owner) or higher.") .setColor(Color.RED) .build() - ); + ).setEphemeral(true).queue(); return; } - Member user = event.getGuild().getMemberById(event.getArgs().replace("<@!", "").replace(">", "")); - if (user == null) { - event.reply("Member could not be found. How? did they leave when you pinged? wtf. if you see this, something went bad"); // or you're just browsing github - return; - } + Member user = event.getOption("user").getAsMember(); event.getGuild().addRoleToMember(user, Roles.Rank.ADMIN.getRole(event.getGuild())).queue( e -> { event.reply(new EmbedBuilder() .setTitle("**User Mode Changed Successfully**") - .setDescription(user.getAsMention() + " has been promoted to admin by " + event.getAuthor().getAsMention()) + .setDescription(user.getAsMention() + " has been promoted to admin by " + event.getUser().getAsMention()) .setColor(Color.GREEN) - .build()); + .build()).queue(); AuditLogManager.logEntry(AuditLogManager.LogType.MODE_CHANGE, user.getUser(), event.getMember(), event.getGuild(), "+a"); } ); diff --git a/src/main/java/pw/chew/chanserv/commands/HalfopCommand.java b/src/main/java/pw/chew/chanserv/commands/HalfopCommand.java index 66e65c4..80b320f 100644 --- a/src/main/java/pw/chew/chanserv/commands/HalfopCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/HalfopCommand.java @@ -1,23 +1,34 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; import pw.chew.chanserv.util.AuditLogManager; import pw.chew.chanserv.util.MemberHelper; import pw.chew.chanserv.util.Roles; import java.awt.Color; +import java.util.ArrayList; +import java.util.List; -public class HalfopCommand extends Command { +public class HalfopCommand extends SlashCommand { public HalfopCommand() { this.name = "halfop"; - this.guildOnly = false; + this.guildOnly = true; + this.guildId = "134445052805120001"; + + List data = new ArrayList<>(); + data.add( + new OptionData(OptionType.USER, "user", "The user to promote to half-op.").setRequired(true) + ); + this.options = data; } @Override - protected void execute(CommandEvent event) { + protected void execute(SlashCommandEvent event) { if (MemberHelper.getRank(event.getMember()).getPriority() < 3) { event.reply( new EmbedBuilder() @@ -25,22 +36,18 @@ protected void execute(CommandEvent event) { .setDescription("You do not have the proper user modes to do this! You must have +o (Op) or higher.") .setColor(Color.RED) .build() - ); + ).setEphemeral(true).queue(); return; } - Member user = event.getGuild().getMemberById(event.getArgs().replace("<@!", "").replace(">", "")); - if (user == null) { - event.reply("Member could not be found. How? did they leave when you pinged? wtf. if you see this, something went bad"); // or you're just browsing github - return; - } + Member user = event.getOption("user").getAsMember(); event.getGuild().addRoleToMember(user, Roles.Rank.HALFOP.getRole(event.getGuild())).queue( e -> { event.reply(new EmbedBuilder() .setTitle("**User Mode Changed Successfully**") - .setDescription(user.getAsMention() + " has been half-opped by " + event.getAuthor().getAsMention()) + .setDescription(user.getAsMention() + " has been half-opped by " + event.getUser().getAsMention()) .setColor(Color.GREEN) - .build()); + .build()).queue(); AuditLogManager.logEntry(AuditLogManager.LogType.MODE_CHANGE, user.getUser(), event.getMember(), event.getGuild(), "+h"); } ); diff --git a/src/main/java/pw/chew/chanserv/commands/JoinCommand.java b/src/main/java/pw/chew/chanserv/commands/JoinCommand.java index c0b9034..5f3cab9 100644 --- a/src/main/java/pw/chew/chanserv/commands/JoinCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/JoinCommand.java @@ -1,30 +1,41 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; import pw.chew.chanserv.util.Community; -public class JoinCommand extends Command { +import java.util.ArrayList; +import java.util.List; + +public class JoinCommand extends SlashCommand { public JoinCommand() { this.name = "join"; - this.aliases = new String[]{"j"}; this.guildOnly = true; + this.guildId = "134445052805120001"; + + OptionData data = new OptionData(OptionType.STRING, "community", "The community to join.") + .setRequired(true); + + for (Community community : Community.values()) { + data.addChoice(community.name(), community.name()); + } + + List dataList = new ArrayList<>(); + dataList.add(data); + + this.options = dataList; } @Override - protected void execute(CommandEvent event) { - String channel = event.getArgs().toUpperCase().replace(" ", ""); - event.getMessage().delete().queue(); - - Community community; - try { - community = Community.valueOf(channel); - } catch (IllegalArgumentException e) { - event.reply("Not a valid community!"); - return; - } + protected void execute(SlashCommandEvent event) { + String channel = event.getOption("community").getAsString(); + Community community = Community.valueOf(channel); community.addMember(event.getMember()); + + event.reply("Joined!").setEphemeral(true).queue(); } } \ No newline at end of file diff --git a/src/main/java/pw/chew/chanserv/commands/KickCommand.java b/src/main/java/pw/chew/chanserv/commands/KickCommand.java index 5345d06..a861b26 100644 --- a/src/main/java/pw/chew/chanserv/commands/KickCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/KickCommand.java @@ -2,22 +2,34 @@ import com.jagrosh.jdautilities.command.Command; import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; import pw.chew.chanserv.util.AuditLogManager; import pw.chew.chanserv.util.MemberHelper; import java.awt.Color; +import java.util.ArrayList; +import java.util.List; -public class KickCommand extends Command { +public class KickCommand extends SlashCommand { public KickCommand() { this.name = "kick"; this.guildOnly = true; + this.guildId = "134445052805120001"; + + List data = new ArrayList<>(); + data.add(new OptionData(OptionType.USER, "user", "The user to kick.").setRequired(true)); + data.add(new OptionData(OptionType.STRING, "reason", "The reason for the kick")); + this.options = data; } @Override - protected void execute(CommandEvent event) { + protected void execute(SlashCommandEvent event) { if (MemberHelper.getRank(event.getMember()).getPriority() < 2) { event.reply( new EmbedBuilder() @@ -25,21 +37,19 @@ protected void execute(CommandEvent event) { .setDescription("You do not have the proper user modes to do this! You must have +h (half-op) or higher.") .setColor(Color.RED) .build() - ); + ).setEphemeral(true).queue(); return; } - Member user = event.getGuild().getMemberById(event.getArgs().replace("<@!", "").replace(">", "")); - if (user == null) { - event.reply("Member could not be found. How? did they leave when you pinged? wtf. if you see this, something went bad"); // or you're just browsing github - return; - } - user.kick(event.getArgs().replace(user.getAsMention() + " ", "")).queue(userid -> { + Member user = event.getOption("user").getAsMember(); + String reason = event.getOption("reason") == null ? "*No reason provided*" : event.getOption("reason").getAsString(); + + user.kick(reason).queue(userid -> { AuditLogManager.logEntry(AuditLogManager.LogType.KICK, user.getUser(), event.getMember(), event.getGuild()); event.reply(new EmbedBuilder() .setTitle("**User Kicked Successfully**") .setDescription("Say goodbye to that user " + user.getUser().getAsTag()) - .build()); + .build()).queue(); }); } } diff --git a/src/main/java/pw/chew/chanserv/commands/LeaveCommand.java b/src/main/java/pw/chew/chanserv/commands/LeaveCommand.java index 6425b9b..dcf861e 100644 --- a/src/main/java/pw/chew/chanserv/commands/LeaveCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/LeaveCommand.java @@ -1,21 +1,22 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import pw.chew.chanserv.util.Community; -public class LeaveCommand extends Command { +public class LeaveCommand extends SlashCommand { public LeaveCommand() { this.name = "leave"; this.guildOnly = true; + this.guildId = "134445052805120001"; } @Override - protected void execute(CommandEvent event) { - event.getMessage().delete().queue(); + protected void execute(SlashCommandEvent event) { if (event.getTextChannel().getParent().getId().equals(Community.categoryId)) { Community.valueOf(event.getChannel().getName().toUpperCase()).removeMember(event.getMember()); } + event.acknowledge().queue(); } } diff --git a/src/main/java/pw/chew/chanserv/commands/ListCommunitiesCommand.java b/src/main/java/pw/chew/chanserv/commands/ListCommunitiesCommand.java index 4668adf..28cca48 100644 --- a/src/main/java/pw/chew/chanserv/commands/ListCommunitiesCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/ListCommunitiesCommand.java @@ -1,41 +1,41 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import pw.chew.chanserv.util.Community; import java.awt.Color; -public class ListCommunitiesCommand extends Command { +public class ListCommunitiesCommand extends SlashCommand { public ListCommunitiesCommand() { this.name = "listcommunities"; this.guildOnly = true; + this.guildId = "134445052805120001"; this.botPermissions = new Permission[]{Permission.MESSAGE_EMBED_LINKS}; this.cooldown = 30; this.cooldownScope = CooldownScope.CHANNEL; } @Override - protected void execute(CommandEvent event) { - event.getMessage().delete().queue(); + protected void execute(SlashCommandEvent event) { EmbedBuilder e = new EmbedBuilder(); e.setTitle("Communities Available on " + event.getGuild().getName()); - e.addField("Gaming", "**Members**: " + Community.GAMING.getMemberCount() + "\n**Join**: `;j gaming`", true); - e.addField("Crypto", "**Members**: " + Community.CRYPTO.getMemberCount() + "\n**Join**: `;j crypto`", true); - e.addField("Programming", "**Members**: " + Community.PROGRAMMING.getMemberCount() + "\n**Join**: `;j programming`", true); - e.addField("Tech", "**Members**: " + Community.TECH.getMemberCount() + "\n**Join**: `;j tech`", true); - e.addField("Music", "**Members**: " + Community.MUSIC.getMemberCount() + "\n**Join**: `;j music`", true); - e.addField("Pets", "**Members**: " + Community.PETS.getMemberCount() + "\n**Join**: `;j pets`", true); - e.addField("Memes", "**Members**: " + Community.MEMES.getMemberCount() + "\n**Join**: `;j memes`", true); - e.addField("LGBT", "**Members**: " + Community.LGBT.getMemberCount() + "\n**Join**: `;j lgbt`", true); - e.addField("Anime", "**Members**: " + Community.ANIME.getMemberCount() + "\n**Join**: `;j anime`", true); + e.addField("Gaming", "**Members**: " + Community.GAMING.getMemberCount() + "\n**Join**: `/join gaming`", true); + e.addField("Crypto", "**Members**: " + Community.CRYPTO.getMemberCount() + "\n**Join**: `/join crypto`", true); + e.addField("Programming", "**Members**: " + Community.PROGRAMMING.getMemberCount() + "\n**Join**: `/join programming`", true); + e.addField("Tech", "**Members**: " + Community.TECH.getMemberCount() + "\n**Join**: `/join tech`", true); + e.addField("Music", "**Members**: " + Community.MUSIC.getMemberCount() + "\n**Join**: `/join music`", true); + e.addField("Pets", "**Members**: " + Community.PETS.getMemberCount() + "\n**Join**: `/join pets`", true); + e.addField("Memes", "**Members**: " + Community.MEMES.getMemberCount() + "\n**Join**: `/join memes`", true); + e.addField("LGBT", "**Members**: " + Community.LGBT.getMemberCount() + "\n**Join**: `/join lgbt`", true); + e.addField("Anime", "**Members**: " + Community.ANIME.getMemberCount() + "\n**Join**: `/join anime`", true); e.setColor(Color.GREEN); - event.reply(e.build()); + event.reply(e.build()).setEphemeral(true).queue(); } } diff --git a/src/main/java/pw/chew/chanserv/commands/ModeCommand.java b/src/main/java/pw/chew/chanserv/commands/ModeCommand.java index 51cbf72..0bb6b3f 100644 --- a/src/main/java/pw/chew/chanserv/commands/ModeCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/ModeCommand.java @@ -1,23 +1,43 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; import pw.chew.chanserv.util.AuditLogManager; import pw.chew.chanserv.util.MemberHelper; import pw.chew.chanserv.util.Roles; import java.awt.Color; +import java.util.ArrayList; +import java.util.List; -public class ModeCommand extends Command { +public class ModeCommand extends SlashCommand { public ModeCommand() { this.name = "mode"; + this.guildOnly = true; + this.guildId = "134445052805120001"; + + List data = new ArrayList<>(); + data.add(new OptionData(OptionType.USER, "user", "The user to promote to voiced.").setRequired(true)); + + OptionData modes = new OptionData(OptionType.STRING, "mode", "The mode to give.").setRequired(true); + for (Roles.UserMode mode : Roles.UserMode.values()) { + if (mode.canGive()) { + modes.addChoice("+" + mode.name(),"+" + mode.name()); + modes.addChoice("-" + mode.name(),"-" + mode.name()); + } + } + data.add(modes); + + this.options = data; } @Override - protected void execute(CommandEvent event) { + protected void execute(SlashCommandEvent event) { if (MemberHelper.getRank(event.getMember()).getPriority() < 4) { event.reply( new EmbedBuilder() @@ -25,18 +45,12 @@ protected void execute(CommandEvent event) { .setDescription("You do not have the proper user modes to do this! You must have +a (Admin) or higher.") .setColor(Color.RED) .build() - ); + ).setEphemeral(true).queue(); return; } - String id = event.getArgs().split(" ")[0]; - String mode = event.getArgs().split(" ")[1]; - - Member user = event.getGuild().getMemberById(id.replace("<@!", "").replace(">", "")); - if (user == null) { - event.reply("Member could not be found. How? did they leave when you pinged? wtf. if you see this, something went bad"); // or you"re just browsing github - return; - } + Member user = event.getOption("user").getAsMember(); + String mode = event.getOption("mode").getAsString(); if (mode.length() > 2) { boolean add = mode.charAt(0) == '+'; String modetemp = mode.substring(1); @@ -63,6 +77,11 @@ protected void execute(CommandEvent event) { } else { return; } + event.reply(new EmbedBuilder() + .setTitle("**User Mode Changed Successfully**") + .setDescription(user.getAsMention() + " has been given mode " + mode + " by " + event.getUser().getAsMention()) + .setColor(Color.GREEN) + .build()).queue(); AuditLogManager.logEntry(AuditLogManager.LogType.MODE_CHANGE, user.getUser(), event.getMember(), event.getGuild(), mode); } } \ No newline at end of file diff --git a/src/main/java/pw/chew/chanserv/commands/OpCommand.java b/src/main/java/pw/chew/chanserv/commands/OpCommand.java index f37e2f6..d3cd8b4 100644 --- a/src/main/java/pw/chew/chanserv/commands/OpCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/OpCommand.java @@ -1,23 +1,34 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; import pw.chew.chanserv.util.AuditLogManager; import pw.chew.chanserv.util.MemberHelper; import pw.chew.chanserv.util.Roles; import java.awt.Color; +import java.util.ArrayList; +import java.util.List; -public class OpCommand extends Command { +public class OpCommand extends SlashCommand { public OpCommand() { this.name = "op"; - this.guildOnly = false; + this.guildOnly = true; + this.guildId = "134445052805120001"; + + List data = new ArrayList<>(); + data.add( + new OptionData(OptionType.USER, "user", "The user to promote to operator.").setRequired(true) + ); + this.options = data; } @Override - protected void execute(CommandEvent event) { + protected void execute(SlashCommandEvent event) { if (MemberHelper.getRank(event.getMember()).getPriority() < 4) { event.reply( new EmbedBuilder() @@ -25,22 +36,18 @@ protected void execute(CommandEvent event) { .setDescription("You do not have the proper user modes to do this! You must have +a (Admin) or higher.") .setColor(Color.RED) .build() - ); + ).setEphemeral(true).queue(); return; } - Member user = event.getGuild().getMemberById(event.getArgs().replace("<@!", "").replace(">", "")); - if (user == null) { - event.reply("Member could not be found. How? did they leave when you pinged? wtf. if you see this, something went bad"); // or you're just browsing github - return; - } + Member user = event.getOption("user").getAsMember(); event.getGuild().addRoleToMember(user, Roles.Rank.OP.getRole(event.getGuild())).queue( e -> { event.reply(new EmbedBuilder() .setTitle("**User Mode Changed Successfully**") - .setDescription(user.getAsMention() + " has been opped by " + event.getAuthor().getAsMention()) + .setDescription(user.getAsMention() + " has been opped by " + event.getUser().getAsMention()) .setColor(Color.GREEN) - .build()); + .build()).queue(); AuditLogManager.logEntry(AuditLogManager.LogType.MODE_CHANGE, user.getUser(), event.getMember(), event.getGuild(), "+o"); } ); diff --git a/src/main/java/pw/chew/chanserv/commands/OwnerCommand.java b/src/main/java/pw/chew/chanserv/commands/OwnerCommand.java index 6056aa4..c4863e0 100644 --- a/src/main/java/pw/chew/chanserv/commands/OwnerCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/OwnerCommand.java @@ -1,23 +1,34 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; import pw.chew.chanserv.util.AuditLogManager; import pw.chew.chanserv.util.MemberHelper; import pw.chew.chanserv.util.Roles; import java.awt.Color; +import java.util.ArrayList; +import java.util.List; -public class OwnerCommand extends Command { +public class OwnerCommand extends SlashCommand { public OwnerCommand() { this.name = "owner"; - this.guildOnly = false; + this.guildOnly = true; + this.guildId = "134445052805120001"; + + List data = new ArrayList<>(); + data.add( + new OptionData(OptionType.USER, "user", "The user to promote to owner.").setRequired(true) + ); + this.options = data; } @Override - protected void execute(CommandEvent event) { + protected void execute(SlashCommandEvent event) { if (MemberHelper.getRank(event.getMember()).getPriority() < 6) { event.reply( new EmbedBuilder() @@ -25,22 +36,18 @@ protected void execute(CommandEvent event) { .setDescription("You do not have the proper user modes to do this! You must have +Y (Oper) or be a NetAdmin.") .setColor(Color.RED) .build() - ); + ).setEphemeral(true).queue(); return; } - Member user = event.getGuild().getMemberById(event.getArgs().replace("<@!", "").replace(">", "")); - if (user == null) { - event.reply("Member could not be found. How? did they leave when you pinged? wtf. if you see this, something went bad"); // or you're just browsing github - return; - } + Member user = event.getOption("user").getAsMember(); event.getGuild().addRoleToMember(user, Roles.Rank.OWNER.getRole(event.getGuild())).queue( e -> { event.reply(new EmbedBuilder() .setTitle("**User Mode Changed Successfully**") - .setDescription(user.getAsMention() + " has been promoted to owner by " + event.getAuthor().getAsMention()) + .setDescription(user.getAsMention() + " has been promoted to owner by " + event.getUser().getAsMention()) .setColor(Color.GREEN) - .build()); + .build()).queue(); AuditLogManager.logEntry(AuditLogManager.LogType.MODE_CHANGE, user.getUser(), event.getMember(), event.getGuild(), "+q"); } ); diff --git a/src/main/java/pw/chew/chanserv/commands/RandomColorCommand.java b/src/main/java/pw/chew/chanserv/commands/RandomColorCommand.java index bafdd30..60a4ce5 100644 --- a/src/main/java/pw/chew/chanserv/commands/RandomColorCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/RandomColorCommand.java @@ -1,31 +1,30 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; import net.dv8tion.jda.api.entities.Role; -import org.slf4j.LoggerFactory; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import java.util.List; import java.util.Random; -public class RandomColorCommand extends Command { +public class RandomColorCommand extends SlashCommand { public RandomColorCommand() { this.name = "randomcolor"; this.cooldownScope = CooldownScope.USER; this.guildOnly = true; + this.guildId = "134445052805120001"; this.cooldown = 21600; } @Override - protected void execute(CommandEvent event) { - event.getChannel().sendTyping().queue(); - List role = event.getGuild().getRolesByName(event.getAuthor().getId(), true); + protected void execute(SlashCommandEvent event) { + List role = event.getGuild().getRolesByName(event.getUser().getId(), true); Random r = new Random(); int color = r.nextInt((16777215) + 1); Role current; if (role.isEmpty()) { - current = event.getGuild().createRole().setName(event.getAuthor().getId()).setColor(color).complete(); + current = event.getGuild().createRole().setName(event.getUser().getId()).setColor(color).complete(); event.getGuild().modifyRolePositions(false).selectPosition(current).moveTo(3).complete(); } else { current = role.get(0); @@ -34,6 +33,6 @@ protected void execute(CommandEvent event) { if (!event.getMember().getRoles().contains(current)) { event.getGuild().addRoleToMember(event.getMember(), current).complete(); } - event.reply("Set your role color successfully!"); + event.reply("Set your role color successfully!").setEphemeral(true).queue(); } } diff --git a/src/main/java/pw/chew/chanserv/commands/RankCommand.java b/src/main/java/pw/chew/chanserv/commands/RankCommand.java index 8e6446a..553023f 100644 --- a/src/main/java/pw/chew/chanserv/commands/RankCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/RankCommand.java @@ -1,18 +1,19 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import pw.chew.chanserv.util.MemberHelper; -public class RankCommand extends Command { +public class RankCommand extends SlashCommand { public RankCommand() { this.name = "rank"; this.guildOnly = true; + this.guildId = "134445052805120001"; } @Override - protected void execute(CommandEvent event) { + protected void execute(SlashCommandEvent event) { String rank = MemberHelper.getRank(event.getMember()).getRoleName(); - event.reply("Your rank is: " + rank); + event.reply("Your rank is: " + rank).setEphemeral(true).queue(); } } diff --git a/src/main/java/pw/chew/chanserv/commands/ReasonCommand.java b/src/main/java/pw/chew/chanserv/commands/ReasonCommand.java index ed3aebb..1915a62 100644 --- a/src/main/java/pw/chew/chanserv/commands/ReasonCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/ReasonCommand.java @@ -1,29 +1,36 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; import pw.chew.chanserv.util.AuditLogManager; import pw.chew.chanserv.util.MemberHelper; -import pw.chew.chanserv.util.MessageHelper; import java.awt.Color; -import java.io.File; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; -public class ReasonCommand extends Command { +public class ReasonCommand extends SlashCommand { public ReasonCommand() { this.name = "reason"; this.guildOnly = true; + this.guildId = "134445052805120001"; + + List data = new ArrayList<>(); + data.add(new OptionData(OptionType.INTEGER, "case", "The case to update.").setRequired(true)); + data.add(new OptionData(OptionType.STRING, "reason", "The reason to set the log to.").setRequired(true)); + + this.options = data; + } @Override - protected void execute(CommandEvent event) { + protected void execute(SlashCommandEvent event) { if (MemberHelper.getRank(event.getMember()).getPriority() < 2) { event.reply( new EmbedBuilder() @@ -31,17 +38,15 @@ protected void execute(CommandEvent event) { .setDescription("You do not have the proper user modes to do this! You must have +h (half-op) or higher.") .setColor(Color.RED) .build() - ); + ).setEphemeral(true).queue(); return; } - event.getMessage().delete().queue(); - - int caseId = Integer.parseInt(event.getArgs().split(" ")[0]); - String reason = event.getArgs().replace(caseId + " ", ""); + int caseId = (int) event.getOption("case").getAsLong(); + String reason = event.getOption("reason").getAsString(); if (caseId < 95) { - event.reply("This log happened prior to audit-log 2.0. The reason will not be able to change! Please try again."); + event.reply("This log happened prior to audit-log 2.0. The reason will not be able to change! Please try again.").setEphemeral(true).queue(); return; } @@ -57,7 +62,7 @@ protected void execute(CommandEvent event) { continue; } fields.remove(find); - fields.add(new MessageEmbed.Field(find.getName(), event.getAuthor().getAsMention(), true)); + fields.add(new MessageEmbed.Field(find.getName(), event.getUser().getAsMention(), true)); } for (MessageEmbed.Field find : fields) { @@ -84,6 +89,6 @@ protected void execute(CommandEvent event) { if (color > 0) newEmbed.setColor(color); - message.editMessage(newEmbed.build()).queue(msg -> MessageHelper.sendTemporaryMessage(event.getTextChannel(), "Reason for case " + caseId + " set to: " + reason, 5)); + message.editMessage(newEmbed.build()).queue(msg -> event.reply("Reason for case " + caseId + " set to: " + reason).setEphemeral(true).queue()); } } diff --git a/src/main/java/pw/chew/chanserv/commands/ShutdownCommand.java b/src/main/java/pw/chew/chanserv/commands/ShutdownCommand.java index 6748692..cbc179a 100644 --- a/src/main/java/pw/chew/chanserv/commands/ShutdownCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/ShutdownCommand.java @@ -1,18 +1,29 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.Command; -public class ShutdownCommand extends Command { +import java.util.List; + +public class ShutdownCommand extends SlashCommand { public ShutdownCommand() { this.name = "shutdown"; this.ownerCommand = true; - this.guildOnly = false; + this.guildOnly = true; + this.guildId = "134445052805120001"; } @Override - protected void execute(CommandEvent commandEvent) { + protected void execute(SlashCommandEvent commandEvent) { // whee - commandEvent.getChannel().sendMessage("Shutting down....").queue((msg) -> System.exit(0)); + List commands = commandEvent.getGuild().retrieveCommands().complete(); + commandEvent.reply("Shutting down....").setEphemeral(true).queue((msg) -> { + for (Command command : commands) { + commandEvent.getGuild().deleteCommandById(command.getId()).queue(); + } + + commandEvent.getJDA().shutdown(); + }); } } \ No newline at end of file diff --git a/src/main/java/pw/chew/chanserv/commands/TopicCommand.java b/src/main/java/pw/chew/chanserv/commands/TopicCommand.java index 61d88e3..ba115e5 100644 --- a/src/main/java/pw/chew/chanserv/commands/TopicCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/TopicCommand.java @@ -1,33 +1,42 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; import pw.chew.chanserv.util.MemberHelper; -import java.awt.*; +import java.awt.Color; +import java.util.ArrayList; +import java.util.List; -public class TopicCommand extends Command { +public class TopicCommand extends SlashCommand { public TopicCommand() { this.name = "topic"; this.guildOnly = true; + this.guildId = "134445052805120001"; + + List data = new ArrayList<>(); + data.add(new OptionData(OptionType.STRING, "topic", "The topic to set.").setRequired(true)); + this.options = data; } @Override - protected void execute(CommandEvent event) { + protected void execute(SlashCommandEvent event) { if (MemberHelper.getRank(event.getMember()).getPriority() >= 2) { String currentTopic = event.getTextChannel().getTopic(); if (currentTopic == null) { currentTopic = fixTopic(event.getTextChannel()); } String mode = currentTopic.split(" ")[0]; - event.getTextChannel().getManager().setTopic(mode + ' ' + event.getArgs()).queue(channel -> { + event.getTextChannel().getManager().setTopic(mode + ' ' + event.getOption("topic").getAsString()).queue(channel -> { event.reply(new EmbedBuilder() - .setTitle("**" + event.getAuthor().getName() + " set the topic**") - .setDescription(event.getArgs()) + .setTitle("**" + event.getUser().getName() + " set the topic**") + .setDescription(event.getOption("topic").getAsString()) .setColor(Color.GREEN).build() - ); + ).queue(); }); } } diff --git a/src/main/java/pw/chew/chanserv/commands/UmodeCommand.java b/src/main/java/pw/chew/chanserv/commands/UmodeCommand.java index 5dfbd60..8724d00 100644 --- a/src/main/java/pw/chew/chanserv/commands/UmodeCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/UmodeCommand.java @@ -1,24 +1,25 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import pw.chew.chanserv.util.MemberHelper; import java.util.List; -public class UmodeCommand extends Command { +public class UmodeCommand extends SlashCommand { public UmodeCommand() { this.name = "umode"; this.guildOnly = true; + this.guildId = "134445052805120001"; } @Override - protected void execute(CommandEvent event) { + protected void execute(SlashCommandEvent event) { List modes = MemberHelper.getUserModes(event.getMember()); if (modes.isEmpty()) { - event.reply("You have no user modes."); + event.reply("You have no user modes.").setEphemeral(true).queue(); } else { - event.reply("Your user modes are: +" + String.join("", modes)); + event.reply("Your user modes are: +" + String.join("", modes)).setEphemeral(true).queue(); } } } \ No newline at end of file diff --git a/src/main/java/pw/chew/chanserv/commands/VoiceCommand.java b/src/main/java/pw/chew/chanserv/commands/VoiceCommand.java index 7758518..1cf49f8 100644 --- a/src/main/java/pw/chew/chanserv/commands/VoiceCommand.java +++ b/src/main/java/pw/chew/chanserv/commands/VoiceCommand.java @@ -1,23 +1,32 @@ package pw.chew.chanserv.commands; -import com.jagrosh.jdautilities.command.Command; -import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jdautilities.command.SlashCommand; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; import pw.chew.chanserv.util.AuditLogManager; import pw.chew.chanserv.util.MemberHelper; import pw.chew.chanserv.util.Roles; import java.awt.Color; +import java.util.ArrayList; +import java.util.List; -public class VoiceCommand extends Command { +public class VoiceCommand extends SlashCommand { public VoiceCommand() { this.name = "voice"; - this.guildOnly = false; + this.guildOnly = true; + this.guildId = "134445052805120001"; + + List data = new ArrayList<>(); + data.add(new OptionData(OptionType.USER, "user", "The user to promote to voiced.").setRequired(true)); + this.options = data; } @Override - protected void execute(CommandEvent event) { + protected void execute(SlashCommandEvent event) { if (MemberHelper.getRank(event.getMember()).getPriority() < 2) { event.reply( new EmbedBuilder() @@ -25,22 +34,18 @@ protected void execute(CommandEvent event) { .setDescription("You do not have the proper user modes to do this! You must have +h (half-op) or higher.") .setColor(Color.RED) .build() - ); + ).setEphemeral(true).queue(); return; } - Member user = event.getGuild().getMemberById(event.getArgs().replace("<@!", "").replace(">", "")); - if (user == null) { - event.reply("Member could not be found. How? did they leave when you pinged? wtf. if you see this, something went bad"); // or you're just browsing github - return; - } + Member user = event.getOption("user").getAsMember(); event.getGuild().addRoleToMember(user, Roles.Rank.VOICED.getRole(event.getGuild())).queue( e -> { event.reply(new EmbedBuilder() .setTitle("**User Mode Changed Successfully**") - .setDescription(user.getAsMention() + " has been voiced by " + event.getAuthor().getAsMention()) + .setDescription(user.getAsMention() + " has been voiced by " + event.getUser().getAsMention()) .setColor(Color.GREEN) - .build()); + .build()).queue(); AuditLogManager.logEntry(AuditLogManager.LogType.MODE_CHANGE, user.getUser(), event.getMember(), event.getGuild(), "+v"); } ); diff --git a/src/main/java/pw/chew/chanserv/util/AuditLogManager.java b/src/main/java/pw/chew/chanserv/util/AuditLogManager.java index 1b5fc2e..d7b93a4 100644 --- a/src/main/java/pw/chew/chanserv/util/AuditLogManager.java +++ b/src/main/java/pw/chew/chanserv/util/AuditLogManager.java @@ -40,7 +40,7 @@ public static void logEntry(@NotNull LogType type, @NotNull User target, @Nullab embed.addField("Responsible Staff", "[Unknown]", true); else embed.addField("Responsible Staff", actor.getAsMention(), true); - embed.addField("Reason", "Responsible staff please add reason by `;reason case# [reason]`", true); + embed.addField("Reason", "Responsible staff please add reason by `/reason case# [reason]`", true); auditLogChannel.sendMessage(embed.build()).queue(msg -> FileManager.appendLine(fileName, msg.getId() + "\n")); }