Floppinqq
Floppinqq

Reputation: 1

say message Only send me the first word of a phrase, JavaScript - Discord.js

This is the code:

const args = msg.content.trim().split(/ +/);
const cmd = args.shift().toLowerCase();

if (cmd === 'dm') {
  (await client.users.fetch(args[0]))
  .send(args[1])
    .catch(() => {
      console.log('Error while sending message.')
    })
}

what I want: dm <user_id>

what I do: dm <my_id> test testing test 123 what I receive: test

can someone explain me how to fix that? the bot only send the first word of my message :/ any help is welcome!

Upvotes: 0

Views: 228

Answers (1)

deb
deb

Reputation: 7451

This should do the work.

const args = msg.content.trim().split(" ");
const cmd = args.shift().toLowerCase();

if (cmd === 'dm') {
  // Clone the args array in case you need it later
  const argsClone = args.slice()
  // Fetch user while removing their id from the list
  const user = await client.users.fetch(argsClone.shift())
  // Send the remaining of the list as a DM.
  user.send(argsClone.join(" "))
    .catch(() => {
      console.log('Error while sending message.')
    })
}

Upvotes: 1

Related Questions