Search in sources :

Example 1 with Category

use of net.dv8tion.jda.api.entities.Category in project dDiscordBot by DenizenScript.

the class DiscordCreateChannelCommand method execute.

// <--[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>'"
// 
// -->
@Override
public void execute(ScriptEntry scriptEntry) {
    DiscordBotTag bot = scriptEntry.requiredArgForPrefix("id", DiscordBotTag.class);
    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);
    List<DiscordUserTag> users = scriptEntry.argForPrefixList("users", DiscordUserTag.class, true);
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), bot, group, name, description, category);
    }
    if (group != null && group.bot == null) {
        group.bot = bot.bot;
    }
    Runnable runner = () -> {
        try {
            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!");
                    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) {
            handleError(scriptEntry, ex);
        }
    };
    Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
        runner.run();
        scriptEntry.setFinished(true);
    });
}
Also used : Category(net.dv8tion.jda.api.entities.Category) Set(java.util.Set) GuildChannel(net.dv8tion.jda.api.entities.GuildChannel) ChannelAction(net.dv8tion.jda.api.requests.restaction.ChannelAction) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ChannelType(net.dv8tion.jda.api.entities.ChannelType)

Example 2 with Category

use of net.dv8tion.jda.api.entities.Category in project TJ-Bot by Together-Java.

the class ChannelMonitor method statusMessage.

/**
 * Creates the status message (specific to the guild specified) that shows which channels are
 * busy/free.
 * <p>
 * It first updates the channel names, order and grouping(categories) according to
 * {@link net.dv8tion.jda.api.JDA} for the monitored channels. So that the output is always
 * consistent with remote changes.
 *
 * @param guild the guild the message is intended for.
 * @return a string representing the busy/free status of channels in this guild. The String
 *         includes emojis and other discord specific markup. Attempting to display this
 *         somewhere other than discord will lead to unexpected results.
 */
public String statusMessage(@NotNull final Guild guild) {
    List<ChannelStatus> statusFor = guildMonitoredChannelsList(guild);
    // update name so that current channel name is used
    statusFor.forEach(channelStatus -> channelStatus.updateChannelName(guild));
    // dynamically separate channels by channel categories
    StringJoiner content = new StringJoiner("\n");
    String categoryName = "";
    for (ChannelStatus status : statusFor) {
        TextChannel channel = guild.getTextChannelById(status.getChannelId());
        if (channel == null) {
            // pointless ... added to remove warnings
            continue;
        }
        Category category = channel.getParent();
        if (category != null && !category.getName().equals(categoryName)) {
            categoryName = category.getName();
            // append the category name on a new line with markup for underlining
            // TODO possible bug when not all channels are part of categories, may mistakenly
            // include uncategorized channels inside previous category. will an uncategorized
            // channel return an empty string or null? javadocs don't say.
            content.add("\n__" + categoryName + "__");
        }
        content.add(status.toDiscordContentRaw());
    }
    return content.toString();
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) Category(net.dv8tion.jda.api.entities.Category)

Example 3 with Category

use of net.dv8tion.jda.api.entities.Category in project HBot-Release by hhhzzzsss.

the class DiscordManager method loadLogChannels.

public void loadLogChannels() {
    ArrayList<TextChannel> channels = new ArrayList<>();
    for (Guild guild : jda.getGuilds()) {
        for (Category category : guild.getCategoriesByName(hbot.getCategoryName(), true)) {
            System.out.println(hbot.getCategoryName());
            for (TextChannel channel : category.getTextChannels()) {
                if (channel.getName().equalsIgnoreCase("hbot-logs")) {
                    channels.add(channel);
                }
            }
        }
    }
    logChannels = channels;
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) Category(net.dv8tion.jda.api.entities.Category) ArrayList(java.util.ArrayList) Guild(net.dv8tion.jda.api.entities.Guild)

Example 4 with Category

use of net.dv8tion.jda.api.entities.Category in project LemBot by Leminee.

