首页 > 解决方案 > 如何为整个文件制作 const?

问题描述

有什么方法可以为整个文件制作 const 吗?比如获取categoryId const,这样就可以用于message.channel.send(categoryId)

module.exports = {
    name: 'new',
    description: "Vytvoří novou sekci pro zákazníka",
    async execute(message, args, Discord, client, chalk){
        message.guild.channels.create(` | ${args[0]}`, { type: 'category' })
        .then(category => {const categoryId = (category.id)})
    
        message.channel.send(categoryId)
    }
}

标签: javascriptdiscorddiscord.jsbots

解决方案


是的,只需在文件顶部声明即可。但是,在您的情况下,let将比 更合适const,因为您必须在创建时为其分配一个值const,因此您的解决方案将如下所示:

let categoryID;
module.exports = {
    name: 'new',
    description: "Vytvoří novou sekci pro zákazníka",
    async execute(message, args, Discord, client, chalk){
        message.guild.channels.create(` | ${args[0]}`, { type: 'category' })
        .then(category => categoryID = category.id)

        message.channel.send(categoryID)
    }
}

我建议您查看 MDN 站点的letconst


推荐阅读