首页 > 解决方案 > Discord.js v12 - MySQL 的平衡命令

问题描述

我目前正在尝试为我的不和谐机器人创建一个货币系统。余额本身正在工作,但我遇到的问题是,如果用户 ID 尚不存在,它不会将用户 ID/任何余额添加到数据库中。

代码:

const Discord = require("discord.js");
const config = require("../config.json");
const mysqlconfig = require("../mysqlconfig.json");
const path = require('path');
const fs = require('fs');
const date = new Date();
const day = date.getDate();
const month = date.getMonth();
var mysql = require('mysql');
const { release } = require("os");

module.exports.run = async (bot, message, args) => {

    var con = mysql.createPool({
        host: mysqlconfig.host,
        user: mysqlconfig.user,
        password: mysqlconfig.password,
        database: mysqlconfig.database,
        port: 3306
      });
            
            var sql = `SELECT * FROM money WHERE userID = '${message.author.id}'`
                con.query(sql, async function (err, results, userID, balance, fields, rows) {
                    let data = results.map(v => {
                        //console.log(v.userID)
                        if(message.author.id = v.userID) { // checks if message.author.id is equal to any id in the database
                        message.channel.send(`You have **${v.balance}** YeetCoins!`)
                        return v.balance
                        } else { // if not it should add the id and also add 100 coins
                        var sql1 = `INSERT INTO balance (userID, balance) VALUES (?, ?) ON DUPLICATE KEY UPDATE userID=VALUES(userID)`

                        con.query(sql1,[message.author.id, '100'], function (err, result, balance, userID) {
                            
                        });
                        message.channel.send(`added 100 coins to <@${message.author.id}>'s balance!`)
                        }
                        
                    })
                })
}

module.exports.help = {
    name: "balance",
    category: "User"
}

可悲的是,我不确定自己做错了什么。希望有人可以帮助我

标签: node.jsdiscord.js

解决方案


问题是当您尝试插入新成员时正在调用一个函数。你不需要那个。您要做的就是在表格中插入一些东西。不需要函数。

con.query(sql1, [message.author.id, '100']);

注意:当你第一次查询数据库时,你调用了一堆你不需要的参数。你需要的是errresults。一旦数据在那里,你就不需要map它了。您可以使用它。

除此之外,您首先检查用户是否存在的方式有点不稳定。您尝试调用可能不存在的东西,因为如果用户存在则SELECT * FROM money WHERE userID = '${message.author.id}'应该返回一行,如果不存在则返回零行。因此,在检查 id 是否匹配之前,您应该检查是否存在行。

if (!results.length) {
    // here we insert the new user
}

如果您的查询返回了某些内容,您可以检查用户 ID 是否匹配。如果不是,您的数据库有问题,但检查一下也无妨。注意:我们需要在===这里使用,因为我们要检查确切的值。

if (message.author.id === results[0].userID) {
    // return the balance here
}

一旦一切都说完了,你得到的查询应该看起来像这样。

con.query(sql, function (err, results) { // we don't need the other values
    if (err) throw err; // check if something went wrong

    if (!results.length) { // check if there are rows and insert if there aren't
        var sql1 = `INSERT INTO money (userID, balance) VALUES ('${message.author.id}', '100')`;
        con.query(sql1); // no need for a function here
        return message.channel.send(`added 100 coins to ${message.member}'s balance!`); // return here so the rest doesn't get executed
    }
    // check if the IDs match
    if (message.author.id === results[0].userID) {
        message.reply(`You have **${results[0].balance}** YeetCoins!`); // we reply to the user who calls this command.
        return results[0].money;
    }
})

推荐阅读