the class VoiceAutoCreation method onGuildVoiceJoin.

@Override
public void onGuildVoiceJoin(@NotNull final GuildVoiceJoinEvent event) {
    final boolean areThereToManyCreatedVoice = event.getGuild().getVoiceChannels().size() >= 10;
    if (areThereToManyCreatedVoice) {
        return;
    }
    final String voiceName = LEFT[random.nextInt(LEFT.length)] + "_" + RIGHT[random.nextInt(RIGHT.length)];
    final boolean isOnlyOneMemberInVoice = event.getChannelJoined().getMembers().size() == 1;
    if (isOnlyOneMemberInVoice) {
        Category voiceFunCategory = Config.getInstance().getCategoryConfig().getVoiceCategory();
        event.getGuild().createVoiceChannel(voiceName, voiceFunCategory).queue();
    }
}
Also used : Category(net.dv8tion.jda.api.entities.Category)

Example 5 with Category

use of net.dv8tion.jda.api.entities.Category in project LemBot by Leminee.

the class VoiceAutoCreation method onGuildVoiceMove.

@Override
public void onGuildVoiceMove(@NotNull GuildVoiceMoveEvent event) {
    final boolean areThereToManyCreatedVoice = event.getGuild().getVoiceChannels().size() >= 10;
    if (areThereToManyCreatedVoice) {
        return;
    }
    final String randomlyCombinedVoiceName = LEFT[random.nextInt(LEFT.length)] + "_" + RIGHT[random.nextInt(RIGHT.length)];
    final Category voiceFunCategory = Config.getInstance().getCategoryConfig().getVoiceCategory();
    final boolean isVoiceLeftEmpty = event.getChannelLeft().getMembers().size() == 0;
    final boolean wasVoiceJoinedEmpty = event.getChannelJoined().getMembers().size() == 1;
    if (isVoiceLeftEmpty && wasVoiceJoinedEmpty) {
        event.getChannelLeft().delete().queue();
        event.getGuild().createVoiceChannel(randomlyCombinedVoiceName, voiceFunCategory).queue();
        return;
    }
    final boolean wasVoiceJoinedNotEmpty = event.getChannelJoined().getMembers().size() >= 2;
    if (isVoiceLeftEmpty && wasVoiceJoinedNotEmpty) {
        event.getChannelLeft().delete().queue();
        return;
    }
    if (!isVoiceLeftEmpty && wasVoiceJoinedEmpty) {
        event.getGuild().createVoiceChannel(randomlyCombinedVoiceName, voiceFunCategory).queue();
    }
}
Also used : Category(net.dv8tion.jda.api.entities.Category)

Aggregations

Category (net.dv8tion.jda.api.entities.Category)6 TextChannel (net.dv8tion.jda.api.entities.TextChannel)3 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 TechDiscordBot (me.TechsCode.TechDiscordBot.TechDiscordBot)1 GitHubUtil (me.TechsCode.TechDiscordBot.github.GitHubUtil)1 GithubRelease (me.TechsCode.TechDiscordBot.github.GithubRelease)1 CommandModule (me.TechsCode.TechDiscordBot.module.CommandModule)1 DefinedQuery (me.TechsCode.TechDiscordBot.objects.DefinedQuery)1 Query (me.TechsCode.TechDiscordBot.objects.Query)1 TechEmbedBuilder (me.TechsCode.TechDiscordBot.util.TechEmbedBuilder)1 ChannelType (net.dv8tion.jda.api.entities.ChannelType)1 Guild (net.dv8tion.jda.api.entities.Guild)1 GuildChannel (net.dv8tion.jda.api.entities.GuildChannel)1 Member (net.dv8tion.jda.api.entities.Member)1 Role (net.dv8tion.jda.api.entities.Role)1 SlashCommandEvent (net.dv8tion.jda.api.events.interaction.SlashCommandEvent)1 Command (net.dv8tion.jda.api.interactions.commands.Command)1