1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-11-10 02:21:25 +00:00

Add informative DM when being kicked

This commit is contained in:
Arnold Alejo Nunag 2020-09-29 21:04:06 +08:00
parent 38b7ac9a26
commit b02f38c70f
Signed by: sciwhiz12
GPG Key ID: 622CF446534317E1
7 changed files with 37 additions and 23 deletions

View File

@ -36,6 +36,7 @@ public class BotConsole {
String[] parts = input.split(" "); String[] parts = input.split(" ");
switch (parts[0]) { switch (parts[0]) {
case "shutdown": { case "shutdown": {
running = false;
bot.shutdown(); bot.shutdown();
break; break;
} }

View File

@ -10,6 +10,8 @@ import sciwhiz12.janitor.commands.CommandRegistry;
import sciwhiz12.janitor.config.BotConfig; import sciwhiz12.janitor.config.BotConfig;
import sciwhiz12.janitor.utils.Util; import sciwhiz12.janitor.utils.Util;
import java.util.concurrent.CompletableFuture;
import static sciwhiz12.janitor.Logging.JANITOR; import static sciwhiz12.janitor.Logging.JANITOR;
import static sciwhiz12.janitor.Logging.STATUS; import static sciwhiz12.janitor.Logging.STATUS;
@ -74,8 +76,7 @@ public class JanitorBot {
JANITOR JANITOR
.error(STATUS, "Error while sending shutdown message to owner", err) .error(STATUS, "Error while sending shutdown message to owner", err)
)) ))
.join() ).ifPresent(CompletableFuture::join);
);
discord.shutdown(); discord.shutdown();
} }
} }

View File

