node.js - “TypeError:无法读取未定义的属性‘我’”
问题描述
我正在为我的 discord.js v12 机器人创建两个命令,但是当第一个命令用于第一个命令的代码时,它会引发错误“TypeError: Cannot read property 'me' of undefined”:
function generateID() {
let ticketGen = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split("");
let ticketStr = "";
for(let i = 0; i < 3; i++) {
ticketStr += ticketGen[Math.floor(Math.random() * ticketGen.length)];
}
return ticketStr;
}
const fsn = require("fs-nextra");
const colors = require("colors");
const Discord = require('discord.js');
module.exports = {
name: 'order',
description: 'Ordering something',
args: 'true',
usage: '<order desription>',
aliases: ['o'],
execute(message) {
const client = message.client; //<- Line added right here
let order = message.content.substring(7);
let channel = message.guild.channels.cache.get("746423099871985755");
let customer = message.author.id
fsn.readJSON("./blacklist.json").then((blacklistDB) => {
let entry = blacklistDB[message.guild.id];
// Checks is server is blacklisted or not.
if(entry === undefined) {
// Gets ticket ID.
const ticketID = generateID();
// Sends ticket information to tickets channel.
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#FFFF33')
.setTitle(message.member.user.tag)
.setAuthor(`Order ID: ${ticketID}`)
.setDescription(order)
.setThumbnail(message.author.avatarURL())
.setTimestamp()
.setFooter(`From ${message.guild.name} (${message.guild.id})`);
channel.send(exampleEmbed);
// Sets ticket info.
fsn.readJSON("./orders.json").then(orderDB => {
// Set JSON information.
if (!orderDB[ticketID]) orderDB[ticketID] = {
"orderID": ticketID,
"userID": message.author.id,
"guildID": message.guild.id,
"channelID": message.channel.id,
"order": order,
"status": "Unclaimed",
"ticketChannelMessageID": "not set"
};
// Write JSON information.
fsn.writeJSON("./orders.json", orderDB, {
replacer: null,
spaces: 4
}).then(() => {
// Sends an embed to the customer.
message.channel.send("Your order has been sent and it will be delivered soon.")
// Logs in console.
console.log(colors.green(`${message.author.username} ordered "${order}" in ${message.guild.name} (${message.guild.id}) in ${message.channel.name} (${message.channel.id}).`));
}).catch((err) => {
if(err) {
message.reply(`There was a database error! Show the following message to a developer: \`\`\`${err}\`\`\``);
console.log(colors.red(`Error in order ${ticketID}: ${err}`));
}
});
});
}else {
message.reply("This server is currently blacklisted.");
}
});
}
};
在这部分,我认为它如何保存信息有问题?
// Sets ticket info.
fsn.readJSON("./orders.json").then(orderDB => {
// Set JSON information.
if (!orderDB[ticketID]) orderDB[ticketID] = {
"orderID": ticketID,
"userID": message.author.id,
"guildID": message.guild.id,
"channelID": message.channel.id,
"order": order,
"status": "Unclaimed",
"ticketChannelMessageID": "not set"
};
// Write JSON information.
fsn.writeJSON("./orders.json", orderDB, {
replacer: null,
spaces: 4
}).then(() => {
// Sends an embed to the customer.
message.channel.send("Your order has been sent and it will be delivered soon.")
使用返回错误“TypeError:无法读取未定义的属性'me'”的第二个命令时发生错误,我认为它与第一个命令第二个命令代码有关:
if (order.status === "Ready") {
// Delete ticket from database.
delete orderDB[ticketID];
// Writes data to JSON.
fsn.writeJSON("./orders.json", orderDB, {
replacer: null,
spaces: 4
}).then(() => {
// If bot has create instant invite permission.
if(client.guilds.cache.get(order.guildID).me.hasPermission("CREATE_INSTANT_INVITE")) {
// Send message to cook.
client.channels.cache.get(order.channelID).send(`${client.users.cache.get(order.userID)}, Here is your taco that you ordered. Remember you can do \`.tip [Amount]\` to give us virtual tips, and \`.feedback [Feedback]\` to give us feedback on how we did. ${order.imageURL}`);
message.reply(`Order has been sent to ${client.guilds.cache.get(order.guildID).name}`);
}else {
// Bot delivers it's self.
client.channels.cache.get(order.channelID).send(`${client.users.cache.get(order.userID)}, Here is your taco that you ordered. Remember you can do \`.tip [Amount]\` to give us virtual tips, and \`.feedback [Feedback]\` to give us feedback on how we did. ${order.imageURL}`);
我只包括了我认为有错误的部分,因为order.guildid
未定义但我该如何解决?
解决方案
正如错误消息所解释的,没有me
未定义的属性,因此公会未定义/未找到。检查您使用的公会 ID 是正确的公会 ID,它是一个字符串而不是数字。可能值得console.log(client.guilds.cache.get(order.guildID).name));
在其中放置一个以帮助确定问题。
推荐阅读
- python - 如何在数据框上使用火山图并打印
- r - 保存到.png时如何不丢失上标格式,R中的KableExtra
- python - Python,我如何将同一列 numpy 中的两个值相加并在该特定列 numpy 上迭代添加函数
- c - 为什么“比数据参数更多的 '%' 转换”错误消息输出一个大的负整数作为第一个数字?为什么是 0 的后记?
- java - 在Android 11(API30)下保存文件的正确方法(resp.文件夹)是什么?
- python - Python - 条件中的无效语法
- powershell - 在 Powershell 中创建可点击的文件夹链接
- python - 查找与数据框中所有可能的点组合的线交点
- sql - 修复表上的“无法将 NULL 插入”SQL 错误
- docker - 如何在 docker 容器中安装本地包?