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

如何编写仅管理员或有管理频道权限者可使用的创建文本/语音频道指令

完整Discord创建频道指令实现代码

以下代码基于Discord.js v12版本编写,若你使用v13及以上版本需将权限校验方法替换为member.permissions.has()

// 假设你的指令前缀为 !,指令名为 createchannel
client.on('message', async message => {
  // 过滤机器人消息、非指令前缀开头的消息
  if (!message.content.startsWith('!') || message.author.bot) return;
  // 拆分指令和参数
  const args = message.content.slice(1).trim().split(/ +/);
  const command = args.shift().toLowerCase();
  const tag = message.author.toString();
  const member = message.member;

  if (command === 'createchannel') {
    // 校验用户权限
    if (
      member.hasPermission('ADMINISTRATOR') ||
      member.hasPermission('MANAGE_CHANNELS')
    ) {
      // 校验频道名称是否填写
      const name = args.join(' ');
      if (name) {
        // 执行频道创建
        await message.guild.channels.create(name, {
          type: 'text',
        });
        // 中文成功提示,需要英文可替换为注释内的内容
        message.channel.send(`${tag} 频道已创建成功`);
        // message.channel.send(`${tag} That channel has been created`)
      } else {
        // 中文参数缺失提示,需要英文可替换为注释内的内容
        message.channel.send(`${tag} 请指定频道名称`);
        // message.channel.send(`${tag} Please specify a name.`)
      }
    } else {
      // 中文无权限提示,需要英文可替换为注释内的内容
      message.channel.send(`${tag} 你没有权限使用该指令`);
      // message.channel.send(`${tag} You do not have permission to use this command.`)
    }
  }
})

注意事项

  • 若使用Discord.js v13+版本,权限校验代码需要改为member.permissions.has([Permissions.FLAGS.ADMINISTRATOR, Permissions.FLAGS.MANAGE_CHANNELS]),同时需要提前引入Permissions模块
  • 可自行调整频道创建时的额外参数,比如指定父分类、权限覆盖配置等
  • 原代码片段中的拼写错误beem已修正为been

内容的提问来源于stack exchange,提问作者Nayeem Patwary

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 02:33:01