Skip to content

Commit

Permalink
Slash commands!
Browse files Browse the repository at this point in the history
  • Loading branch information
Chew committed May 24, 2021
1 parent cd77de4 commit 0031493
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 182 deletions.
24 changes: 17 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,32 @@
<id>chew-jenkins</id>
<url>https://jenkins.chew.pw/plugin/repository/everything/</url>
</repository>
<repository>
<id>chew-m2</id>
<url>https://m2.chew.pro/snapshots/</url>
</repository>
</repositories>

<dependencies>
<!-- The JDA API -->
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_204</version>
<groupId>com.github.dv8fromtheworld</groupId>
<artifactId>jda</artifactId>
<version>feature~slash-commands-v3.7.1-g5951675-657</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
<artifactId>opus-java</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- The sdcf4j core module -->
<!-- The command wrapper, well, my fork of it -->
<dependency>
<groupId>com.jagrosh</groupId>
<artifactId>jda-utilities</artifactId>
<version>3.0.3</version>
<groupId>pw.chew</groupId>
<artifactId>jda-chewtils</artifactId>
<version>1.20-SNAPSHOT</version>
<scope>compile</scope>
<type>pom</type>
</dependency>
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/pw/chew/chanserv/ChanServ.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand All @@ -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<Class<? extends Command>> subTypes = reflections.getSubTypesOf(Command.class);
Expand All @@ -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<Class<? extends SlashCommand>> subTypes = reflections.getSubTypesOf(SlashCommand.class);
List<SlashCommand> commands = new ArrayList<>();

for (Class<? extends SlashCommand> theClass : subTypes) {
commands.add(theClass.getDeclaredConstructor().newInstance());
LoggerFactory.getLogger(theClass).debug("Loaded Successfully!");
}

return commands.toArray(new SlashCommand[0]);
}
}
33 changes: 20 additions & 13 deletions src/main/java/pw/chew/chanserv/commands/AdminCommand.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
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<OptionData> 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()
.setTitle("**Permission Error**")
.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");
}
);
Expand Down
33 changes: 20 additions & 13 deletions src/main/java/pw/chew/chanserv/commands/HalfopCommand.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
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<OptionData> 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()
.setTitle("**Permission Error**")
.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");
}
);
Expand Down
41 changes: 26 additions & 15 deletions src/main/java/pw/chew/chanserv/commands/JoinCommand.java
Original file line number Diff line number Diff line change
@@ -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<OptionData> 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();
}
}
30 changes: 20 additions & 10 deletions src/main/java/pw/chew/chanserv/commands/KickCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,54 @@

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<OptionData> 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()
.setTitle("**Permission Error**")
.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();
});
}
}
11 changes: 6 additions & 5 deletions src/main/java/pw/chew/chanserv/commands/LeaveCommand.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 0031493

Please sign in to comment.