Discord.js-commando从ID中获取用户名

时间:2020-03-25 01:26:33

标签: node.js discord.js

我正在尝试从数据库返回的ID中查找用户名,但是,我遇到了问题。我遇到的问题是用户在Commando命令中,并且似乎无法运行discord.js函数(如.fetchUser)。我已经尝试过了,但是,你们所有人都可能知道该做些什么比我更好!

const commando = require ('discord.js-commando');
const { RichEmbed } = require('discord.js');
const sqlite3 = require ('sqlite3');
class claim extends commando.Command
{
    constructor(client )
    {
        super(client,{
            name: 'marry',
            group: 'simple',
            memberName: 'marry',
            description: 'Marries the user tagged for a specified cost.'
        });
    }

    async run(message, args)
    {
      var updatedbalance;
      const string = message.content.slice(1).trim().split(/ +/g);
      const user = message.mentions.users.first().id;
      const username = message.mentions.users.first();
      const payment = string[2]
      let db = new sqlite3.Database('./duncanbot.db')
      function getBalance(id, callback)
      {
        var query = "SELECT balance FROM main WHERE userid = " + id;
        db.all(query, function (err, rows) {
          if(err){
              console.log(err);
          }
          else
          {
            try{
              callback(rows[0].balance);
            }
            catch (err)
            {
              console.log(err)
            }
          }

        });
      }
      function getAvailPrice(mention, callback)
      {
        var query = "SELECT price FROM partners WHERE partner = " + mention;
        db.all(query, function (err, rows) {
          if(err){
              console.log(err);
          }
          else
          {
            try
            {
              callback(rows[0].partner, rows[0].price);
            }
            catch (err)
            {
              callback(mention, '0')
              console.log(err)
            }
          }

        });
      }
      function findBal(balance) 
      {
        updatedbalance = balance - payment
        if (balance >= payment)
        {
          getAvailPrice(user, findAval);
        }
        else 
        {
          const marryembed = new RichEmbed()
          .setTitle('You do not have the required funds')
          .setAuthor(message.author.username, message.author.displayAvatarURL)
          .setColor(0x8AC784);
          message.channel.send(marryembed); 
        }
      }
      function updateDB()
      {
        db.run(`INSERT INTO partners (userid, partner, price) VALUES(${message.author.id}, ${user}, ${payment})`)
      }
      function findAval(partner, price) {
        if (payment > price) 
        {
          try{
            db.run(`UPDATE main SET balance = ${updatedbalance} WHERE userid = ${message.author.id}` );
            if (price == 0)
            {
              db.run(`INSERT INTO partners (userid, partner, price) VALUES(${message.author.id}, ${user}, ${payment})`);
            }
            else if (price > 0)
            {
              db.run(`DELETE FROM partners WHERE partner = (${user})`)
              setTimeout(updateDB,500)

            }
            else
            {
              console.log(err)
            }
            const marryembed = new RichEmbed()
                  .setTitle('You have married ' + username.username)
                  .setAuthor(message.author.username, message.author.displayAvatarURL)
                  .setColor(0x8AC784);
            message.channel.send(marryembed);     
          }
          catch(err)
          {
            console.log(err)
          }
        }
        else 
        {
          const marryembed = new RichEmbed()
          .setTitle('You must pay more than the current amount, which is ' + price)
          .setAuthor(message.author.username, message.author.displayAvatarURL)
          .setColor(0x8AC784);
          message.channel.send(marryembed); 
        }
      }
      getBalance(message.author.id, findBal);
      };     

}

module.exports = claim;

1 个答案:

答案 0 :(得分:0)

好像您此时id忘记声明function getBalance(id, callback) 也许您不需要尝试尝试的数量..赶上...,对不起,但回调的地狱。我建议您注意promise。 这将使您的代码更易于理解,并且更容易捕获错误。

const commando = require ('discord.js-commando');
const { RichEmbed } = require('discord.js');
const sqlite3 = require ('sqlite3');
class claim extends commando.Command
{
    constructor(client )
    {
        super(client,{
            name: 'marry',
            group: 'simple',
            memberName: 'marry',
            description: 'Marries the user tagged for a specified cost.'
        });
    }

    async run(message, args)
    {
      var updatedbalance;
      const string = message.content.slice(1).trim().split(/ +/g);
      const user = message.mentions.users.first().id;
      const username = message.mentions.users.first();
      const payment = string[2]
      let db = new sqlite3.Database('./duncanbot.db')
      let id = //SOMETHINK/////
      function getBalance(id, callback)
      {
        var query = "SELECT balance FROM main WHERE userid = " + id;
        db.all(query, function (err, rows) {
          if(err){
              console.log(err);
          }
          else
          {
            try{
              callback(rows[0].balance);
            }
            catch (err)
            {
              console.log(err)
            }
          }

        });
      }
      function getAvailPrice(mention, callback)
      {
        var query = "SELECT price FROM partners WHERE partner = " + mention;
        db.all(query, function (err, rows) {
          if(err){
              console.log(err);
          }
          else
          {
            try
            {
              callback(rows[0].partner, rows[0].price);
            }
            catch (err)
            {
              callback(mention, '0')
              console.log(err)
            }
          }

        });
      }
      function findBal(balance) 
      {
        updatedbalance = balance - payment
        if (balance >= payment)
        {
          getAvailPrice(user, findAval);
        }
        else 
        {
          const marryembed = new RichEmbed()
          .setTitle('You do not have the required funds')
          .setAuthor(message.author.username, message.author.displayAvatarURL)
          .setColor(0x8AC784);
          message.channel.send(marryembed); 
        }
      }
      function updateDB()
      {
        db.run(`INSERT INTO partners (userid, partner, price) VALUES(${message.author.id}, ${user}, ${payment})`)
      }
      function findAval(partner, price) {
        if (payment > price) 
        {
          try{
            db.run(`UPDATE main SET balance = ${updatedbalance} WHERE userid = ${message.author.id}` );
            if (price == 0)
            {
              db.run(`INSERT INTO partners (userid, partner, price) VALUES(${message.author.id}, ${user}, ${payment})`);
            }
            else if (price > 0)
            {
              db.run(`DELETE FROM partners WHERE partner = (${user})`)
              setTimeout(updateDB,500)

            }
            else
            {
              console.log(err)
            }
            const marryembed = new RichEmbed()
                  .setTitle('You have married ' + username.username)
                  .setAuthor(message.author.username, message.author.displayAvatarURL)
                  .setColor(0x8AC784);
            message.channel.send(marryembed);     
          }
          catch(err)
          {
            console.log(err)
          }
        }
        else 
        {
          const marryembed = new RichEmbed()
          .setTitle('You must pay more than the current amount, which is ' + price)
          .setAuthor(message.author.username, message.author.displayAvatarURL)
          .setColor(0x8AC784);
          message.channel.send(marryembed); 
        }
      }
      getBalance(message.author.id, findBal);
      };     

}
```javascript
module.exports = claim;