Skip to content

Commit

Permalink
type argument (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
acikek committed Feb 5, 2022
1 parent 57878fa commit aae015d
Showing 1 changed file with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Category;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.GuildChannel;
import net.dv8tion.jda.api.requests.restaction.ChannelAction;
import org.bukkit.Bukkit;

Expand All @@ -21,14 +22,14 @@ public class DiscordCreateChannelCommand extends AbstractDiscordCommand implemen

public DiscordCreateChannelCommand() {
setName("discordcreatechannel");
setSyntax("discordcreatechannel [id:<id>] [group:<group>] [name:<name>] (description:<description>) (category:<category_id>) (position:<#>) (roles:<list>) (users:<list>)");
setRequiredArguments(3, 8);
setSyntax("discordcreatechannel [id:<id>] [group:<group>] [name:<name>] (description:<description>) (type:<type>) (category:<category_id>) (position:<#>) (roles:<list>) (users:<list>)");
setRequiredArguments(3, 9);
}
// <--[command]
// @Name discordcreatechannel
// @Syntax discordcreatechannel [id:<id>] [group:<group>] [name:<name>] (description:<description>) (category:<category_id>) (position:<#>) (roles:<list>) (users:<list>)
// @Syntax discordcreatechannel [id:<id>] [group:<group>] [name:<name>] (description:<description>) (type:<type>) (category:<category_id>) (position:<#>) (roles:<list>) (users:<list>)
// @Required 3
// @Maximum 8
// @Maximum 9
// @Short Creates text channels on Discord.
// @Plugin dDiscordBot
// @Group external
Expand All @@ -38,7 +39,11 @@ public DiscordCreateChannelCommand() {
//
// 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 description (aka "topic") with the "description" argument. (type:<type>)
//
// 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.
Expand All @@ -59,8 +64,8 @@ public DiscordCreateChannelCommand() {
// - ~discordcreatechannel id:mybot group:1234 name:my-channel category:5678
//
// @Usage
// Use to create a channel and log its name upon creation.
// - ~discordcreatechannel id:mybot group:1234 name:very-important-channel save:stuff
// 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>'"
//
// -->
Expand All @@ -76,6 +81,7 @@ public void execute(ScriptEntry scriptEntry) {
DiscordGroupTag group = scriptEntry.requiredArgForPrefix("group", DiscordGroupTag.class);
ElementTag name = scriptEntry.requiredArgForPrefixAsElement("name");
ElementTag description = scriptEntry.argForPrefixAsElement("description", null);
ElementTag type = scriptEntry.argForPrefixAsElement("type", null);
ElementTag category = scriptEntry.argForPrefixAsElement("category", null);
ElementTag position = scriptEntry.argForPrefixAsElement("position", null);
List<DiscordRoleTag> roles = scriptEntry.argForPrefixList("roles", DiscordRoleTag.class, true);
Expand All @@ -88,11 +94,39 @@ public void execute(ScriptEntry scriptEntry) {
}
Runnable runner = () -> {
try {
ChannelAction<TextChannel> action = group.getGuild().createTextChannel(name.asString());
ChannelAction<? extends GuildChannel> action;
ChannelType typeEnum = type != null ? ChannelType.valueOf(type.asString().toUpperCase()) : ChannelType.TEXT;
switch (typeEnum) {
case NEWS:
case TEXT: {
action = group.getGuild().createTextChannel(name.asString()).setType(typeEnum);
break;
}
case CATEGORY: {
action = group.getGuild().createCategory(name.asString());
break;
}
case VOICE: {
action = group.getGuild().createVoiceChannel(name.asString());
break;
}
default: {
handleError(scriptEntry, "Invalid channel type!");
return;
}
}
if (description != null) {
if (typeEnum != ChannelType.TEXT && typeEnum != ChannelType.NEWS) {
handleError(scriptEntry, "Only text and news channels can have descriptions!");
return;
}
action = action.setTopic(description.asString());
}
if (category != null) {
if (typeEnum == ChannelType.CATEGORY) {
handleError(scriptEntry, "A category cannot have a parent category!");
return;
}
Category resultCategory = group.getGuild().getCategoryById(category.asString());
if (resultCategory == null) {
handleError(scriptEntry, "Invalid category!");
Expand All @@ -117,7 +151,7 @@ public void execute(ScriptEntry scriptEntry) {
action = action.addMemberPermissionOverride(user.user_id, permissions, null);
}
}
TextChannel resultChannel = action.complete();
GuildChannel resultChannel = action.complete();
scriptEntry.addObject("channel", new DiscordChannelTag(bot.bot, resultChannel));
}
catch (Exception ex) {
Expand Down

0 comments on commit aae015d

Please sign in to comment.