@ -5,6 +5,7 @@ import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import sciwhiz12.janitor.commands.BaseCommand; import sciwhiz12.janitor.commands.BaseCommand;
@ -47,20 +48,19 @@ public class KickCommand extends BaseCommand {
} }
private int runWithReason(CommandContext<MessageReceivedEvent> ctx, @Nullable String reason) throws CommandSyntaxException { private int runWithReason(CommandContext<MessageReceivedEvent> ctx, @Nullable String reason) throws CommandSyntaxException {
MessageChannel channel = ctx.getSource().getChannel();
if (!ctx.getSource().isFromGuild()) { if (!ctx.getSource().isFromGuild()) {
General.guildOnlyCommand(ctx.getSource().getTextChannel()).queue(); General.guildOnlyCommand(channel).queue();
return 1; return 1;
} }
Member performer = ctx.getSource().getMember(); Member performer = ctx.getSource().getMember();
if (performer == null) return 1; if (performer == null) return 1;
List<Member> members = getMembers("member", ctx).fromGuild(ctx.getSource().getGuild()); List<Member> members = getMembers("member", ctx).fromGuild(performer.getGuild());
if (members.size() > 1) {
General.ambiguousMember(ctx.getSource().getTextChannel()).queue();
return 1;
}
Member target = members.get(0); Member target = members.get(0);
if (ModerationHelper.ensurePermissions(ctx.getSource().getTextChannel(), performer, target, KICK_PERMISSION)) { if (ModerationHelper.ensurePermissions(channel, performer, target, KICK_PERMISSION)) {
ModerationHelper.kickUser(target.getGuild(), performer, target, reason) target.getUser().openPrivateChannel()
.flatMap(dm -> Moderation.kickedDM(dm, performer, target, reason))
.flatMap(v -> ModerationHelper.kickUser(target.getGuild(), performer, target, reason))
.flatMap(v -> Moderation.kickUser(ctx.getSource().getChannel(), performer, target, reason)) .flatMap(v -> Moderation.kickUser(ctx.getSource().getChannel(), performer, target, reason))
.queue(); .queue();
} }

View File

@ -6,7 +6,7 @@ import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import sciwhiz12.janitor.msg.General; import sciwhiz12.janitor.msg.General;
@ -21,7 +21,7 @@ public class CommandHelper {
return RequiredArgumentBuilder.argument(command, argument); return RequiredArgumentBuilder.argument(command, argument);
} }
public static boolean canInteract(TextChannel response, Member target) { public static boolean canInteract(MessageChannel response, Member target) {
if (!target.getGuild().getSelfMember().canInteract(target)) { if (!target.getGuild().getSelfMember().canInteract(target)) {
General.cannotInteract(response, target).queue(); General.cannotInteract(response, target).queue();
return false; return false;
@ -29,7 +29,7 @@ public class CommandHelper {
return true; return true;
} }
public static boolean hasPermission(TextChannel response, Guild guild, EnumSet<Permission> permissions) { public static boolean hasPermission(MessageChannel response, Guild guild, EnumSet<Permission> permissions) {
if (!guild.getSelfMember().hasPermission(permissions)) { if (!guild.getSelfMember().hasPermission(permissions)) {
General.insufficientPermissions(response, permissions).queue(); General.insufficientPermissions(response, permissions).queue();
return false; return false;

View File

@ -3,7 +3,7 @@ package sciwhiz12.janitor.commands.util;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import sciwhiz12.janitor.msg.General; import sciwhiz12.janitor.msg.General;
@ -20,7 +20,7 @@ public class ModerationHelper {
return guild.kick(target, auditReason.toString()); return guild.kick(target, auditReason.toString());
} }
public static boolean ensurePermissions(TextChannel channel, Member performer, Member target, EnumSet<Permission> permissions) { public static boolean ensurePermissions(MessageChannel channel, Member performer, Member target, EnumSet<Permission> permissions) {
if (!CommandHelper.hasPermission(channel, target.getGuild(), permissions)) return false; if (!CommandHelper.hasPermission(channel, target.getGuild(), permissions)) return false;
if (!CommandHelper.canInteract(channel, target)) return false; if (!CommandHelper.canInteract(channel, target)) return false;
if (!performer.hasPermission(permissions)) { if (!performer.hasPermission(permissions)) {

View File

@ -14,7 +14,7 @@ public final class General {
private General() { private General() {
} }
public static RestAction<Message> guildOnlyCommand(TextChannel channel) { public static RestAction<Message> guildOnlyCommand(MessageChannel channel) {
return channel.sendMessage( return channel.sendMessage(
new EmbedBuilder() new EmbedBuilder()
.setTitle("Guild only command!") .setTitle("Guild only command!")
@ -24,7 +24,7 @@ public final class General {
); );
} }
public static RestAction<Message> insufficientPermissions(TextChannel channel, EnumSet<Permission> permissions) { public static RestAction<Message> insufficientPermissions(MessageChannel channel, EnumSet<Permission> permissions) {
return channel.sendMessage( return channel.sendMessage(
new EmbedBuilder() new EmbedBuilder()
.setTitle("I have insufficient permissions!") .setTitle("I have insufficient permissions!")
@ -39,7 +39,7 @@ public final class General {
); );
} }
public static RestAction<Message> ambiguousMember(TextChannel channel) { public static RestAction<Message> ambiguousMember(MessageChannel channel) {
return channel.sendMessage( return channel.sendMessage(
new EmbedBuilder() new EmbedBuilder()
.setTitle("Ambiguous member argument!") .setTitle("Ambiguous member argument!")
@ -50,7 +50,7 @@ public final class General {
); );
} }
public static RestAction<Message> cannotInteract(TextChannel channel, Member target) { public static RestAction<Message> cannotInteract(MessageChannel channel, Member target) {
return channel.sendMessage( return channel.sendMessage(
new EmbedBuilder() new EmbedBuilder()
.setTitle("Member is higher than me!") .setTitle("Member is higher than me!")

View File

@ -15,6 +15,7 @@ import static sciwhiz12.janitor.msg.General.nameFor;
public final class Moderation { public final class Moderation {
public static final int MODERATION_COLOR = 0xF1BD25; public static final int MODERATION_COLOR = 0xF1BD25;
public static final String GAVEL_ICON_URL = "https://cdn.discordapp.com/attachments/738478941760782526/760463743330549760/gavel.png";
private Moderation() { private Moderation() {
} }
@ -48,14 +49,25 @@ public final class Moderation {
public static MessageAction kickUser(MessageChannel channel, Member performer, Member target, @Nullable String reason) { public static MessageAction kickUser(MessageChannel channel, Member performer, Member target, @Nullable String reason) {
final EmbedBuilder embed = new EmbedBuilder() final EmbedBuilder embed = new EmbedBuilder()
.setTitle("Kicked.") .setAuthor("Kicked user from server.", null, GAVEL_ICON_URL)
.addField("Performer", nameFor(performer.getUser()), true) .addField("Moderator", nameFor(performer.getUser()), true)
.addField("Target", nameFor(target.getUser()), true); .addField("Target", nameFor(target.getUser()), true);
if (reason != null) if (reason != null)
embed.addField("Reason", reason, false); embed.addField("Reason", reason, false);
return channel.sendMessage( return channel.sendMessage(
embed.setColor(MODERATION_COLOR) embed.setColor(MODERATION_COLOR).build()
.build() );
}
public static MessageAction kickedDM(MessageChannel channel, Member performer, Member target, @Nullable String reason) {
final EmbedBuilder embed = new EmbedBuilder()
.setAuthor(performer.getGuild().getName(), null, performer.getGuild().getIconUrl())
.setTitle("You were kicked from this server.")
.addField("Moderator", nameFor(performer.getUser()), true);
if (reason != null)
embed.addField("Reason", reason, false);
return channel.sendMessage(
embed.setColor(MODERATION_COLOR).build()
); );
} }
} }