【问题标题】:Discord.js v12 - Balance Command with MySQLDiscord.js v12 - MySQL 的平衡命令
【发布时间】:2020-10-15 02:31:32
【问题描述】:

我目前正在尝试为我的不和谐机器人创建一个货币系统。 余额本身正在工作,但我遇到的问题是,如果用户 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.js discord.js


    【解决方案1】:

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

    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;
        }
    })
    

    【讨论】:

    • 哦,这更有意义!我真的很高兴你以这种方式解释了我做错了什么,这实际上有很大帮助。特别是因为我对 nodejs 和 mysql 还很陌生。真的很感激!
    • 如果用户ID不存在,它似乎仍然没有插入balance
    • 但是它插入了用户ID?
    • 哦完全忘记了我真正想写什么。它不插入任何东西
    • 检查你的初始查询是否真的返回了一些东西。它会给你任何错误吗?
    猜你喜欢
    • 2021-01-01
    • 2021-02-18
    • 1970-01-01
    • 2021-01-18
    • 2021-04-14
    • 2021-04-19
    • 2021-04-18
    • 2021-04-23
    • 2021-01-30
    相关资源
    最近更新 更多