/
DiscordBanCommand.java
92 lines (85 loc) · 4.52 KB
/
DiscordBanCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.denizenscript.ddiscordbot.commands;
import com.denizenscript.ddiscordbot.DiscordCommandUtils;
import com.denizenscript.ddiscordbot.objects.DiscordBotTag;
import com.denizenscript.ddiscordbot.objects.DiscordGroupTag;
import com.denizenscript.ddiscordbot.objects.DiscordUserTag;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.scripts.commands.generator.*;
import net.dv8tion.jda.api.entities.UserSnowflake;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
import java.util.concurrent.TimeUnit;
public class DiscordBanCommand extends AbstractCommand implements Holdable {
public DiscordBanCommand() {
setName("discordban");
setSyntax("discordban (id:<bot>) ({add}/remove) [user:<user>] [group:<group>] (reason:<reason>) (deletion_timeframe:<time>/{0s})");
setRequiredArguments(2, 6);
isProcedural = false;
autoCompile();
}
// <--[command]
// @Name discordban
// @Syntax discordban (id:<bot>) ({add}/remove) [user:<user>] [group:<group>] (reason:<reason>) (deletion_timeframe:<time>/{0s})
// @Required 2
// @Maximum 6
// @Short Bans or unbans a member from a group.
// @Plugin dDiscordBot
// @Guide https://guide.denizenscript.com/guides/expanding/ddiscordbot.html
// @Group external
//
// @Description
// Bans or unbans a member from a group.
//
// To ban a user, use the "add" argument. To unban a user, use the "remove" argument.
// The group is required for both "add" and "remove" arguments, but "reason" can only be used with "add".
// Reasons show up in the group's Audit Logs.
//
// The "deletion_timeframe" argument will, if set, delete all messages sent by the user being banned within the timeframe given.
// The timeframe defaults to 0 seconds, which will not delete any messages. The timeframe cannot be greater than 7 days.
// This argument can only be used when adding a ban using the "add" argument, although it is not required.
//
// The command should usually be ~waited for. See <@link language ~waitable>.
//
// @Tags
// <DiscordUserTag.is_banned[<group>]> returns if the user is banned from a certain group.
// <DiscordGroupTag.banned_members> returns a list of all banned members in a group.
//
// @Usage
// # Bans a user with a reason and deletes all messages sent by the user in the past 2 hours.
// - ~discordban id:mybot add user:<[user]> group:<[group]> "reason:Was being mean!" deletion_timeframe:2h
//
// @Usage
// # Bans a user with but does not delete any messages sent and does not have a reason.
// - ~discordban id:mybot add user:<[user]> group:<[group]>
//
// @Usage
// # Unbans a user.
// - ~discordban id:mybot remove user:<[user]> group:<[group]>
// -->
public enum DiscordBanInstruction { ADD, REMOVE }
public static void autoExecute(ScriptEntry scriptEntry,
@ArgPrefixed @ArgName("id") @ArgDefaultNull DiscordBotTag bot,
@ArgName("instruction") @ArgDefaultText("add") DiscordBanInstruction instruction,
@ArgPrefixed @ArgName("user") DiscordUserTag user,
@ArgPrefixed @ArgName("group") DiscordGroupTag group,
@ArgPrefixed @ArgDefaultNull @ArgName("reason") String reason,
@ArgPrefixed @ArgDefaultText("0s") @ArgName("deletion_timeframe") DurationTag deletionTimeframe) {
bot = DiscordCommandUtils.inferBot(bot, group, user);
if (group.bot == null) {
group = new DiscordGroupTag(bot.bot, group.guild_id);
}
UserSnowflake userObj = UserSnowflake.fromId(user.user_id);
DiscordCommandUtils.cleanWait(scriptEntry, switch (instruction) {
case ADD -> {
AuditableRestAction<Void> banAction = group.getGuild().ban(userObj, deletionTimeframe.getSecondsAsInt(), TimeUnit.SECONDS);
if (reason != null) {
banAction = banAction.reason(reason);
}
yield banAction;
}
case REMOVE -> group.getGuild().unban(userObj);
});
}
}