javascript - Discord.js - 承诺被“覆盖”的问题?
问题描述
好的,所以我几乎已经完成了我用 JS 构建的 Discord Bot。我遇到了在旧承诺完成之前解决新承诺的问题,导致不正确的信息被发送到错误的服务器。
例如,我的“!armory”命令-它的作用是:(需要用户接受的参数中的项目参数:xanax,吗啡,血袋,重新填充)发出请求-promise.post->如果请求成功->发送“正在加载...”消息 -> 然后发出单独的 request.post -> 成功编辑“正在加载...”消息,其中包含来自用户 args 的结果。
请求的 HTTP 包含来自我的 Torn.com 派系的用户列表,以及他们从我们的派系军械库中使用过的物品。因此,在编辑中发送的消息包含所有用户以及他们使用过的参数中给出的项目的数量。
我遇到的问题是,当有人执行命令两次时,无论是否在同一台服务器上;最后一个命令使用的结果提供给每个人。
示例:服务器 1 中的用户使用命令获取 xanax 使用情况,如“!armory xanax”......虽然承诺仍在解决,但服务器 2 中的用户使用命令获取吗啡使用情况,如“!armory morphine”...... .
一旦第二个承诺解决了,服务器 2 中使用的命令的吗啡结果将在编辑消息中发送到两个服务器。
我希望每台服务器都能得到各自的结果。
编辑以添加代码
let configJson = fs.readFileSync(`./config.json`);
let configObj = JSON.parse(configJson);
const server_api_get = 'https://torn.market:8443/getarmoury';
const server_api_update = 'https://torn.market:8443/updatearmoury';
updateData = {
method: 'POST',
url: server_api_update,
form: {
API_KEY: configObj.guilds[message.guild.id].key,
end_date: endDate, // - End Date Dropdown on Faction page(Date furthest from today)
start_date: startDate, // - Start Date Dropdown on Faction page(Date closest to today)
},
};
//
getData = {
method: 'POST',
url: server_api_get,
form: {
API_KEY: configObj.guilds[message.guild.id].key,
end_date: endDate, // - End Date Dropdown on Faction page(Date furthest from today)
start_date: startDate, // - Start Date Dropdown on Faction page(Date closest to today)
},
};
//
rp(updateData)
.then(function (body) {
// POST succeeded...
updateJson = JSON.parse(body)
console.log(body);
if (updateJson.message === "Update Successful") {
let responseTitle = '';
console.log(` - New request -\nDate/Time: ${Date()}\n` + updateJson.message)
embed.setColor('BLUE')
.setTitle('`Loading Faction Armory use for ' + type + '......`')
message.channel.send(embed).then(msg => {
rp(getData)
.then(function (body) {
responseMsg = '';
embed.setColor(`GREEN`)
.setTitle(responseTitle)
.setDescription("```coffeescript\n" + responseMsg + "\n```")
msg.edit(embed)
})
.catch(function (err) {
// POST failed...
errJSON = JSON.stringify(err)
errObj = JSON.parse(errJSON)
console.log(`${errObj.name}\n${errObj.message}`);
});
})
} else {
console.log(`Code: ${updateJson.error}\nError: ${updateJson.message}\nWrong API_KEY or no Faction API Access.`);
}
})
解决方案
呃,我是个白痴……
我只需要以与 responseTitle 和 responseMsg 相同的方式定义一个空白“类型”变量......
let type = '';
'type' 由命令用户给出的参数组成,如下所示:
let responseTitle = '';
if (newArgs.includes("xa")) {
type = "Xanax";
responseTitle = "Xanax used from Armory\n__*" + endDate + "*__ to __*" + startDate + "*__\n"
} else if (newArgs.includes("morph")) {
type = "Morphine";
responseTitle = "Morphine used from Armory\n__*" + endDate + "*__ to __*" + startDate + "*__\n"
} else if (newArgs.includes("bb") || newArgs.includes("blood bag") || newArgs.includes("blood") || newArgs.includes("bag")) {
type = "BloodBag";
responseTitle = "Blood Bags used from Armory\n__*" + endDate + "*__ to __*" + startDate + "*__\n"
} else if (newArgs.includes("re")) {
type = "Refill";
responseTitle = "Blood Bags refilled from Armory\n__*" + endDate + "*__ to __*" + startDate + "*__\n"
} else if (newArgs.includes("sf")) {
type = "SFAK";
responseTitle = "Small First Aid Kits used from Armory\n__*" + endDate + "*__ to __*" + startDate + "*__\n"
} else if (newArgs == "fak") {
type = "FAK";
responseTitle = "First Aid Kits used from Armory\n__*" + endDate + "*__ to __*" + startDate + "*__\n"
}