【发布时间】:2021-07-04 08:47:55
【问题描述】:
我收到以下错误...
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(node:8232) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'prefix' of undefined
当使用来自我的机器人的命令时。我认为问题出在机器人的主文件上。 以下代码块是我认为错误的来源。
function startsWithPrefix(str, prefix) { if (str.slice(0, prefix.length).toLowerCase() == prefix.toLowerCase()) return true; }
// Execute when the bot sees a message
client.on('message', async msg => {
// Initialize guild settings on first message
if (msg.guild && !Settings[msg.guild.id]) {
const pool = new Pool();
try {
const res = await pool.query('SELECT prefix FROM settings WHERE guild_id = $1', [msg.guild.id]);
if (res.rows[0]) { Settings[msg.guild.id] = res.rows[0]; }
else {
Settings[msg.guild.id] = {
prefix: 'a!' // Default setting
};
}
} catch (err) { console.log(err.stack); }
finally { pool.end(); }
}
// Exit if msg doesn't start with prefix
if (msg.guild && !startsWithPrefix(msg.content, Settings[msg.guild.id].prefix)) { return; }
// Exit if msg was sent by a bot
if (msg.author.bot) { return; }
// Exit + Reply with a helpful message if msg was dm-ed to this bot
if (msg.channel.type == 'dm') { return await msg.reply(`I got nothin' for ya buddy :pensive:\nI think you meant to type that in the **Among Us LFG** server :+1:`); }
// Separate the command from the arguments
const words = msg.content.split(' ');
const cmdName = words[0].toLowerCase().slice(Settings[msg.guild.id].prefix.length);
const cmd = client.cmds.get(cmdName) || client.cmds.get(client.aliases.get(cmdName));
const args = words.slice(1);
// Run the command
if (cmd) cmd.run(client, msg, args);
});
// Start the bot
client.login(discordToken);
编辑:似乎 Settings[msg.guild.id] 未定义,但我将其定义为
let Settings = {};
exports.Settings = Settings;
在代码的开头不知道有什么问题
【问题讨论】:
-
似乎
Settings[msg.guild.id]未定义。您能否尝试console.log(Settings[msg.guild.id])并将输出发送给我们? -
哦,是的,当我记录它时它的未定义很奇怪,该机器人过去可以正常工作
标签: node.js discord.js