等待使用sqlite的JS文件3

发布于 2025-02-10 04:54:49 字数 1034 浏览 3 评论 0原文

我是Discord Bot开发人员,我将数据库.db文件用作数据库。我在JS中代码,因此我安装了SQLITE3模块。我的“ messageCreate”事件的代码就在那里:

module.exports = {
  name: "messageCreate",
  once: false,
  async execute(client, message) {
    let prefix;
    await client.db.get(
      `SELECT prefix FROM "Guilds" WHERE id = "${message.guild.id}"`,
      async (err, row) => {
        prefix = row.prefix;
      }
    );
    if (message.author.bot) return;
    if (!message.content.startsWith(prefix)) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const cmdName = args.shift().toLowerCase();

    if (cmdName.length == 0) return;

    let cmd = client.commands.get(cmdName);
    if (cmd) {
      cmd.run(client, message, args);
    }
  },
};

我想将.get()函数的重新放在prefix actible中,但是当我尝试运行时代码,它返回未定义,因此我做了console.log(),并看到代码没有等待结果并继续运行。 因此,问题是我不知道如何等待.get()函数的结果。我试图放置等待,但我什么也没做。是否有另一种方法来等待结果,然后创建一个与结果中的变量?

I'm a discord bot developer and I use a database.db file as a database. I code in JS so i installed the sqlite3 module. My code for the 'messageCreate' event is right there:

module.exports = {
  name: "messageCreate",
  once: false,
  async execute(client, message) {
    let prefix;
    await client.db.get(
      `SELECT prefix FROM "Guilds" WHERE id = "${message.guild.id}"`,
      async (err, row) => {
        prefix = row.prefix;
      }
    );
    if (message.author.bot) return;
    if (!message.content.startsWith(prefix)) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const cmdName = args.shift().toLowerCase();

    if (cmdName.length == 0) return;

    let cmd = client.commands.get(cmdName);
    if (cmd) {
      cmd.run(client, message, args);
    }
  },
};

I want to put the resut of the .get() function in the prefix variable, but when I try to run the code, it returns undefined, so I did a console.log() and saw that the code didn't await the result and continued to run.
So the problem is that I don't know how to await the result of the .get() function. I tried to put an await, but i didn't do anything. Is there another way to wait the result and then create a variable with the result in it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

却一份温柔 2025-02-17 04:54:49

使用Promise构造函数()。

const prefix = await new Promise((resolve, reject) =>
  client.db.get(SQL,
    (err, row) => err ? reject(err) : resolve(row.prefix)
  )
)

Use Promise constructor (mdn).

const prefix = await new Promise((resolve, reject) =>
  client.db.get(SQL,
    (err, row) => err ? reject(err) : resolve(row.prefix)
  )
)
新雨望断虹 2025-02-17 04:54:49

您能检查是否有错误?我推测您在SQL语法中有一个错误,因为您在此处围绕表名的引号:

SELECT prefix FROM "Guilds" WHERE id = "${message.guild.id}"
                   ^^^^^^^^

添加此信息以进行故障排除目的,让我知道它返回了什么:

      async (err, row) => {
        if (err) throw err;
        prefix = row.prefix;
      }

Could you check if there's an error? I speculate that you have an error in your SQL syntax, since you are putting quotes around the table name here:

SELECT prefix FROM "Guilds" WHERE id = "${message.guild.id}"
                   ^^^^^^^^

Add this for troubleshooting purposes and let me know what it returns:

      async (err, row) => {
        if (err) throw err;
        prefix = row.prefix;
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文