-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordBanCommand.java
111 lines (104 loc) · 5.13 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.denizenscript.ddiscordbot.commands;
import com.denizenscript.ddiscordbot.DenizenDiscordBot;
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 com.denizenscript.denizencore.utilities.debugging.Debug;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.UserSnowflake;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
import org.bukkit.Bukkit;
import java.util.concurrent.TimeUnit;
public class DiscordBanCommand extends AbstractCommand implements Holdable {
public DiscordBanCommand() {
setName("discordban");
setSyntax("discordban [id:<id>] ({add}/remove) [user:<user>] [group:<group>] (reason:<reason>) (deletion_timeframe:<time>/{0s})");
setRequiredArguments(3, 6);
isProcedural = false;
autoCompile();
}
// <--[command]
// @Name discordban
// @Syntax discordban [id:<id>] ({add}/remove) [user:<user>] [group:<group>] (reason:<reason>) (deletion_timeframe:<time>/{0s})
// @Required 3
// @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") DiscordBotTag bot,
@ArgName("instruction") DiscordBanInstruction instruction,
@ArgPrefixed @ArgName("user") DiscordUserTag user,
@ArgPrefixed @ArgName("group") DiscordGroupTag rawGroup,
@ArgPrefixed @ArgDefaultNull @ArgName("reason") String reason,
@ArgPrefixed @ArgDefaultText("0s") @ArgName("deletion_timeframe") DurationTag deletionTimeframe) {
if (rawGroup != null && rawGroup.bot == null) {
rawGroup = new DiscordGroupTag(bot.bot, rawGroup.guild_id);
}
final DiscordGroupTag group = rawGroup;
Guild guild = group.getGuild();
UserSnowflake userObj = UserSnowflake.fromId(user.user_id);
Runnable runnable = () -> {
try {
switch (instruction) {
case ADD: {
AuditableRestAction<Void> banAction = guild.ban(userObj, deletionTimeframe.getSecondsAsInt(), TimeUnit.SECONDS);
if (reason != null) {
banAction.reason(reason);
}
banAction.queue();
break;
}
case REMOVE: {
guild.unban(userObj).queue();
break;
}
}
}
catch (Exception ex) {
Debug.echoError(scriptEntry, ex);
}
};
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
runnable.run();
scriptEntry.setFinished(true);
});
}
}