为了账号安全,请及时绑定邮箱和手机立即绑定

!创建类别 | 不创建类别 | 该怎么办?Discord.js

!创建类别 | 不创建类别 | 该怎么办?Discord.js

波斯汪 2023-06-09 14:56:08
client.on('ready', () => {    command(client, 'createcategory', (message) => {      const name = message.content.replace('!createcategory ', '')            if(message.guild.channels.cache.find(c => c.name == message.author.username && c.type == "category") === undefined){          message.guild.channels.create(message.author.username, {type: 'category', permissionOverwrites: [          {              id: message.guild.id,              deny: ['VIEW_CHANNEL'],          },          {              id: message.author.id,              allow: ['VIEW_CHANNEL'],          },      ]})      message.guild.channels.create('Text channel', {type: 'text', permissionOverwrites: [        {            id: message.guild.id,            deny: ['VIEW_CHANNEL'],        },        {            id: message.author.id,            allow: ['VIEW_CHANNEL'],        },    ]}).then(channel => {      let category = message.guild.channels.cache.find(c => c.name == message.author.username && c.type ==       "category");        if (!category) throw new Error("Category channel does not exist");      channel.setParent(category.id);    }).catch(console.error);      message.guild.channels.create('Voice channel', {type: 'voice', permissionOverwrites: [        {            id: message.guild.id,            deny: ['VIEW_CHANNEL'],        },        {            id: message.author.id,            allow: ['VIEW_CHANNEL'],        },    ]}).then(channel => {      let category = message.guild.channels.cache.find(c => c.name == message.author.username && c.type ==       "category");        if (!category) throw new Error("Category channel does not exist");      channel.setParent(category.id);    }).catch(console.error);       } else {message.send('Jau tu turi kanala, kurviuk tu')}  });});代码曾经有效,但不知何故我忘记了我用它做了什么并且没有编码 2 个月左右......功能应该是 - 当你编写它时应该!createcategory创建一个包含语音和文本通道的类别。该类别应以您的用户名命名。控制台没有错误,请帮忙,谢谢!
查看完整描述

1 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

我不确定是什么导致了您的问题,但请尝试在创建频道时设置文本和语音频道的父级:


// GuildChannelManager#create returns the channel you created

message.guild.channels.create(message.author.username, {

    type: 'category',

    permissionOverwrites: [

        {id: message.guild.id, deny: ['VIEW_CHANNEL']},

        {id: message.author.id, allow: ['VIEW_CHANNEL']},

    ]

}).then(parent => {

    // Create the text channel

    message.guild.channels.create('Text channel', {

        type: 'text',

        // under the parent category

        parent, // shorthand for parent: parent

        permissionOverwrites: [

            {id: message.guild.id, deny: ['VIEW_CHANNEL']},

            {id: message.author.id, allow: ['VIEW_CHANNEL']},

        ]

    }).catch(console.error)

    // Same with the voice channel

    message.guild.channels.create('Voice channel', {

        type: 'voice',

        parent,

        permissionOverwrites: [

            {id: message.guild.id, deny: ['VIEW_CHANNEL']},

            {id: message.author.id, allow: ['VIEW_CHANNEL']},

        ]

    }).catch(console.error)

})

你也可以使用 ES2017 的async/await:


// Must be an async function      vvvvv

command(client, 'createcategory', async (message) => {

   // ...

   const parent = await message.guild.channels.create(/* ... */)

    try {

        // Run the promises concurrently, like in your code

        await Promise.all([

            message.guild.channels.create('Text channel', {/* ... */})

            message.guild.channels.create('Voice channel', {/* ... */)

        ])

    } catch (error) {

        console.error(error)

    }

    // ...

})


查看完整回答
反对 回复 2023-06-09
  • 1 回答
  • 0 关注
  • 82 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信