下面是使用 Discord4J 3.2.4 版本的示例代码,将频道改为禁止发言:

import discord4j.core.DiscordClient;
import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.entity.Channel;
import discord4j.core.object.entity.Guild;
import discord4j.core.object.entity.Member;
import discord4j.core.object.entity.PermissionOverwrite;
import discord4j.core.object.entity.Role;
import discord4j.core.object.util.Permission;
import discord4j.core.spec.ChannelEditSpec;
import reactor.core.publisher.Mono;

public class Main {
    public static void main(String[] args) {
        // Replace with your own bot token
        DiscordClient client = DiscordClient.create('your-bot-token');

        client.getEventDispatcher().on(MessageCreateEvent.class)
                .flatMap(event -> {
                    // Get the channel where the command was sent
                    Mono<Channel> channelMono = event.getMessage().getChannel().blockOptional();

                    // Get the guild where the channel is located
                    Mono<Guild> guildMono = channelMono
                            .flatMap(channel -> channel.getGuild().blockOptional());

                    // Get the member who sent the command
                    Mono<Member> memberMono = event.getMember().blockOptional();

                    // Check if the member has permission to manage channels
                    Mono<Boolean> hasPermission = memberMono.flatMap(member -> guildMono
                            .flatMap(guild -> member.getBasePermissions()
                                    .map(permissions -> permissions.contains(Permission.MANAGE_CHANNELS))));

                    // If the member has permission, edit the channel permissions to disallow sending messages
                    return hasPermission.flatMap(has -> {
                        if (has) {
                            return channelMono.flatMap(channel ->
                                    // Create a new permission overwrite for the @everyone role
                                    guildMono.flatMap(guild ->
                                            guild.getEveryoneRole().flatMap(role ->
                                                    channel.createPermissionOverwrite(role.getId(),
                                                            PermissionOverwrite.forbid(Permission.SEND_MESSAGES))
                                                            .flatMap(overwrite ->
                                                                    // Edit the channel with the new permission overwrite
                                                                    channel.edit(ChannelEditSpec.builder()
                                                                            .permissionOverwrite(overwrite)
                                                                            .build())))));
                        } else {
                            return Mono.empty(); // If the member doesn't have permission, do nothing
                        }
                    });
                })
                .subscribe();

        client.login().block();
    }
}

该代码监听消息创建事件,并在接收到命令时,将频道的 @everyone 角色的发送消息权限设置为禁止,并更新频道的权限覆盖。在实际使用时,您需要将 your-bot-token 替换为您的机器人的令牌。

Discord4J 3.2.4: 使用 editChannelPermissions 禁止频道发言

原文地址: https://www.cveoy.top/t/topic/nf61 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录