Implement basic ban command
This commit is contained in:
parent
420666eab1
commit
35b2c8fb42
@ -242,7 +242,7 @@ public class Cache
|
||||
try {
|
||||
Field field = Color.class.getField(colorName);
|
||||
color = (Color)field.get(null);
|
||||
} catch (Exception e) {
|
||||
} catch (RuntimeException | NoSuchFieldException | IllegalAccessException e) {
|
||||
logger.log("Unknown color: " + colorName);
|
||||
}
|
||||
return color == null ? defaultColor : color;
|
||||
|
@ -138,6 +138,7 @@ public class HidekoBot
|
||||
messageCommandListener.registerCommand(new HelloCommand());
|
||||
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.AliasCommand());
|
||||
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.AvatarCommand());
|
||||
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BanCommand());
|
||||
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BannerCommand());
|
||||
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BotInfoCommand());
|
||||
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.CoinFlipCommand());
|
||||
|
@ -132,9 +132,8 @@ public class ClearChat
|
||||
which are restricted by discord, and thus has to use
|
||||
a less efficient way that triggers rate-limiting very quickly. */
|
||||
|
||||
} catch (Exception e)
|
||||
} catch (RuntimeException ignored)
|
||||
{
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,157 @@
|
||||
package wtf.beatrice.hidekobot.commands.base;
|
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.IMentionable;
|
||||
import net.dv8tion.jda.api.entities.Mentions;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
|
||||
import org.apache.commons.text.WordUtils;
|
||||
import wtf.beatrice.hidekobot.Cache;
|
||||
import wtf.beatrice.hidekobot.HidekoBot;
|
||||
import wtf.beatrice.hidekobot.objects.MessageResponse;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class UserPunishment
|
||||
{
|
||||
public static void handle(MessageReceivedEvent event, String[] args, PunishmentType punishmentType)
|
||||
{
|
||||
Mentions msgMentions = event.getMessage().getMentions();
|
||||
List<IMentionable> mentions = msgMentions.getMentions();
|
||||
|
||||
MessageResponse response = getResponse(event.getAuthor(),
|
||||
punishmentType,
|
||||
event.getChannel(),
|
||||
mentions,
|
||||
args);
|
||||
|
||||
if(response.embed() != null)
|
||||
event.getMessage().replyEmbeds(response.embed()).queue();
|
||||
else if(response.content() != null)
|
||||
event.getMessage().reply(response.content()).queue();
|
||||
}
|
||||
|
||||
public static MessageResponse getResponse(User author,
|
||||
PunishmentType punishmentType,
|
||||
MessageChannelUnion channel,
|
||||
List<IMentionable> mentions,
|
||||
String[] args)
|
||||
{
|
||||
String punishmentTypeName = punishmentType.name().toLowerCase();
|
||||
|
||||
|
||||
if(!(channel instanceof TextChannel))
|
||||
{
|
||||
// todo nicer looking with emojis
|
||||
return new MessageResponse("Sorry! I can't " + punishmentTypeName + " people in DMs.", null);
|
||||
}
|
||||
|
||||
if(mentions.isEmpty())
|
||||
{
|
||||
// todo nicer looking with emojis
|
||||
return new MessageResponse("You have to tell me who to " + punishmentTypeName + "!", null);
|
||||
}
|
||||
|
||||
String mentionedId = mentions.get(0).getId();
|
||||
User mentioned = null;
|
||||
|
||||
try {
|
||||
mentioned = HidekoBot.getAPI().retrieveUserById(mentionedId).complete();
|
||||
} catch (RuntimeException ignored)
|
||||
{
|
||||
// todo nicer looking with emojis
|
||||
return new MessageResponse("I can't " + punishmentTypeName + " that user!", null);
|
||||
}
|
||||
|
||||
StringBuilder reasonBuilder = new StringBuilder();
|
||||
String reason = "";
|
||||
if(args.length > 1)
|
||||
{
|
||||
for(int i = 1; i < args.length; i++)
|
||||
{
|
||||
String arg = args[i];
|
||||
reasonBuilder.append(arg);
|
||||
|
||||
if(i + 1 != arg.length())
|
||||
reasonBuilder.append(" "); // separate args with a space except on last iteration.
|
||||
}
|
||||
|
||||
reason = reasonBuilder.toString();
|
||||
}
|
||||
|
||||
if(mentioned == null)
|
||||
{
|
||||
// todo nicer looking with emojis
|
||||
return new MessageResponse("I can't " + punishmentTypeName + " that user!", null);
|
||||
}
|
||||
|
||||
Guild guild = ((TextChannel) channel).getGuild();
|
||||
|
||||
AuditableRestAction<Void> punishmentAction = null;
|
||||
|
||||
try {
|
||||
switch (punishmentType) {
|
||||
case BAN -> punishmentAction = guild.ban(mentioned, 0, TimeUnit.SECONDS);
|
||||
case KICK -> punishmentAction = guild.kick(mentioned);
|
||||
}
|
||||
} catch (RuntimeException ignored) {
|
||||
// todo nicer looking with emojis
|
||||
return new MessageResponse("Sorry, I couldn't " + punishmentTypeName + " " + mentioned.getAsMention() + "!",
|
||||
null);
|
||||
}
|
||||
|
||||
if(!reason.isEmpty() && !reasonBuilder.isEmpty()) punishmentAction.reason(reason);
|
||||
|
||||
try {
|
||||
punishmentAction.complete();
|
||||
} catch (RuntimeException ignored)
|
||||
{
|
||||
// todo nicer looking with emojis
|
||||
return new MessageResponse("Sorry, I couldn't " + punishmentTypeName + " " + mentioned.getAsMention() + "!",
|
||||
null);
|
||||
}
|
||||
|
||||
EmbedBuilder embedBuilder = new EmbedBuilder();
|
||||
|
||||
embedBuilder.setAuthor(author.getAsTag(), null, author.getAvatarUrl());
|
||||
embedBuilder.setColor(Cache.getBotColor());
|
||||
embedBuilder.setTitle("User " + punishmentType.getPastTense());
|
||||
|
||||
embedBuilder.addField("\uD83D\uDC64 User", mentioned.getAsMention(), true);
|
||||
embedBuilder.addField("✂️ By", author.getAsMention(), true);
|
||||
|
||||
if(reason.isEmpty())
|
||||
reason = "*No reason specified*";
|
||||
|
||||
embedBuilder.addField("\uD83D\uDCD6 Reason", reason, false);
|
||||
|
||||
|
||||
return new MessageResponse(null, embedBuilder.build());
|
||||
|
||||
}
|
||||
|
||||
public enum PunishmentType {
|
||||
KICK("kicked"),
|
||||
BAN("banned"),
|
||||
|
||||
;
|
||||
|
||||
private final String pastTense;
|
||||
|
||||
PunishmentType(String pastTense)
|
||||
{
|
||||
this.pastTense = pastTense;
|
||||
}
|
||||
|
||||
public String getPastTense()
|
||||
{
|
||||
return pastTense;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package wtf.beatrice.hidekobot.commands.message;
|
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.IMentionable;
|
||||
import net.dv8tion.jda.api.entities.Mentions;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import wtf.beatrice.hidekobot.Cache;
|
||||
import wtf.beatrice.hidekobot.HidekoBot;
|
||||
import wtf.beatrice.hidekobot.commands.base.UserPunishment;
|
||||
import wtf.beatrice.hidekobot.objects.MessageResponse;
|
||||
import wtf.beatrice.hidekobot.objects.commands.CommandCategory;
|
||||
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class BanCommand implements MessageCommand
|
||||
{
|
||||
|
||||
@Override
|
||||
public LinkedList<String> getCommandLabels() {
|
||||
return new LinkedList<>(Collections.singletonList("ban"));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public List<Permission> getPermissions() {
|
||||
return new ArrayList<Permission>(Collections.singletonList(Permission.BAN_MEMBERS));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean passRawArgs() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
return CommandCategory.MODERATION;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Ban the mentioned user.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "<mentioned user> [reason]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runCommand(MessageReceivedEvent event, String label, String[] args)
|
||||
{
|
||||
UserPunishment.handle(event, args, UserPunishment.PunishmentType.BAN);
|
||||
}
|
||||
}
|
@ -69,9 +69,7 @@ public class InviteCommand implements MessageCommand
|
||||
.addActionRow(inviteButton)
|
||||
.queue();
|
||||
event.getMessage().addReaction(Emoji.fromUnicode("✅")).queue();
|
||||
}, (error) -> {
|
||||
event.getMessage().addReaction(Emoji.fromUnicode("❌")).queue();
|
||||
});
|
||||
}, error -> event.getMessage().addReaction(Emoji.fromUnicode("❌")).queue());
|
||||
} else {
|
||||
event.getMessage()
|
||||
.replyEmbeds(inviteEmbed)
|
||||
|
@ -12,6 +12,8 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import wtf.beatrice.hidekobot.Cache;
|
||||
import wtf.beatrice.hidekobot.HidekoBot;
|
||||
import wtf.beatrice.hidekobot.commands.base.UserPunishment;
|
||||
import wtf.beatrice.hidekobot.objects.MessageResponse;
|
||||
import wtf.beatrice.hidekobot.objects.commands.CommandCategory;
|
||||
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
|
||||
|
||||
@ -60,87 +62,6 @@ public class KickCommand implements MessageCommand
|
||||
@Override
|
||||
public void runCommand(MessageReceivedEvent event, String label, String[] args)
|
||||
{
|
||||
if(!(event.getChannel() instanceof TextChannel))
|
||||
{
|
||||
// todo nicer looking with emojis
|
||||
event.getMessage().reply("Sorry! I can't kick people in DMs.").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
Mentions msgMentions = event.getMessage().getMentions();
|
||||
List<IMentionable> mentions = msgMentions.getMentions();
|
||||
|
||||
if(args.length == 0 ||mentions.isEmpty())
|
||||
{
|
||||
// todo nicer looking with emojis
|
||||
event.getMessage().reply("You have to tell me who to kick!").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
String mentionedId = mentions.get(0).getId();
|
||||
User mentioned = null;
|
||||
|
||||
try {
|
||||
mentioned = HidekoBot.getAPI().retrieveUserById(mentionedId).complete();
|
||||
} catch (Exception e)
|
||||
{
|
||||
// todo nicer looking with emojis
|
||||
event.getMessage().reply("I can't kick that user!").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder reasonBuilder = new StringBuilder();;
|
||||
String reason = "";
|
||||
if(args.length > 1)
|
||||
{
|
||||
for(int i = 1; i < args.length; i++)
|
||||
{
|
||||
String arg = args[i];
|
||||
reasonBuilder.append(arg);
|
||||
|
||||
if(i + 1 != arg.length())
|
||||
reasonBuilder.append(" "); // separate args with a space except on last iteration.
|
||||
}
|
||||
|
||||
reason = reasonBuilder.toString();
|
||||
}
|
||||
|
||||
if(mentioned == null)
|
||||
{
|
||||
// todo nicer looking with emojis
|
||||
event.getMessage().reply("I can't kick that user!").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
AuditableRestAction<Void> kickAction = event.getGuild().kick(mentioned);
|
||||
if(!reason.isEmpty() && !reasonBuilder.isEmpty()) kickAction.reason(reason);
|
||||
|
||||
final String finalReason = reason;
|
||||
final IMentionable finalMentioned = mentioned;
|
||||
|
||||
kickAction.queue(success -> {
|
||||
String embedReason = finalReason;
|
||||
|
||||
EmbedBuilder embedBuilder = new EmbedBuilder();
|
||||
|
||||
embedBuilder.setAuthor(event.getAuthor().getAsTag(), null,event.getAuthor().getAvatarUrl());
|
||||
embedBuilder.setColor(Cache.getBotColor());
|
||||
embedBuilder.setTitle("User kicked");
|
||||
|
||||
embedBuilder.addField("\uD83D\uDC64 User", finalMentioned.getAsMention(), true);
|
||||
embedBuilder.addField("✂️ By", event.getAuthor().getAsMention(), true);
|
||||
|
||||
if(embedReason.isEmpty())
|
||||
embedReason = "*No reason specified*";
|
||||
|
||||
embedBuilder.addField("\uD83D\uDCD6 Reason", embedReason, false);
|
||||
|
||||
|
||||
event.getMessage().replyEmbeds(embedBuilder.build()).queue();
|
||||
}, throwable -> {
|
||||
// todo nicer looking with emojis
|
||||
event.getMessage().reply("Sorry, I couldn't kick " + finalMentioned.getAsMention() + "!").queue();
|
||||
});
|
||||
|
||||
UserPunishment.handle(event, args, UserPunishment.PunishmentType.KICK);
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ public class LoveCalculatorCommand extends SlashCommandImpl
|
||||
OptionMapping firsUserArg = event.getOption("first");
|
||||
if(firsUserArg != null)
|
||||
{
|
||||
firstUser = firsUserArg.getAsUser();
|
||||
firstUser = firsUserArg.getAsUser(); //todo null check?
|
||||
} else {
|
||||
event.reply("\uD83D\uDE22 I need to know who to check! Please mention them.")
|
||||
.setEphemeral(true)
|
||||
@ -50,7 +50,7 @@ public class LoveCalculatorCommand extends SlashCommandImpl
|
||||
OptionMapping secondUserArg = event.getOption("second");
|
||||
if(secondUserArg != null)
|
||||
{
|
||||
secondUser = secondUserArg.getAsUser();
|
||||
secondUser = secondUserArg.getAsUser(); //todo null check?
|
||||
} else {
|
||||
secondUser = event.getUser();
|
||||
}
|
||||
|
@ -230,8 +230,6 @@ public class CommandUtil
|
||||
databaseSource.untrackExpiredMessage(messageId);
|
||||
},
|
||||
|
||||
(error) -> {
|
||||
databaseSource.untrackExpiredMessage(messageId);
|
||||
});
|
||||
error -> databaseSource.untrackExpiredMessage(messageId));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user