Trying to select a random user against the current user for a match with both having the same role

166 Views Asked by At

I am currently trying to create some sort of matchmaking command for my discord code.

The first half works fine, but the second part is giving me quite a headache. To explain how I want the code to work is for example, There are a number of users having the same role and with this command, I want it to choose two users. One being me (the user using the command) and another random from the list.

I got it to work but sometimes I might get chosen as the second member which if possible, I'd like to exclude myself from the list. Consider it some sort of myself finding a random duel.

Upon successful selection, the command will remove the queue role and add in the new matching role. I hope my explanation is clear. Thanks in advance!

client.on("message", msg=> {
  if(msg.content.toLowerCase() === "!queuepvp") {
    const pvpqueue = ['713765272162402355'];
    let membersList = msg.guild.members.filter(member => member.roles.some(role => pvpqueue.includes(role.id)));
    console.log(membersList.size);
    if(membersList.size < 2) {
      msg.reply('Not enough PVP seekers in queue.');
      return;
    }
    else{
    let randMembers = membersList.random(1);
    let firstMember = msg.member;
    let secondMember = randMembers[1];
    if (!secondMember === msg.member) {
      msg.reply("You are matched up against " + secondMember + "!")
      firstMember.addRole('713765226495082496');
      secondMember.addRole('713765226495082496');
      firstMember.removeRole('713765272162402355');
      secondMember.removeRole('713765272162402355');
  }}}
2

There are 2 best solutions below

1
On

you can filter the randMembers like randMembers = membersList.fileter(member => member.member !== msg.member).random(1)

0
On

I managed to pin this down through msg.member.id after countless trial and errors.