Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/jda slash command implementation #293

Draft
wants to merge 7 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ repositories {

dependencies {
// discord/jda related
implementation('net.dv8tion:JDA:4.2.0_241') {
implementation('com.github.DV8FromTheWorld:JDA:0363076696') {
exclude group: 'club.minnced', module: 'opus-java'
}
implementation 'com.jagrosh:jda-utilities:3.0.5'
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/de/kittybot/kittybot/commands/admin/KickCommand.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.kittybot.kittybot.commands.admin;

import de.kittybot.kittybot.slashcommands.GuildCommandContext;
import de.kittybot.kittybot.slashcommands.Options;
import de.kittybot.kittybot.slashcommands.application.Category;
import de.kittybot.kittybot.slashcommands.application.RunGuildCommand;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionString;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionUser;
import de.kittybot.kittybot.slashcommands.interaction.GuildInteraction;
import de.kittybot.kittybot.slashcommands.interaction.Options;
import de.kittybot.kittybot.utils.MessageUtils;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.utils.MarkdownSanitizer;
Expand All @@ -23,26 +23,26 @@ public KickCommand(){
}

@Override
public void run(Options options, GuildInteraction ia){
if(!ia.getSelfMember().hasPermission(Permission.KICK_MEMBERS)){
ia.error("I don't have the required permission to kick members");
public void run(Options options, GuildCommandContext ctx){
if(!ctx.getSelfMember().hasPermission(Permission.KICK_MEMBERS)){
ctx.error("I don't have the required permission to kick members");
return;
}
var user = options.getUser("user");
if(user.getIdLong() == ia.getUserId()){
ia.error("You can't kick yourself");
if(user.getIdLong() == ctx.getUserId()){
ctx.error("You can't kick yourself");
}
var member = options.getMember("user");
if(member != null){
if(!ia.getSelfMember().canInteract(member)){
ia.error("I can't interact with this member");
if(!ctx.getSelfMember().canInteract(member)){
ctx.error("I can't interact with this member");
return;
}
}
var reason = options.getOrDefault("reason", "Kicked by " + ia.getMember().getAsMention());
ia.getGuild().kick(user.getId(), reason).reason(reason).queue(success ->
ia.reply("Kicked `" + MarkdownSanitizer.escape(user.getAsTag()) + "`(`" + user.getId() + "`).\nReason: " + reason),
error -> ia.error("Failed to kick " + user.getAsMention() + ".\nReason: `" + error.getMessage() + "`")
var reason = options.getString("reason", "Kicked by " + ctx.getMember().getAsMention());
ctx.getGuild().kick(user.getId(), reason).reason(reason).queue(success ->
ctx.reply("Kicked `" + MarkdownSanitizer.escape(user.getAsTag()) + "`(`" + user.getId() + "`).\nReason: " + reason),
error -> ctx.error("Failed to kick " + user.getAsMention() + ".\nReason: `" + error.getMessage() + "`")
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package de.kittybot.kittybot.commands.admin;

import de.kittybot.kittybot.slashcommands.GuildCommandContext;
import de.kittybot.kittybot.slashcommands.Options;
import de.kittybot.kittybot.slashcommands.application.Category;
import de.kittybot.kittybot.slashcommands.application.Command;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionEmote;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionRole;
import de.kittybot.kittybot.slashcommands.application.options.GuildSubCommand;
import de.kittybot.kittybot.slashcommands.interaction.GuildInteraction;
import de.kittybot.kittybot.slashcommands.interaction.Options;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Role;
Expand Down Expand Up @@ -47,21 +47,21 @@ public SetCommand(){
}

@Override
public void run(Options options, GuildInteraction ia){
if(!ia.getSelfMember().hasPermission(Permission.MANAGE_EMOTES)){
ia.error("I can't manage emotes due to lack of permissions. Please give me the `MANAGE_EMOTES` permission to use this command.");
public void run(Options options, GuildCommandContext ctx){
if(!ctx.getSelfMember().hasPermission(Permission.MANAGE_EMOTES)){
ctx.error("I can't manage emotes due to lack of permissions. Please give me the `MANAGE_EMOTES` permission to use this command.");
return;
}
var emoteAction = options.getEmote(ia.getGuild(), "emote");
var emoteAction = options.getEmote(ctx.getGuild(), "emote");
if(emoteAction == null){
ia.error("Failed to parse emote: `" + options.getString("emote") + "`");
ctx.error("Failed to parse emote: `" + options.getString("emote") + "`");
return;
}
emoteAction.queue(emote -> emote.getManager().setRoles(getRoles(ia.getGuild(), options, "role1", "role2", "role3", "role4", "role5", "role6", "role7", "role8", "role9")).queue(
success -> ia.reply("Successfully set roles"),
error -> ia.error("Failed to set roles")
emoteAction.queue(emote -> emote.getManager().setRoles(getRoles(ctx.getGuild(), options, "role1", "role2", "role3", "role4", "role5", "role6", "role7", "role8", "role9")).queue(
success -> ctx.reply("Successfully set roles"),
error -> ctx.error("Failed to set roles")
),
error -> ia.error("Emote not found in this guild")
error -> ctx.error("Emote not found in this guild")
);
}

Expand All @@ -81,21 +81,21 @@ public ResetCommand(){
}

@Override
public void run(Options options, GuildInteraction ia){
if(!ia.getSelfMember().hasPermission(Permission.MANAGE_EMOTES)){
ia.error("I can't manage emotes due to lack of permissions. Please give me the `MANAGE_EMOTES` permission to use this command.");
public void run(Options options, GuildCommandContext ctx){
if(!ctx.getSelfMember().hasPermission(Permission.MANAGE_EMOTES)){
ctx.error("I can't manage emotes due to lack of permissions. Please give me the `MANAGE_EMOTES` permission to use this command.");
return;
}
var emoteAction = options.getEmote(ia.getGuild(), "emote");
var emoteAction = options.getEmote(ctx.getGuild(), "emote");
if(emoteAction == null){
ia.error("Failed to parse emote: `" + options.getString("emote") + "`");
ctx.error("Failed to parse emote: `" + options.getString("emote") + "`");
return;
}
emoteAction.queue(emote -> emote.getManager().setRoles(new HashSet<>()).queue(
success -> ia.reply("Successfully reset emote"),
error -> ia.error("Failed to reset emote")
success -> ctx.reply("Successfully reset emote"),
error -> ctx.error("Failed to reset emote")
),
error -> ia.error("Emote not found in this guild")
error -> ctx.error("Emote not found in this guild")
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.kittybot.kittybot.commands.admin.ban;

import de.kittybot.kittybot.slashcommands.GuildCommandContext;
import de.kittybot.kittybot.slashcommands.Options;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionInteger;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionString;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionUser;
import de.kittybot.kittybot.slashcommands.application.options.GuildSubCommand;
import de.kittybot.kittybot.slashcommands.interaction.GuildInteraction;
import de.kittybot.kittybot.slashcommands.interaction.Options;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.utils.MarkdownSanitizer;

Expand All @@ -21,27 +21,27 @@ public AddCommand(){
}

@Override
public void run(Options options, GuildInteraction ia){
if(!ia.getSelfMember().hasPermission(Permission.BAN_MEMBERS)){
ia.error("I don't have the required permission to ban members");
public void run(Options options, GuildCommandContext ctx){
if(!ctx.getSelfMember().hasPermission(Permission.BAN_MEMBERS)){
ctx.error("I don't have the required permission to ban members");
return;
}
var user = options.getUser("user");
if(user.getIdLong() == ia.getUserId()){
ia.error("You can't ban yourself");
if(user.getIdLong() == ctx.getUserId()){
ctx.error("You can't ban yourself");
}
var member = options.getMember("user");
if(member != null){
if(!ia.getSelfMember().canInteract(member)){
ia.error("I can't interact with this member");
if(!ctx.getSelfMember().canInteract(member)){
ctx.error("I can't interact with this member");
return;
}
}
var reason = options.has("reason") ? options.getString("reason") : "Banned by " + ia.getMember().getAsMention();
var reason = options.has("reason") ? options.getString("reason") : "Banned by " + ctx.getMember().getAsMention();
var delDays = options.has("del-days") ? options.getInt("del-days") : 0;
ia.getGuild().ban(user, delDays, reason).reason(reason).queue(success ->
ia.reply("Banned `" + MarkdownSanitizer.escape(user.getAsTag()) + "`(`" + user.getId() + "`)\nReason: " + reason + "\nDeleted messages of last " + delDays + " days"),
error -> ia.error("Failed to ban " + user.getAsMention() + ".\nReason: `" + error.getMessage() + "`")
ctx.getGuild().ban(user, delDays, reason).reason(reason).queue(success ->
ctx.reply("Banned `" + MarkdownSanitizer.escape(user.getAsTag()) + "`(`" + user.getId() + "`)\nReason: " + reason + "\nDeleted messages of last " + delDays + " days"),
error -> ctx.error("Failed to ban " + user.getAsMention() + ".\nReason: `" + error.getMessage() + "`")
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package de.kittybot.kittybot.commands.admin.ban;

import de.kittybot.kittybot.slashcommands.application.options.GuildSubCommand;
import de.kittybot.kittybot.slashcommands.interaction.GuildInteraction;
import de.kittybot.kittybot.slashcommands.interaction.Options;
import de.kittybot.kittybot.slashcommands.GuildCommandContext;
import de.kittybot.kittybot.slashcommands.Options;
import net.dv8tion.jda.api.utils.MarkdownSanitizer;

import java.util.stream.Collectors;
Expand All @@ -14,14 +14,14 @@ public ListCommand(){
}

@Override
public void run(Options options, GuildInteraction ia){
ia.getGuild().retrieveBanList().queue(bans -> {
public void run(Options options, GuildCommandContext ctx){
ctx.getGuild().retrieveBanList().queue(bans -> {
if(bans.isEmpty()){
ia.reply("There are no banned users yet");
ctx.reply("There are no banned users yet");
return;
}
ia.reply("**Banned Users:**\n" + bans.stream().map(ban -> MarkdownSanitizer.escape(ban.getUser().getAsTag()) + "(`" + ban.getUser().getId() + "`)" + " - " + ban.getReason()).collect(Collectors.joining("\n")));
}, error -> ia.error("I was not able to retrieve the bans. Please give me the `ban members` permission")
ctx.reply("**Banned Users:**\n" + bans.stream().map(ban -> MarkdownSanitizer.escape(ban.getUser().getAsTag()) + "(`" + ban.getUser().getId() + "`)" + " - " + ban.getReason()).collect(Collectors.joining("\n")));
}, error -> ctx.error("I was not able to retrieve the bans. Please give me the `ban members` permission")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionString;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionUser;
import de.kittybot.kittybot.slashcommands.application.options.GuildSubCommand;
import de.kittybot.kittybot.slashcommands.interaction.GuildInteraction;
import de.kittybot.kittybot.slashcommands.interaction.Options;
import de.kittybot.kittybot.slashcommands.GuildCommandContext;
import de.kittybot.kittybot.slashcommands.Options;

public class RemoveCommand extends GuildSubCommand{

Expand All @@ -17,16 +17,16 @@ public RemoveCommand(){
}

@Override
public void run(Options options, GuildInteraction ia){
public void run(Options options, GuildCommandContext ctx){
var user = options.getUser("user");
if(user == null){
ia.error("Please provide a valid user id");
ctx.error("Please provide a valid user id");
return;
}
var reason = options.has("reason") ? options.getString("reason") : "Unbanned by " + ia.getMember().getAsMention();
ia.getGuild().unban(user).reason(reason).queue(success ->
ia.reply("Unbanned " + user.getAsMention() + " with reason: " + reason),
error -> ia.error("Failed to unban " + user.getAsMention() + " for reason: `" + error.getMessage() + "`")
var reason = options.has("reason") ? options.getString("reason") : "Unbanned by " + ctx.getMember().getAsMention();
ctx.getGuild().unban(user).reason(reason).queue(success ->
ctx.reply("Unbanned " + user.getAsMention() + " with reason: " + reason),
error -> ctx.error("Failed to unban " + user.getAsMention() + " for reason: `" + error.getMessage() + "`")
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package de.kittybot.kittybot.commands.admin.ignore.channel;

import de.kittybot.kittybot.modules.SettingsModule;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionChannel;
import de.kittybot.kittybot.slashcommands.GuildCommandContext;
import de.kittybot.kittybot.slashcommands.Options;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionGuildChannel;
import de.kittybot.kittybot.slashcommands.application.options.GuildSubCommand;
import de.kittybot.kittybot.slashcommands.interaction.GuildInteraction;
import de.kittybot.kittybot.slashcommands.interaction.Options;

public class AddCommand extends GuildSubCommand{

public AddCommand(){
super("add", "Used to disable a channel");
addOptions(
new CommandOptionChannel("channel", "Channel to disable").required()
new CommandOptionGuildChannel("channel", "Channel to disable").required()
);
}

@Override
public void run(Options options, GuildInteraction ia){
public void run(Options options, GuildCommandContext ctx){
var channel = options.getTextChannel("channel");
ia.get(SettingsModule.class).setBotDisabledInChannel(ia.getGuildId(), channel.getIdLong(), true);
ia.reply("Disabled commands in: " + channel.getAsMention());
ctx.get(SettingsModule.class).setBotDisabledInChannel(ctx.getGuildId(), channel.getIdLong(), true);
ctx.reply("Disabled commands in: " + channel.getAsMention());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import de.kittybot.kittybot.modules.SettingsModule;
import de.kittybot.kittybot.slashcommands.application.options.GuildSubCommand;
import de.kittybot.kittybot.slashcommands.interaction.GuildInteraction;
import de.kittybot.kittybot.slashcommands.interaction.Options;
import de.kittybot.kittybot.slashcommands.GuildCommandContext;
import de.kittybot.kittybot.slashcommands.Options;
import de.kittybot.kittybot.utils.MessageUtils;

import java.util.stream.Collectors;
Expand All @@ -15,12 +15,12 @@ public ListCommand(){
}

@Override
public void run(Options options, GuildInteraction ia){
var channels = ia.get(SettingsModule.class).getBotDisabledChannels(ia.getGuildId());
public void run(Options options, GuildCommandContext ctx){
var channels = ctx.get(SettingsModule.class).getBotDisabledChannels(ctx.getGuildId());
if(channels.isEmpty()){
ia.reply("No disabled channels configured yet");
ctx.reply("No disabled channels configured yet");
}
ia.reply("**Disabled following channels:**\n" + channels.stream().map(MessageUtils::getChannelMention).collect(Collectors.joining(", ")));
ctx.reply("**Disabled following channels:**\n" + channels.stream().map(MessageUtils::getChannelMention).collect(Collectors.joining(", ")));
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package de.kittybot.kittybot.commands.admin.ignore.channel;

import de.kittybot.kittybot.modules.SettingsModule;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionChannel;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionGuildChannel;
import de.kittybot.kittybot.slashcommands.application.options.GuildSubCommand;
import de.kittybot.kittybot.slashcommands.interaction.GuildInteraction;
import de.kittybot.kittybot.slashcommands.interaction.Options;
import de.kittybot.kittybot.slashcommands.GuildCommandContext;
import de.kittybot.kittybot.slashcommands.Options;

public class RemoveCommand extends GuildSubCommand{

public RemoveCommand(){
super("remove", "Used to enable a channel");
addOptions(
new CommandOptionChannel("channel", "Channel to enable").required()
new CommandOptionGuildChannel("channel", "Channel to enable").required()
);
}

@Override
public void run(Options options, GuildInteraction ia){
public void run(Options options, GuildCommandContext ctx){
var channel = options.getTextChannel("channel");
ia.get(SettingsModule.class).setBotDisabledInChannel(ia.getGuildId(), channel.getIdLong(), false);
ia.reply("Enabled commands in: " + channel.getAsMention());
ctx.get(SettingsModule.class).setBotDisabledInChannel(ctx.getGuildId(), channel.getIdLong(), false);
ctx.reply("Enabled commands in: " + channel.getAsMention());
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package de.kittybot.kittybot.commands.admin.ignore.user;

import de.kittybot.kittybot.modules.SettingsModule;
import de.kittybot.kittybot.slashcommands.GuildCommandContext;
import de.kittybot.kittybot.slashcommands.Options;
import de.kittybot.kittybot.slashcommands.application.options.CommandOptionUser;
import de.kittybot.kittybot.slashcommands.application.options.GuildSubCommand;
import de.kittybot.kittybot.slashcommands.interaction.GuildInteraction;
import de.kittybot.kittybot.slashcommands.interaction.Options;

import java.util.Collections;

Expand All @@ -18,10 +18,10 @@ public AddCommand(){
}

@Override
public void run(Options options, GuildInteraction ia){
public void run(Options options, GuildCommandContext ctx){
var user = options.getUser("user");
ia.get(SettingsModule.class).addBotIgnoredUsers(ia.getGuildId(), Collections.singleton(user.getIdLong()));
ia.reply("Disabled commands for: " + user.getAsMention());
ctx.get(SettingsModule.class).addBotIgnoredUsers(ctx.getGuildId(), Collections.singleton(user.getIdLong()));
ctx.reply("Disabled commands for: " + user.getAsMention());
}

}
Loading