You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Discord.py通过命令启用/禁用社区服务器状态

如何用命令启用/禁用Discord社区服务器状态

Discord官方没有提供原生聊天指令直接切换社区服务器状态,你需要通过开发机器人来实现这个功能。下面是基于Discord.js v14的实现方案:

前置准备

  • 机器人账号需要拥有ManageGuild(管理服务器)权限
  • 已安装Discord.js依赖:执行npm install discord.js完成安装

启用社区状态的机器人代码示例

你可以创建一个斜杠命令(如/enable-community)来触发启用操作:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.on('ready', () => {
  console.log(`已登录:${client.user.tag}`);
});

client.on('interactionCreate', async interaction => {
  if (!interaction.isChatInputCommand()) return;

  if (interaction.commandName === 'enable-community') {
    // 检查用户权限
    if (!interaction.member.permissions.has('ManageGuild')) {
      return interaction.reply('你没有管理服务器的权限,无法执行此操作');
    }
    try {
      // 添加COMMUNITY特性到服务器
      await interaction.guild.edit({
        features: [...interaction.guild.features, 'COMMUNITY']
      });
      await interaction.reply('社区服务器状态已成功启用');
    } catch (err) {
      await interaction.reply(`启用失败:${err.message}`);
    }
  }
});

// 替换为你的机器人Token
client.login('YOUR_BOT_TOKEN');

禁用社区状态的机器人代码示例

同理,创建/disable-community斜杠命令来禁用:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.on('ready', () => {
  console.log(`已登录:${client.user.tag}`);
});

client.on('interactionCreate', async interaction => {
  if (!interaction.isChatInputCommand()) return;

  if (interaction.commandName === 'disable-community') {
    if (!interaction.member.permissions.has('ManageGuild')) {
      return interaction.reply('你没有管理服务器的权限,无法执行此操作');
    }
    try {
      // 从服务器特性中移除COMMUNITY
      await interaction.guild.edit({
        features: interaction.guild.features.filter(f => f !== 'COMMUNITY')
      });
      await interaction.reply('社区服务器状态已成功禁用');
    } catch (err) {
      await interaction.reply(`禁用失败:${err.message}`);
    }
  }
});

client.login('YOUR_BOT_TOKEN');

重要提示

  • 启用社区状态前,服务器必须满足Discord的社区要求(比如设置规则频道、公告频道等),否则会触发错误
  • 禁用社区状态后,服务器会丢失所有社区专属功能(如发现页展示、社区 moderation工具等)

内容的提问来源于stack exchange,提问作者Jordan Flames

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 20:00:04