查找具有匹配角色标准的Discord用户

时间:2020-02-10 04:12:52

标签: javascript arrays string discord discord.js

首先,在开始这个项目之前,我对这种事情零经验。我已经使用了各种资源来实现一个功能全面的bot,但是我仍然想添加一个关键功能。

我要设置的是Discord服务器的游戏配对系统。为此,用户将选择我已经设置并正在工作的定时“配对”角色。

我想做的还是遇到麻烦的是,我想从那一卷中拉随机用户,这些用户还从代表“语言”的两个独立的“子角色”组中至少具有一个匹配角色(如以已知用于通讯目的的语言”和“硬件”(例如,他们将在哪种平台上进行游戏)。

我认为这可以通过数组/字符串或类似方式完成,并设置以下框架:

const english = message.guild.roles.get('671416995253714965');
const spanish = message.guild.roles.get('671417045916712961');
const french = message.guild.roles.get('671417095166361603');
const german = message.guild.roles.get('671417141731262516');
const japanese = message.guild.roles.get('671417224531279962');
const korean = message.guild.roles.get('671417269666185239');
const portuguese = message.guild.roles.get('671417316193599500');
const russian = message.guild.roles.get('671417467888730122');
const italian = message.guild.roles.get('671418325661777932');
const chinese = message.guild.roles.get('671418692742938647');

const pc = message.guild.roles.get('670715882292641812');
const mobile = message.guild.roles.get('670716301135839282');
const playstation = message.guild.roles.get('670716107929419789');
const xbox = message.guild.roles.get('670715986655313941');
const nintendo = message.guild.roles.get('670716141915602955');

let languages = ['english', 'spanish', 'french', 'german', 'japanese', 'korean', 'portuguese', 'russian', 'italian', 'chinese']
let hardware = ['pc', 'mobile', 'playstation', 'xbox', 'switch']

编辑:在Cipher的帮助下,我现在编写了一段代码,可以针对单个组合使用。我可以对每种组合使用此格式来创建一个长字符串,以给出所需的结果。我仍然想遍历数组或其他东西来清理和简化代码。以下是其中一种组合的功能代码示例。

V2:(d)是“对接会”角色

var myItaxbo = setInterval(() => {
    let itaxboList = message.guild.members.filter(member => member.roles.array().includes(d) && member.roles.array().includes(italian) && member.roles.array().includes(xbox));
    if(itaxboList.size > 1) {
    console.log(itaxboList.size)
    let randItaxbo = itaxboList.random(2)
    let firstItaxbo = randItaxbo[0]
    let secondItaxbo = randItaxbo[1]
    firstItaxbo.removeRole(d).catch(console.error);
    secondItaxbo.removeRole(d).catch(console.error);
    matchmakingChannel.send("Users <@" + firstItaxbo + "> and <@" + secondItaxbo + "> have been matched!")
    .then(msg => {
        msg.delete(3.6e+6)
    })
    } else {
        console.log('No itaxbo members')
        clearInterval(myItaxbo);
    };
    }, 5000);

V3

var mat = setInterval(() => {
    let membersList = message.guild.members.filter(member => member.roles.array().includes(d) && member.roles.some(role => languages.includes(role.id)) && member.roles.some(role => hardware.includes(role.id)));
    if (membersList.size > 1) {
    console.log(membersList.size)
    let randMembers = membersList.random(2)
    let firstMember = randMembers[0]
    let secondMember = randMembers[1]
    firstMember.removeRole(d).catch(console.error);
    secondMember.removeRole(d).catch(console.error);
    matchmakingChannel.send("Users <@" + firstMember + "> and <@" + secondMember + "> have been matched!")
    .then(msg => {
        msg.delete(3.6e+6)
    })
    } else {
        console.log('No members')
        clearInterval(mat);
    };
    }, 5000);

1 个答案:

答案 0 :(得分:0)

您可以这样做。筛选GuildMembers成员,然后检查它们是否具有游戏和语言角色,以及是否有2个rand成员的rand集合。

    client.on('message', message => {
        const languages = ['english', 'spanish', 'french', 'german', 'japanese', 'korean', 'portuguese', 'russian', 'italian', 'chinese']
        const hardware = ['pc', 'mobile', 'playstation', 'xbox', 'nintendo']
        let membersList = guild.members.filter(member => member.roles.some(role => languages.includes(role.name)) && member.roles.some(role => hardware.includes(role.name)))
        if(membersList.size < 2) {
            console.log('No members')
            return
        }

        let randMembers = membersList.random(2)
        let firstMember = randMembers.first();
        let secondMeber = randMembers.last();

    });

V2版本

bot.on('message', message => {
  const languages = ['english', 'spanish', 'french', 'german', 'japanese', 'korean', 'portuguese', 'russian', 'italian', 'chinese']
  const hardware = ['pc', 'mobile', 'playstation', 'xbox', 'nintendo']
  let membersList = message.guild.members.filter(member => member.roles.some(role => languages.includes(role.name)) && member.roles.some(role => hardware.includes(role.name)))
  console.log(membersList.size)
  if(membersList.size < 2) {
      console.log('No members')
      return
  }

  let randMembers = membersList.random(2)
  let firstMember = randMembers[0]
  let secondMeber = randMembers[1]
});

V3版本尝试通过ID获取角色

bot.on('message', message => {
    const languages = ['123123', '12321312', '213213123'];
    const hardware = ['12321312', '213213213', '213213123'];
    let membersList = message.guild.members.filter(member => member.roles.some(role => languages.includes(role.id)) && member.roles.some(role => hardware.includes(role.id)));
    console.log(membersList.size);
    if (membersList.size < 2) {
        console.log('No members');
        return;
    }

    let randMembers = membersList.random(2);
    let firstMember = randMembers[0];
    let secondMeber = randMembers[1];
});