/
DiscordCreateChannelCommand.java
163 lines (158 loc) · 7.77 KB
/
DiscordCreateChannelCommand.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.denizenscript.ddiscordbot.commands;
import com.denizenscript.ddiscordbot.DenizenDiscordBot;
import com.denizenscript.ddiscordbot.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
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.Permission;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.Category;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.requests.restaction.ChannelAction;
import org.bukkit.Bukkit;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class DiscordCreateChannelCommand extends AbstractCommand implements Holdable {
public DiscordCreateChannelCommand() {
setName("discordcreatechannel");
setSyntax("discordcreatechannel [id:<id>] [group:<group>] [name:<name>] (description:<description>) (type:<type>) (category:<category_id>) (position:<#>) (roles:<list>) (users:<list>)");
setRequiredArguments(3, 9);
isProcedural = false;
autoCompile();
}
// <--[command]
// @Name discordcreatechannel
// @Syntax discordcreatechannel [id:<id>] [group:<group>] [name:<name>] (description:<description>) (type:<type>) (category:<category_id>) (position:<#>) (roles:<list>) (users:<list>)
// @Required 3
// @Maximum 9
// @Short Creates text channels on Discord.
// @Plugin dDiscordBot
// @Group external
//
// @Description
// Creates text channels on Discord.
//
// This functionality requires the Manage Channels permission.
//
// You can optionally specify the channel description (aka "topic") with the "description" argument.
//
// You can optionally specify the channel type. Valid types are TEXT, NEWS, CATEGORY, and VOICE.
// Only text and news channels can have a description.
// Categories cannot have a parent category.
//
// You can optionally specify the channel's parent category with the "category" argument.
// By default, the channel will not be attached to any category.
//
// You can optionally specify the channel's position in the list as an integer with the "position" argument.
//
// You can optionally specify the roles or users that are able to view the channel.
// The "roles" argument takes a list of DiscordRoleTags, and the "users" argument takes a list of DiscordUserTags.
// Specifying either of these arguments will create a private channel (hidden for anyone not in the lists).
//
// The command should usually be ~waited for. See <@link language ~waitable>.
//
// @Tags
// <entry[saveName].channel> returns the DiscordChannelTag of a channel upon creation when the command is ~waited for.
//
// @Usage
// Use to create a channel in a category.
// - ~discordcreatechannel id:mybot group:1234 name:my-channel category:5678
//
// @Usage
// Use to create a voice channel and log its name upon creation.
// - ~discordcreatechannel id:mybot group:1234 name:very-important-channel type:voice save:stuff
// - debug log "Created channel '<entry[stuff].channel.name>'"
//
// -->
public static void autoExecute(ScriptEntry scriptEntry,
@ArgPrefixed @ArgName("id") DiscordBotTag bot,
@ArgPrefixed @ArgName("group") DiscordGroupTag rawGroup,
@ArgPrefixed @ArgName("name") String name,
@ArgPrefixed @ArgDefaultNull @ArgName("description") String description,
@ArgPrefixed @ArgDefaultText("text") @ArgName("type") ChannelType type,
@ArgPrefixed @ArgDefaultNull @ArgName("category") String category,
@ArgPrefixed @ArgDefaultNull @ArgName("position") ElementTag position,
@ArgPrefixed @ArgDefaultNull @ArgName("roles") @ArgSubType(DiscordRoleTag.class) List<DiscordRoleTag> roles,
@ArgPrefixed @ArgDefaultNull @ArgName("users") @ArgSubType(DiscordUserTag.class) List<DiscordUserTag> users) {
if (rawGroup.bot == null) {
rawGroup = new DiscordGroupTag(bot.bot, rawGroup.guild_id);
}
final DiscordGroupTag group = rawGroup;
Runnable runner = () -> {
try {
ChannelAction<? extends GuildChannel> action;
switch (type) {
case NEWS: {
action = group.getGuild().createNewsChannel(name);
break;
}
case TEXT: {
action = group.getGuild().createTextChannel(name);
break;
}
case CATEGORY: {
action = group.getGuild().createCategory(name);
break;
}
case VOICE: {
action = group.getGuild().createVoiceChannel(name);
break;
}
default: {
Debug.echoError(scriptEntry, "Invalid channel type!");
return;
}
}
if (description != null) {
if (type != ChannelType.TEXT && type != ChannelType.NEWS) {
Debug.echoError(scriptEntry, "Only text and news channels can have descriptions!");
return;
}
action = action.setTopic(description);
}
if (category != null) {
if (type == ChannelType.CATEGORY) {
Debug.echoError(scriptEntry, "A category cannot have a parent category!");
return;
}
Category resultCategory = group.getGuild().getCategoryById(category);
if (resultCategory == null) {
Debug.echoError(scriptEntry, "Invalid category!");
return;
}
action = action.setParent(resultCategory);
}
if (position != null) {
action = action.setPosition(position.asInt());
}
Set<Permission> permissions = Collections.singleton(Permission.VIEW_CHANNEL);
if (roles != null || users != null) {
action = action.addRolePermissionOverride(group.guild_id, null, permissions);
}
if (roles != null) {
for (DiscordRoleTag role : roles) {
action = action.addRolePermissionOverride(role.role_id, permissions, null);
}
}
if (users != null) {
for (DiscordUserTag user : users) {
action = action.addMemberPermissionOverride(user.user_id, permissions, null);
}
}
GuildChannel resultChannel = action.complete();
scriptEntry.addObject("channel", new DiscordChannelTag(bot.bot, resultChannel));
}
catch (Exception ex) {
Debug.echoError(scriptEntry, ex);
}
};
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
runner.run();
scriptEntry.setFinished(true);
});
}
}