Discord.js v14调用quick.db的db.get时报错is not a function求助
解决Discord机器人messageCreate事件中
TypeError: db.get is not a function报错 TypeError: db.get is not a function
我为Discord机器人编写了messageCreate事件,原本运行正常,但突然出现上述报错。怀疑quick.db更新导致,但最新版本仍存在该问题,无法定位原因。使用Replit进行开发,代码如下:
module.exports = { name: "messageCreate", async execute(message) { //get prefix// PREFIX = require('../config.json') let prefix = await db.get(`prefix_${message.guild.id}`); //<<< Error// if (prefix === null) prefix = PREFIX; //if bot or not a command return// if (message.author.bot || !message.content.startWith(prefix)) return const args = message.content.slice(prefix.lenght).trim().split(/ +/); const command = args.shift().toLowerCase(); if (!client.commands.has(command) && !client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(command))) return var inputcommand = client.commands.get(command) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(command)) //if onlyAdmin = true if(inputcommand.onlyAdmin){ if(message.member.hasPermission("ADMINISTRATOR")) { message.channel.send("Only Administrators can run this command") return } } //run the given command// inputcommand.execute(message, args, client); } }
问题排查与修复方案
quick.db的导入与初始化错误
代码中未正确导入并初始化quick.db,v9+版本的quick.db需要通过类实例化才能使用:// 在文件顶部添加以下代码 const { QuickDB } = require('quick.db'); const db = new QuickDB();旧版本直接
const db = require('quick.db')的写法已被废弃,必须使用类实例化方式。依赖版本适配
若坚持使用旧版API,可以卸载当前版本后安装兼容版本:npm uninstall quick.db npm install quick.db@7.1.3但建议优先适配新版写法,避免后续版本兼容问题。
代码中的其他错误修正
- 拼写错误:
startWith改为startsWith,lenght改为length - 权限逻辑颠倒:管理员权限判断应该是没有权限时提示,修改为:
if(inputcommand.onlyAdmin){ if(!message.member.hasPermission("ADMINISTRATOR")) { message.channel.send("Only Administrators can run this command") return } } client未定义:execute方法需要传入client参数,修改事件定义为async execute(message, client),并在触发事件时传入client实例。
- 拼写错误:
内容的提问来源于stack exchange,提问作者BlackDelta
相关产品推荐
相关产品推荐

