discord.js 机器人如何从 Mysql 数据库获取数据?

问题描述 投票:0回答:1

我有欢迎系统的代码,我在其中保存公会的数据,并在会员加入时获取此数据。但是我在从数据库获取数据时遇到问题,我该如何解决它?

数据库截图:https://imgur.com/a/N4q8d7b

但是当我访问服务器时,该行只是跳过并且不执行任何操作

run()
async function run() {
    const db = await database.connect({ // creates a database connection
        host: 'localhost',
        port: '3306', // the default is 3306
        user: 'root',
        password: 'root123',
        database: 'database1',
        charset: 'utf8mb4'
    });


        client.on(Events.GuildMemberAdd, async (member) => {
            console.log(`User ${member.user.username} (${member.id}) join guild and add to database`)
            db.query(`SELECT welcomeChannel FROM welcomes WHERE "guildId" = '${member.guild.id}';`, function(result){
                console.log(`Result:${result}`)
            })
        
                console.log("1")
  
                const embed = new EmbedBuilder()
                .setDescription(`Пользователь ${member} присоединился к серверу!`)
                //.setColor(embedcolor)
                //const channel = member.guild.channels.cache.get(Channelid)
                //channel.send({embeds: [embed]})
        })
};
  module.exports = {run}
mysql discord discord.js
1个回答
0
投票

我不认为我完全明白你在说什么,但我确实有一种感觉。

db.query
返回 3 个对象。这些按顺序是:

  1. 错误,当你有一个时
  2. 包含结果的数组
  3. 包含有关结果的额外元数据的字段(如果可用)(您的情况并非真正需要)

按照你这样做的方式,它看起来像这样:

db.query(`YOUR QUERY`, function (error, results) {
  if (error) throw error;
  console.log(results);
});

您在第一个位置有

results
,当有一个时就会出现错误,这就是为什么您没有得到任何结果。

您可以在文档这里阅读更多内容,点击“回调”查看您正在使用的方法。

© www.soinside.com 2019 - 2024. All rights reserved.