【问题标题】:Discord.js-commando fetching username from idDiscord.js-commando 从 id 获取用户名
【发布时间】:2020-07-05 14:00:08
【问题描述】:

我正在尝试从从数据库返回的 ID 中查找用户名,但是我遇到了问题。我遇到的问题是用户在突击队命令中,而且我似乎无法运行 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;

【问题讨论】:

  • 从您的代码来看,问题出在哪里还不是很清楚,从技术上讲,您可以在声明它的地方使用任何 Discord 方法。请指定任务并提供更多您有困难的代码。据我了解,这里涉及到几个模块。
  • @Cipher 我会把所有的代码都加进去,就这么简单

标签: node.js discord.js


【解决方案1】:

您现在好像忘记声明 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;

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2021-05-01
    • 1970-01-01
    • 2020-08-02
    相关资源
    最近更新 更多