Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

javascript - arguments.shift() is not a function, Discord.js

I am trying to make a discord bot for someone and there is this command that I get stuck on, the giverole one. Particularly, the arguments.shift part of it.

Here is my code:

client.on('message', message => {
  if (message.content.startsWith(`${prefix}giverole`)) {
    if (!message.member.hasPermission("ADMINISTRATOR")) {
      message.channel.send("Vous n'avez pas la permission d'utiliser cette commande.")
      return;
    }
    const targetUser = message.mentions.users.first()
    if (!targetUser) {
      message.reply('Please specify someone to give a role to.')
      return
    }

    arguments.shift()

    const roleName = arguments.join(' ')
    const { guild } = message

    const role = guild.roles.cache.find((role) => {
      return role.name === roleName
    })
    if (!role) {
      message.reply(`There is no role with the name "${roleName}"`)
      return
    }

    const member = guild.members.cache.get(targetUser.id)
    member.roles.add(role)

    message.reply(`that user now has the "${roleName}" role`)
  }
}

And I receive this error:

TypeError: arguments.shift is not a function

The error may be that arguments isn't passed anywhere, yet I don't know where to place it.

question from:https://stackoverflow.com/questions/66059581/arguments-shift-is-not-a-function-discord-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems you've never defined arguments (which wouldn't be a wise choice for a variable name anyway, as there is an arguments object in normal functions).

Instead of getting custom arguments from the message and searching the role by its name, you could mention the role(s) and get them by using message.mentions.roles. This way you don't have to chop and clean up the message.content either.

client.on('message', async (message) => {
  if (message.content.startsWith(`${prefix}giverole`)) {
    if (!message.member.hasPermission('ADMINISTRATOR')) {
      return message.channel.send(
        "Vous n'avez pas la permission d'utiliser cette commande.",
      );
    }

    const user = message.mentions.users.first();
    const member = message.guild.member(user);
    const role = message.mentions.roles.first();

    if (!member) {
      return message.reply('Please mention someone to give a role to.');
    }

    if (!role) {
      return message.reply(`Please mention a role to add to ${member}.`);
    }

    try {
      await member.roles.add(role);
      message.reply(`${member} now has the "${role}" role`);
    } catch (error) {
      console.log(error);
      message.reply(`Role "${role}" is not added to ${member}.
_${error}_`);
    }
  }
});

enter image description here

PS: Make sure to drag your bot's role above all other roles in your server settings and give it the Manage Roles permission to avoid DiscordAPIError: Missing Permissions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...