Discord机器人角色命令由于未知原因停止工作

时间:2019-06-13 05:56:44

标签: discord.js

我有一个有效的命令,但在某些时候停止了,返回到该角色不存在的聊天消息。每次我使用命令时,都会向控制台发送错误消息“(节点:12228)DeprecationWarning:Collection#find:改为传递函数”,但我总是遇到该错误

const Discord = require("discord.js");

module.exports.run = async (bot, message, args) => {

  if(!message.member.hasPermission("MANAGE_MEMBERS")) return message.reply();
  let rMember = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0]);
  if(!rMember) return message.reply("nope.");
  let role = args.join(" ").slice(22);
  if(!role) return message.reply("nope!");
  let gRole = message.guild.roles.find(`name`, role);
  if(!gRole) return message.reply("role does not exist.");
  const allowed = ['some id'];

  if (!allowed.includes(gRole.id)) return;
  if(rMember.roles.has(gRole.id)) return message.reply("nope.");
  await(rMember.removeRoles(['some id']));
  await(rMember.addRole(gRole.id));
  if(gRole.id == 'id') rMember.addRole('id') && rMember.removeRoles(['some id']);;

  try{
    await rMember.send(`you got ${gRole.name}!`)
  }catch(e){
  }
}

module.exports.help = {
  name: "role"
}

所以我需要命令才能工作。

1 个答案:

答案 0 :(得分:0)

从外观上看,我在您的代码中看到了这些错误...

结合您对本代码的其他疑问提供的答案,这是一种正确,更简洁的重写方法……

const getMention = require('discord-mentions'); // Don't forget to install.

module.exports.run = async (bot, message, args) => {
  if (!message.guild) return;

  try {
    if (!message.member.hasPermission('MANAGE_ROLES')) return;

    if (!args[1]) return await message.reply('Please provide a user.');
    if (!args[2]) return await message.reply('Please provide a role.');
    if (args[3]) return await message.reply('Too many arguments provided.');

    let member;
    if (getMention(args[1])) member = getMention(args[1], message.guild).member;
    else member = message.guild.members.find(m => m.user.tag === args[1] || m.id === args[1]);

    if (!member) return await message.reply('Invalid user provided.');

    let role;
    if (getMention(args[2])) role = getMention(args[2], message.guild).role;
    else role = message.guild.roles.find(r => r.name === args[2] || r.id === args[2]);

    if (!role) return await message.reply('Invalid role provided.');

    const allowed = ['role1ID', 'role2ID', 'role3ID'];
    if (!allowed.includes(role.id)) return await message.reply('That role is not allowed.');

    if (member.roles.has(role.id)) return await message.reply('That user already has that role.');

    await member.removeRoles(['someID', 'someOtherID']);
    await member.addRole(role.id);

    if (role.id === 'anAlternateRoleID') {
      await member.removeRoles(['someID', 'someOtherID']);
      await member.addRole('otherRoleID');
    }

    await member.send(`You got the ${role.name} role.`)
      .catch(() => console.log(`Couldn't send message to ${member.user.tag}.`));
  } catch(err) {
    console.error(err);
  }
};

module.exports.help = {
  name: 'role'
};

请记住,如果要从成员中删除一个角色,则应使用方法的单数版本GuildMember.removeRole()