[英]MySQL in discord.js
我正在设置一个机器人来使用 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);
准备好了.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 文件的错误,但不知道如何修复它。 谢谢
由于您的async
方法仅在 promise 解决后才设置connection
变量,因此在您使用它来设置事件处理程序时它仍然是未定义的。
您可以将connection
保持为每次需要时等待的 promise,或者将创建移动到与消费者相同的异步 function 中(如下所示)
require('dotenv').config();
//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 });
(async () => {
const connection = await require('./database/db.js');
['command', 'event'].forEach(handler =>{
require(`./Bot/handlers/${handler}`)(client, Discord, connection);
});
await client.login(process.env.TOKEN);
})();
在db.js
,您还需要从Promise
链的最后一步返回connection
:
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((connection) => {
console.log(`Connected to MySQL Database.`);
return connection;
})
.catch(err=>console.error(err));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.