javascript - Discord.js - if(something === undefined) 问题
问题描述
我正在为我的不和谐机器人创建索赔订单,所有订单都存储在这样的 json 文件中,{ "mUT": { "orderID": "the order's id", "userID": "A number", "guildID": "another number", "channelID": "another remember", "order": "the order", "status": "Unclaimed", "ticketChannelMessageID": "another another number" },
但是当使用索赔命令时,它一直说订单不存在,即使我检查了 json 文件并且订单在那里我不太明白这个问题,命令根本不起作用
const colors = require("colors");
exports.id = "claim";
exports.onLoad = api => {
api.commands.add("claim", (msg) => {
let employeeRole = msg.guild.roles.get("745410836901789749");
if(msg.member.roles.has(employeeRole.id)) {
if(msg.channel.id == 746423099871985755) {
// The ticket ID.
let ticketID = msg.content.substring(7);
fsn.readJSON("./orders.json").then((orderDB) => {
const order = orderDB[ticketID];
// If the order doesn't exist.
if(order === undefined) {
msg.reply("Couldn't find that order.");
return;
}
if (order.status === "Unclaimed") {
// Edits the message in the tickets channel.
api.client.channels.get("746423099871985755").fetchMessages({
around: order.ticketChannelMessageID,
limit: 1
}).then(messages => {
const fetchedMsg = messages.first();
fetchedMsg.edit({embed: {
color: 0xFFFFFF,
title: api.client.users.get(order.userID).username,
fields: [{
name: "Order Description",
value: order,
}, {
name: "Order ID",
value: ticketID,
}, {
name: "Order Status",
value: "claimed",
}],
timestamp: new Date(),
footer: {
text: "Taco Bot"
}
}}).then((m) => {
m = m.id;
// Update Status
delete orderDB[ticketID];
orderDB[ticketID] = {
"orderID": order.orderID,
"userID": order.userID,
"guildID": order.guildID,
"channelID": order.channelID,
"order": order.order,
"status": "Claimed",
"ticketChannelMessageID": m,
"chef": msg.author.id
};
// Writes Data to JSON.
fsn.writeJSON("./orders.json", orderDB, {
replacer: null,
spaces: 4
}).then(() => {
// Sends a message to the cook.
msg.channel.send(`You've now claimed \`${ticketID}\`.`);
// Sends a message to the customer.
api.client.users.get(order.userID).send(`Your order has been claimed and will be delivered soon.`);
}).catch((err) => {
if (err) {
msg.reply(`There was an error while writing to the database! Show the following message to a developer: \`\`\`${err}\`\`\``);
}
});
console.log(colors.green(`${msg.author.username} claimed order ${ticketID}.`));
});
});
}else if(order.status === "Claimed") {
if(order.chef !== msg.author.id) {
msg.reply(`This order is already claimed by ${api.client.users.get(order.chef).tag}`);
}else {
msg.reply("You have already claimed this order. Run `.setimage [Order ID]` to set the order's image.");
}
}else if(order.status === "Waiting") {
msg.reply("This order is in the waiting process. Wait a little bit, then run `.deliver [Order ID]` to deliver.");
}else if(order.status === "Ready") {
msg.reply("This order is already ready to be delivered. Run `.deliver [Order ID]` to deliver.");
}
});
}else {
msg.reply("Please use this command in the orders channel.");
console.log(colors.red(`${msg.author.username} used the claim command in the wrong channel.`));
}
}else {
msg.reply("You do not have access to this command.");
console.log(colors.red(`${msg.author.username} did not have access to the claim command.`));
}
});
};
解决方案
推荐阅读
- random - 强化学习:序列中样本的 SGD 使用和独立性
- ruby-on-rails - 为什么我的 Rails `db/schema.rb` 函数的前缀是 `public`?
- c# - 请求被中止:无法创建 SSL/TLS 安全通道。在本地工作但不住在
- sql - 在同一个表的列中返回具有唯一值的行
- html - 无论网格布局如何,都放置一个按钮 bootstrap4
- .net - 服务器响应是:smtp23.relay.iad3b.emailsrvr.com
- javascript - 用冒号和分号分隔之间是否存在语义差异?
- catboost - 了解 CatBoost 有序提升
- java - 如何使用 Jenkins 参数在黄瓜框架中测试不同的环境?
- tableau-api - Tableau 中按创建日期划分的生命周期值