Search code examples
node.jsdiscord.js

why it's not split the roles


const { EmbedBuilder, SlashCommandBuilder } = require('discord.js');
const mongoose = require('mongoose');

const AdminsSchema = mongoose.model('Admins Binds');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('admins_view')
    .setDescription('View all the admins level for this server.'),

  async execute(interaction) {
    const discordavatar = interaction.user.displayAvatarURL();
    const discordUsername = interaction.user.username;
    const guild_id = interaction.guild.id;
    const guildname = interaction.guild.name;

    const admins = await AdminsSchema.find({ guildid: guild_id }).select('AdminRole Adminlevel');

    const adminLevels = admins.reduce((levels, admin) => {
        if (!levels[admin.Adminlevel]) {
          levels[admin.Adminlevel] = [];
        }
        levels[admin.Adminlevel].push(admin.AdminRole);
        return levels;
      }, {});
      

    const adminFields = Object.entries(adminLevels).map(([level, roles]) => ({
        name: `Admin Level ${level}`,
        value: roles.map(role => `- <@&${role}>`).join('\n'),
      }));      
      
    try {
      const message = new EmbedBuilder()
        .setColor(0x0099FF)
        .setTitle('Server Admins')
        .setDescription(`Listing server level admins for the server ${guildname}`)
        .setAuthor({ name: discordUsername, iconURL: interaction.user.displayAvatarURL() })
        .addFields(adminFields);

      await interaction.reply({ embeds: [message] });
    } catch (err) {
      console.log(err);
      const errorEmbed = new EmbedBuilder()
        .setDescription(`An error occurred while loading server admins: ${err.message}`)
        .setColor('#ff2d08');
      interaction.edit({ embeds: [errorEmbed], ephemeral: false });
    }
  }
};

output:

output

It should be (go to image 2) and also mention the role. I also have no idea why it's not creating a new line since I'm using /n. Can someone help me?

image

using Node.js v18.15.0. discord js


Solution

  • The map isn't doing what you expect it to.

    I suggest simplifying it like this:

    const admins = await AdminsSchema.find({ guildid: guild_id }).select('AdminRole Adminlevel').sort({ Adminlevel: -1 }).exec()
    
    
      const message = new EmbedBuilder()
        .setColor(0x0099FF)
        .setTitle('Server Admins')
        .setDescription(`Listing server level admins for the server ${guildname}`)
        .setAuthor({ name: discordUsername, iconURL: interaction.user.displayAvatarURL() })
    
      let adminlist = []
      for (const admin of admins) {
        let thisLevel = adminlist.find(a => a.level === admin.Adminlevel)
        if (!thisLevel) {
          thisLevel = { level: admin.Adminlevel, roles: ["<@&" + admin.AdminRole + ">"] }
          adminlist.push(thisLevel)
        } else {
          thisLevel.roles.push("<@&" + admin.AdminRole + ">")
        }
      }
    
      for (const admin of adminlist) {
         message.addFields([ { name: `Level ${admin.level}`, value: admin.roles.join("\n") } ])
      }
    

    This should do what you expect in a easy to understand way.