首页 > 解决方案 > 保存新创建的角色和频道的 ID

问题描述

我正在尝试保存我刚刚创建的频道和角色的 ID,以便以后可以继续删除它们。我正在尝试将 ID 存储在变量中,以便以后可以从单独的文件公开访问它们。这就是我尝试这样做的方式:

let categoryID;
let channelID;
Promise.all([r1, c1, c2]).then(([role, cat, chan]) => {
    chan.setParent(cat);
    chan.overwritePermissions([
            {
                //everyone
                id: '762492106156539945',
                deny: ['SEND_MESSAGES', 'VIEW_CHANNEL']
            },
            {
                //verified
                id: '763048301552992346',
                allow: ['VIEW_CHANNEL']
            },
            {
                //new role
                id: role.id,
                allow: ['SEND_MESSAGES']
            }
        ])
    console.log(chan.id);
    console.log(cat.id);
    channelID = chan.id;
    categoryID = cat.id;
});
console.log(categoryID);
console.log(channelID);

console.log(chan.id)有效但console.log(channelID)返回未定义,即使我已将其设置为chan.id.

标签: javascriptdiscord.js

解决方案


我不完全确定,但你的问题可能是你在控制台日志之后声明了 channelID 变量,如果你明白我想说的话。试试这个代码:

let categoryID;
let channelID;
Promise.all([r1, c1, c2]).then(([role, cat, chan]) => {
    chan.setParent(cat);
    chan.overwritePermissions([
            {
                //everyone
                id: '762492106156539945',
                deny: ['SEND_MESSAGES', 'VIEW_CHANNEL']
            },
            {
                //verified
                id: '763048301552992346',
                allow: ['VIEW_CHANNEL']
            },
            {
                //new role
                id: role.id,
                allow: ['SEND_MESSAGES']
            }
        ])
    channelID = chan.id;
    categoryID = cat.id;
    console.log(chan.id);
    console.log(cat.id);
});
console.log(categoryID);
console.log(channelID);

推荐阅读