【发布时间】:2021-08-05 15:57:32
【问题描述】:
我正在设置一个机器人来处理 MySQL 数据库。 它之前有效,现在在我添加 MySQL 代码后它不起作用:
(node:12312) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'query' of undefined
at C:\Users\Admin\Desktop\MuffinMod Data\MuffinMod - Recode\Bot\events\ready.js:4:20
at Map.forEach (<anonymous>)
at module.exports (C:\Users\Admin\Desktop\MuffinMod Data\MuffinMod - Recode\Bot\events\ready.js:3:25)
at Client.emit (events.js:315:20)
at WebSocketManager.triggerClientReady (C:\Users\Admin\Desktop\MuffinMod Data\MuffinMod - Recode\node_modules\discord.js\src\client\websocket\WebSocketManager.js:431:17)
at WebSocketManager.checkShardsReady (C:\Users\Admin\Desktop\MuffinMod Data\MuffinMod - Recode\node_modules\discord.js\src\client\websocket\WebSocketManager.js:415:10)
at WebSocketShard.<anonymous> (C:\Users\Admin\Desktop\MuffinMod Data\MuffinMod - Recode\node_modules\discord.js\src\client\websocket\WebSocketManager.js:197:14)
at WebSocketShard.emit (events.js:315:20)
at WebSocketShard.checkReady (C:\Users\Admin\Desktop\MuffinMod Data\MuffinMod - Recode\node_modules\discord.js\src\client\websocket\WebSocketShard.js:475:12)
at WebSocketShard.onPacket (C:\Users\Admin\Desktop\MuffinMod Data\MuffinMod - Recode\node_modules\discord.js\src\client\websocket\WebSocketShard.js:447:16)
(node:12312) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12312) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
index.js:
require('dotenv').config();
//Database
let connection;
(async () => {
connection = await require('./database/db.js');
})();
//Bot
const Discord = require("discord.js");
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'], restRequestTimeout: 50000 });
const guildSettings = new Map();
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
const DisTube = require('distube');
client.distube = new DisTube(client, { searchSongs: false, emitNewSongOnly: true });
['command', 'event'].forEach(handler =>{
require(`./Bot/handlers/${handler}`)(client, Discord, connection);
});
await client.login(process.env.TOKEN);
ready.js:
module.exports = async(Discord, client, connection) => {
console.log(`Bot online. (${client.user.tag})`);
client.guilds.cache.forEach(guild => {
connection.query(
`SELECT * FROM GuildConfigurable WHERE guildID = ${guild.id}`
).then(result => {
guildSettings.set(guild.id, result[0][0]);
}).catch(err => console.log(err));
});
}
db.js:
const sql = require('mysql2/promise');
module.exports = sql.createConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
})
.then(()=>console.log(`Connected to MySQL Database.`))
.catch(err=>console.error(err));
我认为这是在 db.js 文件可以返回连接之前执行的 ready.js 文件的错误,但不知道如何修复它。 谢谢
【问题讨论】:
-
提示与您的问题无关,使用
SELECT id FROM GuildConfigurable WHERE guildID = ..这样 MySQL 服务器不会浪费带宽和精力返回不需要的数据。 -
错误来自你的
ready.js,connection返回未定义。 -
@Elitezen 是的,我认为机器人正在登录并且在 db.js 可以返回数据库连接之前执行 ready.js
标签: javascript mysql node.js discord discord.js