繁体   English   中英

Discord.js 排序对象数组故障文本

[英]Discord.js Sorting Array Of Objects glitching text

我有这段代码,我在其中洗牌一个数组,并在 discord.js 嵌入中显示前 16 个值:

const items = require('../schemas/gameSchema');
const items0 = await items.find();
var shuffle = require('shuffle-array');

const items1 = shuffle(items0);

const lb = items1
  .slice(0)
  .map(
    ({ DepositAmount, BetAmount, GameName, TotalPlays }, pos) =>
      `**${GameName}** \n Initial Deposit Amount: **${commaNumber(
        DepositAmount,
      )}** <:bob:809551218217058354> \n Bet Amount:  **${commaNumber(
        BetAmount,
      )}** <:bob:809551218217058354> \n Total Game Plays : ${commaNumber(
        TotalPlays,
      )} 🕹 \n `,
  )
  .join('\n');
const lbnewnew = lb.slice(15);
const newlb = lbnewnew.toString();
let exampleEmbed = new Discord.MessageEmbed()
  .setColor('RANDOM')
  .setTitle('Discover Games')
  .setDescription(`${newlb}`)

  .setTimestamp()
  .setFooter(`You can play games with /game play <game name>`);

await interaction.reply({ embeds: [exampleEmbed] });

这是 gameSchema(在 items 常量中定义)

const mongoose = require('mongoose')

const adSchema = mongoose.Schema({
    User:String,
    Funds:Number,
    ProfitToClaim:Number,
    TotalProfit:Number,
    TotalLoss:Number,
    DepositAmount:Number,
    BetAmount:Number,
    TotalPlays:Number,
    TotalWins:Number,
    TotalLosses:Number,
    GameName:String,
    GameDescription:String,

})
module.exports = mongoose.model('NewGames', adSchema, 'NewGames')

我从架构中获取的所有内容都已定义,但是当我运行此命令时,显示的第一个元素的名称带有一些随机字母

在此处输入图像描述

或者像这样在此处输入图像描述

数据库中没有名为emial的游戏,所以我不确定为什么会这样

在此处输入图像描述

如何阻止它说出随机字符而不显示第一个元素的游戏名称? 为什么会这样?

这是因为您要切掉前 15 个字符(即lb.slice(15) )。 相反,您应该保留items1的前 15 个结果。 您也不需要所有这些toString和模板文字。

const description = items1
  .slice(0, 15)
  .map(
    ({ DepositAmount, BetAmount, GameName, TotalPlays }, pos) =>
      `**${GameName}** \n Initial Deposit Amount: **${commaNumber(
        DepositAmount,
      )}** <:bob:809551218217058354> \n Bet Amount:  **${commaNumber(
        BetAmount,
      )}** <:bob:809551218217058354> \n Total Game Plays : ${commaNumber(
        TotalPlays,
      )} 🕹 \n `,
  )
  .join('\n');

let exampleEmbed = new Discord.MessageEmbed()
  .setColor('RANDOM')
  .setTitle('Discover Games')
  .setDescription(description)
  .setTimestamp()
  .setFooter(`You can play games with /game play <game name>`);

await interaction.reply({ embeds: [exampleEmbed] });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM