开发Discord每日金币功能时遇TypeError: ms is not a function错误
解决Discord机器人每日金币功能的TypeError: ms is not a function错误
错误原因
你使用了动态import("parse-ms"),它返回的是一个Promise对象,而非直接可用的函数,因此调用ms()时会触发类型错误。同时你的代码采用CommonJS模块规范(用require导入discord.js),应使用对应规范的方式导入parse-ms。
修复方案
1. 修正parse-ms的导入方式
将动态import替换为CommonJS的require导入:
const ms = require("parse-ms");
2. 优化数据库操作(可选)
给db.add和db.set加上await,确保数据库操作完成后再执行后续逻辑,避免异步操作引发的潜在问题。
修改后的完整代码
const discord = require("discord.js"); const ms = require("parse-ms"); module.exports = { name: "daily", aliases: ["coins-system"], category: "economy", description: "Gives You 200 per day", usage: " ", accessableby: "everyone", run: async (client, message, args, db) => { let blacklisted = await db.get(`blacklisted_${message.author.id}`); if (blacklisted) return message.channel.send('You cannot use this bot since you are blacklisted'); let user = message.author; let timeout = 86400000; let amount = 150; let vip = await db.get(`vip_${message.author.id}`); if (vip) amount = 350; let daily = await db.get(`daily_${user.id}`); if (daily !== null && timeout - (Date.now() - daily) > 0) { let time = ms(timeout - (Date.now() - daily)); let timeEmbed = new discord.EmbedBuilder() .setDescription(`❌ You've already collected your daily reward\n\nCollect it again in ${time.hours}h ${time.minutes}m ${time.seconds}s `); message.channel.send(timeEmbed); } else { let moneyEmbed = new discord.EmbedBuilder() .setDescription(`✅ You've collected your daily reward of ${amount} coins`); message.channel.send(moneyEmbed); await db.add(`cash_${user.id}`, amount); await db.set(`daily_${user.id}`, Date.now()); } } };
内容的提问来源于stack exchange,提问作者Merton Support
相关产品推荐
相关产品推荐

