提问者:小点点

使用来自mysql的数据时发送多个嵌入而不是一个嵌入的不和谐Bot


嘿,所以我为minecraft服务器编写了一个不和谐机器人,它可以显示服务器上玩家的前10个游戏时间。但它不是发送一个嵌入它发送10个嵌入,一个为每一个球员在10行我查询任何帮助将被主持!


var now = new Date();
var playtimes = '../cost of ballin/PLAYTIMES/playtime-' + now.getFullYear() + "-" + now.getMonth() + "-" + now.getDate() + '.txt'

client.on("message", (msg) => {
    if (msg.content == prefix + "pt") {
        con.query("SELECT DAYS,NAME,MINUTES,HOURS FROM playtimes ORDER BY DAYS DESC LIMIT 10",
            function(err, result) {
                if (err) throw err;
                Object.keys(result).forEach(function(key) {
                    var row = result[key];
                    fs.appendFileSync(playtimes, "Player Name: " + row.NAME + " Days: " + row.DAYS + " Hours: " + row.HOURS + " Minutes: " + row.MINUTES + "\r\n")
                    var file = fs.readFileSync(playtimes)
                    const embed = new Discord.MessageEmbed()
                        .setColor('#000000')
                        .setTitle('Playtime Leaderboard')
                        .setDescription(playtimes)
                    wait(10000)
                    channel.send(embed);
                })

            }
        );
    }
});

共1个答案

匿名用户

我在这里看到的问题是,每次运行.foreach()循环时,您都在执行channel.send()

每次运行它时,它都会发送一条消息。相反,您应该做的是创建某种对象,存储从查询中获得的统计数据,然后在.foreach()循环完成后发送消息。例如:

var now = new Date();
var playtimes = '../cost of ballin/PLAYTIMES/playtime-' + now.getFullYear() + "-" + now.getMonth() + "-" + now.getDate() + '.txt'

client.on("message", (msg) => {
    if (msg.content == prefix + "pt") {

        let playTimeStats = {} //create a dictionary for the stats

        con.query("SELECT DAYS,NAME,MINUTES,HOURS FROM playtimes ORDER BY DAYS DESC LIMIT 10",
            function(err, result) {
                if (err) throw err;
                Object.keys(result).forEach(function(key) {
                    var row = result[key];
                    fs.appendFileSync(playtimes, "Player Name: " + row.NAME + " Days: " + row.DAYS + " Hours: " + row.HOURS + " Minutes: " + row.MINUTES + "\r\n")
                    var file = fs.readFileSync(playtimes)
    
                    playTimeStats[row.NAME] = {
                        "Days": row.DAYS,
                        "Hours": row.HOURS,
                        "Minutes": row.MINUTES
                    } //add the stats to the dictionary                   

                })
            }
        );

//make an embed here for the stats, and then send it

    }
});

我还没有为你写好所有的代码,但是我已经添加了我认为你需要开始的东西...我已经添加了字典和如何添加元素到它,你只需要制作和发送嵌入。希望这有帮助!