3 回答

TA贡献1936条经验 获得超7个赞
问题是您调用后的代码then不会等待then连接到的承诺解决。为了让等待,你必须把它所有在该then处理后newChannelRole = x;线。
但是由于您使用的是async函数,所以不要使用.then,使用await. 见***评论:
async run(msg,args){
//get our guilds Information
let guildID = msg.guild.id;
let locationDataKey = guildID+"_mapData";
//Our Guild Settings
let settingMapKey = guildID+"_setting";
let settings = settingsMap.get(settingMapKey);
//load up our location Data Array
let locationDataArray = data.get(locationDataKey);
//make the new channel
let formatedNameDashes = args.name.replace(/ /g,"-");
let formatedNameSpace = args.name.replace(/-/g," ")
//make a role for the channel
let newChannelRole = await msg.guild.createRole({ // ***
name:formatedNameSpace
});
//Make the Channel and set the new permissions
//Everyone has none, NPCs and the unique channel role can see it/type/read history
const channel = await msg.guild.createChannel(formatedNameDashes,{ // ***
type:'text',
permissionOverwrites:[
{
id:msg.guild.defaultRole,
deny: ['VIEW_CHANNEL','SEND_MESSAGES','READ_MESSAGE_HISTORY']
},
{
id:settings.npcRoleID,
allow:['VIEW_CHANNEL','SEND_MESSAGES','READ_MESSAGE_HISTORY']
},
{
id:newChannelRole.id,
allow:['VIEW_CHANNEL','SEND_MESSAGES','READ_MESSAGE_HISTORY']
}
]
});
//move it into the category
let rpCategory = msg.guild.channels.get(settings.rpCategoryID)
channel.setParent(rpCategory);
//push the information into the locationDataArray
mapDataArray.push({
name:formatedNameSpace,
channelName:channel.name,
connections:[],
channelID:channel.id
});
//save the locationDataArray
data.set(locationDataKey,locationDataKey);
}
请注意,任何调用都run需要处理run返回承诺的事实(因为所有 async函数都返回承诺)。特别重要的是,无论调用它处理承诺拒绝(或将承诺传递给将)。

TA贡献1796条经验 获得超4个赞
改变这一点:
msg.guild.createRole({
name:formatedNameSpace
}).then( x => {
newChannelRole = x;
});
对此:
newChannelRole = await msg.guild.createRole({
name:formatedNameSpace
})

TA贡献1785条经验 获得超8个赞
使用异步函数时,请尝试使用 await 实现
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function SecondCall()
{
console.log('I got called in order');
}
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
async function asyncCall() {
console.log('calling');
var result = await resolveAfter2Seconds();
console.log(result);
await sleep(1000);
SecondCall();
// expected output: 'resolved'
}
asyncCall();
添加回答
举报