简体   繁体   中英

How do you shift an arg twice in Discord.js

So I need to shift args twice in my code. Here's what I have so far:

const Discord = require('discord.js')
const bot = new Discord.Client()
const token = token here;
const PREFIX = '/';
const embed = new Discord.MessageEmbed()
const ping = require('minecraft-server-util')
bot.on('ready', () => {
  console.log('This bot is online! Created by @littleBitsman.');
})

bot.on('message', message => {
  let args = message.content.substring(PREFIX.length).split(' ')
  if(message.content.startsWith(PREFIX))
  switch (args[0]) {
    case 'ticket':
      if (message.member.roles.highest == '701895573737046066') {
        var usertosendto = args[1]
        var thing = args.shift().shift()
        var embed = new Discord.MessageEmbed()
          .setTitle('Ticket')
          .setDescription('Hey ' + usertosendto + '! You recieved this ticket because of: ' + thing + '.')
          message.channel.send(embed)

      }
  }
})
bot.login(token);

When I do args.shift().shift() , it says "TypeError: args.shift(...).shift is not a function". What should I do? (I have taken out a large chunk of code here)

Array.prototype.shift() is an Array method.

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

So, you end up shifting the first element off the array. And that is RETURNED from shift(). You then want to again call shift on the ARRAY, but you are calling it on the returned element (first element in the array).

var thing = args.shift(); // "thing" is now what args[0] was originally ('ticket' in your example).
// There is no shift() method on a String, so you cannot chain another shift here
// but you can again reference args to shift from.
thing = args.shift(); // "thing" is now what args[1] was originally

Here thing will be the 2nd item in the array and the args array will be 2 elements shorter.

If you do not want to shorten the array, you can also just do:

var thing = args[1]; // Note: You have defined args[1] as `usertosendto`, you may be actually trying to work with args[2], the 3rd element in the arguments list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM