'use strict';

const Command = require('../../Command.js');
const rpad = require('right-pad');

function createGroupedArray(arr, chunkSize) {
  const groups = [];
  for (let i = 0; i < arr.length; i += (chunkSize || 10)) {
    groups.push(arr.slice(i, i + (chunkSize || 10)));
  }
  return groups;
}

/**
 * Add a joinable role
 */
class Roles extends Command {
  constructor(bot) {
    super(bot, 'settings.roleids', 'roleids', 'Get list of role ids');
    this.allowDM = false;
    this.requiresAuth = true;
  }

  /**
   * Run the command
   * @param {Message} message Message with a command to handle, reply to,
   *                          or perform an action based on parameters.
   */
  run(message) {
    const roles = message.guild.roles.array().sort((a, b) => { 
      if (a < b) {
        return -1;
      } else if (a > b) {
        return 1;
      } else {
        return 0;
      }
    });
    const longest = roles.map(role => role.name)
     .reduce((a, b) => (a.length > b.length ? a : b));
    const roleGroups = createGroupedArray(roles.map(role => `\`${rpad(role.name, longest.length, ' ')} ${role.id}\``), 20);
    this.messageManager.embed(message,
      {
        fields: roleGroups.map(roleGroup => ({
          name: '_ _',
          value: roleGroup.join('\n'),
        })),
      }, true, false);
  }
}

module.exports = Roles;

@@ -783,12 +783,22 @@
 f (a
- %3C b
+.name %3C b.name
 ) %7B%0A
@@ -834,20 +834,30 @@
 se if (a
- %3E b
+.name %3E b.name
 ) %7B%0A    
@@ -1147,16 +1147,116 @@
 ), 20);%0A
+    const metaGroups = createGroupedArray(rolegroups, 4);%0A    metagroups.forEach((metaGroup) =%3E %7B%0A  
     this
@@ -1310,26 +1310,25 @@
 fields: 
-role
+meta
 Group
-s
 .map(rol
@@ -1438,16 +1438,24 @@
 false);%0A
+    %7D);%0A
   %7D%0A%7D%0A%0Am

update roleids grouping