node.js - Discord.js 每个服务器前缀错误“TypeError:无法读取属性 'ID' of null”
问题描述
嗨,我正在使用 quick.db 为我的 discord.js v12 机器人制作每个服务器前缀的东西,一切都很顺利,但是当使用命令时它会抛出这个错误
C:\Users\Aoshey\Desktop\Taco Bot V2\tacobot.js:50
let prefix = db.get(`prefix_${message.guild.ID}`);
^
TypeError: Cannot read property 'ID' of null
我不太明白,因为我是编码新手,所以很抱歉,如果主机器人文件的代码有些愚蠢:
client.on('message', message => {
let prefix = db.get(`prefix_${message.guild.ID}`);
if(prefix === null) prefix = default_prefix;
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
if (command.guildOnly && message.channel.type === 'dm') {
return message.reply('I can\'t execute that command inside DMs!');
}
if (command.args && !args.length) {
let reply = `${message.author}, wrong usage`;
if (command.usage) {
reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command! Please tell Aro#1221 about this.');
}
});
我注意到该错误仅在仅用于 1 个命令时发生,同时其他命令运行良好(也是的,我在行会中使用该命令)导致我认为错误的命令代码?
const fsn = require("fs-nextra");
const colors = require("colors");
module.exports = {
name: 'deleteorder',
description: 'deleting/denying an order.',
args: 'true',
usage: '<order id> <reason>',
aliases: ['od', 'delorder', 'do'],
execute(message, args) {
if (message.member.roles.cache.find(r => r.id === '745410836901789749')) {
let ticketID = args[0];
let reason = args[1];
fsn.readJSON("./orders.json").then((orderDB) => {
const order = orderDB[ticketID];
if(order === undefined) {
message.reply(`Couldn't find order \`${args[0]}\` Try again.`);
return;
}
if(reason === "") {
reason = "None Provided";
}
delete orderDB[ticketID];
fsn.writeJSON("./orders.json", orderDB, {
replacer: null,
spaces: 4
});
message.reply(`You deleted order \`${args[0]}\` with reason \`${args[1]}\``)
// Sends a message to the customer.
let customer = message.guild.members.cache.find(m => m.id === order.userID)
//message.users.cache.get(order.userID).send(`Sorry, but your order was cancelled by **${message.author.username}** due to the following reason: \`${reason}\`.`);
customer.send(`Sorry, but your order was cancelled by **${message.author.username}** due to the following reason: \`${reason}\`.`)
// Logs in console.
console.log(colors.red(`${message.author.username} deleted order ${order.orderID}.`));
});
} else {
message.reply("You aren't an employee.");
console.log(colors.red(`${message.author.username} did not have access to the delorder command.`));
}
}
}
解决方案
Its just id
in lowercase letters. ID
in uppercase letters doesn't exist.
let prefix = db.get(`prefix_${message.guild.id}`);
推荐阅读
- django - 永久超级用户 Django Docker
- mysql - 为父表选择子表的值数
- delphi - 如何检查两个文本文件中的哪些行相等?
- sqlite - 在 sqflite 中查询没有列名的列值
- protocols - 我必须向 NNTP 提供哪些标头?
- c - c语言中的8051两个定时器和中断问题
- c++ - 在我关闭它然后再次运行它以从文件中读取之后,我的程序没有从文件中读取什么?
- java - 我可以在 Java 中的 if 语句中有 3 个条件语句吗?
- sharepoint - 使用 SSIS 从 SharePoint Online 列表中删除所有项目
- python - Pandas - 如何使用带有索引的字典对列的行进行排序