Quick.db discord.js

Posted

技术标签:

【中文标题】Quick.db discord.js【英文标题】: 【发布时间】:2021-02-20 03:25:18 【问题描述】:

我正在创建一个从 .db 文件返回项目的 userinfo 命令,但它返回为 [object Object]。我如何使它返回名称? 这是我的代码:

const  MessageEmbed  = require(`discord.js`)
const db = require(`quick.db`);

exports.execute = async(client, message, args) => 
    let member = message.mentions.members.first() || message.member;
    let hasStarted = db.fetch(`started_$member.id`)
    let bal = db.fetch(`money_$member.id`)
    let items = db.get(`items_$member.id`)
    let displayItems;
    let started;

    if (bal === null) bal = 0;
    if (items === null) 
        displayItems = 'This user has no items!';
     else 
        displayItems = items;
    
    if (hasStarted === null) 
        started = 'False'
     else 
        started = 'True'
    

    const userEmbed = new MessageEmbed()
    .setTitle(member.displayName + '#' + member.user.discriminator + "'s Profile")
    .addField(`Balance:`, bal)
    .addField(`Items:`, displayItems)
    .addField(`Has Started:`, started)
    .setFooter(`Requested By: $message.author.username`, message.author.avatarURL( dynamic:true ))
    .setImage(member.user.avatarURL( dynamic:true ))
    message.channel.send(userEmbed)


exports.help = 
    name: 'userinfo',
    aliases: ['user'],
    usage: 'userinfo <user>'

【问题讨论】:

[object Object] 被显示时,这意味着你要显示的东西是一个对象。尝试console.log() 以查看该对象中的内容。 Quick.db 是一个基于 Promise 的节点包。当某事基于承诺时,通常意味着您必须等待它们。请记住,您只能等待异步函数中的项目。 【参考方案1】:

Discord 不发送对象,而是发送字符串。如果数据是一个对象,你应该尝试:

// Send this, not object:
c.send(JSON.stringify(object)) // Also, you should wrap in a code block with the JSON language, it'll look much nicer

如果是 Promise,你应该尝试:

c.send(await promise)

【讨论】:

【参考方案2】:

试试这个

let member = message.mentions.members.first() || message.member;
let hasStarted = db.fetch(`started_$member.id`)
let bal = db.fetch(`money_$member.id`)
let items = db.get(`items_$member.id`)
let displayItems;
let started;

if (bal === null) bal = 0;
if (items === null) 
    displayItems = 'This user has no items!';
 else 
    displayItems = items;

if (hasStarted === null) 
    started = 'False'
 else 
    started = 'True'


const userEmbed = new MessageEmbed()
.setTitle(member.displayName + '#' + member.user.discriminator + "'s Profile")
.addField(`Balance:`, `$bal`)
.addField(`Items:`, `$displayItems`)
.addField(`Has Started:`, `$started`)
.setFooter(`Requested By: $message.author.username`, message.author.avatarURL( dynamic:true ))
.setImage(member.user.avatarURL( dynamic:true ))
message.channel.send(embeds: [userEmbed)

【讨论】:

考虑为代码的作用以及它如何解决问题添加更多描述。 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于Quick.db discord.js的主要内容,如果未能解决你的问题,请参考以下文章

如何安装 quick.db?

尝试关闭并打开命令 quick.db

将用户名存储在 quick.db 数据库中

如何将反应存储到 quick.db

如何安装 quick.db? (再次)

如何使 quick.db 异步读取