【问题标题】:Can not use startsWith Discord.js question不能使用startsWith Discord.js 问题
【发布时间】:2019-07-01 22:21:19
【问题描述】:

这是我的角色命令。
我正在尝试使用startsWith 来赋予角色:如果arg 的第一个字母与角色相同,则应将其分配给用户。

exports.run = async function (msg,args) {
  if(!msg.member.hasPermission('MANAGE_ROLES')) return;
  const member = msg.mentions.members.first();
  const name = msg.mentions.users.size ? args.replace(/ +/, ' ').split(' ')[1] : args;
  const role = msg.guild.roles.find(r => r.name.toLowerCase().startsWith(name.toLowerCase()));
  if (name && !role.size) return msg.channel.send("Role Not found");
  if (name && role.size > 1) return msg.channel.send("There is similar roles , Please supply more letters");
  await member.addRole(role);
  msg.channel.send(`**Role \`${role}\`  Given to **${member} Succesflly`);
};

我更新了当前代码,但仍然无法根据 args 或首字母给出角色!

【问题讨论】:

  • 您是否尝试过记录name 的值、msg.guild.roles 中的角色以及role 的值?这些值显示了什么,你的代码当前做了什么,你得到了什么错误?

标签: discord discord.js


【解决方案1】:

使用.find()的函数版本是个好主意,但你的错误在于你使用.startsWith()的方式:它不是属性,而是方法。

const role = msg.guild.roles.find(r => r.name.toLowerCase().startsWith(args.split(/ +/g).slice(1).join(' ').toLowerCase()));

【讨论】:

  • TypeError: args.split(...).slice(...).join(...).toLowerCase(...).first is not a function 它不起作用@federico
  • 我用更简洁的方式更新了代码,但仍然没有任何想法??
  • 抱歉,我已经更新了我的答案,应该没有first(顺便说一句,这是来自您的原始代码)
猜你喜欢
  • 1970-01-01
  • 2020-11-26
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 2020-07-24
  • 1970-01-01
  • 2021-08-05
相关资源
最近更新 更多