简体   繁体   中英

Discord.js transfering multiple users from a voice channel to another

For example, we have two voice channels named A and B and channel A has 4 users. I want those 4 users to go channel B, but I have some issues how can I point them (I mean let things).

Example command usage: +vmove channelid channelid

let args1 = args[1];
let args2 = args[2];
let oldChannel = message.guild.channelMembers.get(args1);
let newChannel = message.guild.channels.get(args2);
let kullanicilar = message.guild.channels.get(args1).members.forEach(member);

if (typeof args[0] == "undefined");
return;
message.channel.send("Please provide a channelname.");
if (!oldChannel)
  return message.channel
    .send({ embed: { description: `Kanal Belirtmedin` } })
    .then((msg) => msg.delete(10000));
if (!newChannel)
  return message.channel
    .send({ embed: { description: `Nereye taşınacağını belirtmedin` } })
    .then((msg) => msg.delete(10000));
if (!kullanicilar)
  return message.channel
    .send({ embed: { description: `Sesli odada kimse bulunmuyor.` } })
    .then((msg) => msg.delete(10000));
kullanicilar
  .setVoiceChannel(`${newChannel}`)
  .then(() =>
    message.channel.send(`${kullanicilar} <#${newChannel}> adlı kanala taşındı`)
  )
  .catch(console.error);

The main problem with this command is that there's no easy to mention a voice channel. The ideal way would be to mention by its ID, but I don't think that looks good even.

It seems like you're on v11 of Discord.js, so I'd highly recommend you upgrading to v12 if you feel like it. You'll certainly find better support.

So I will try to cover both versions. v12 intended for other people who see the thread.

The problem with your code was that you first tried to map using a forEach loop using a wrong syntax. But if even if it was correct, it returns nothing. So you would have to use .map(member => member) instead. And then later iterate through the generated array to setVoiceChannel. Though this is not necessary as you can use oldChannel.members.forEach() directly.

Also, I don't understand why on line 3 you try to get the channel by 'channelMembers' inside the GuildChannel object, line 4 approach is correct.

v11

You should correct then the line 3 mistake.

// change this
let oldChannel = message.guild.channelMembers.get(args1);
let newChannel = message.guild.channels.get(args2);
// to this
let oldChannel = message.guild.channels.get(args1);
let newChannel = message.guild.channels.get(args2);

If you would like to get the channels by name instead, here's my version for it:

const originChannelName = args[1];
const destinationChannelName = args[2];

let originChannel;
let destinationChannel;

message.guild.channels.forEach(channel => {
  if(channel.name === originChannelName) originChannel = channel;
  if(channel.name === destinationChannelName) destinationChannel = channel;
});

// You can handle your exceptions here

(Notice that this will require that the name is exactly equal and the channel name can't have whitespace.)

Now that you've got both channels. You can iterate through the members of the origin channel and set their channels to destination one.

originChannel.members.forEach(member => {
  member.setVoiceChannel(destinationChannel);
});

The final command should look something like this:

// Variables Declarations
const originChannelName = args[0];
const destinationChannelName = args[1];

let originChannel;
let destinationChannel;

message.guild.channels.forEach(channel => {
  if(channel.name === originChannelName) originChannel = channel;
  if(channel.name === destinationChannelName) destinationChannel = channel;
});

// Exceptions
if(originChannel === undefined) {
  return message.channel.send({
    embed: {
      description: 'Origin channel not found!'
    }
  }).then(message => message.delete(10000));
}
if(destinationChannel === undefined) {
  return message.channel.send({
    embed: {
      description: 'Destination channel not found!'
    }
  }).then(message => message.delete(10000));
}
if(originChannel.members.size < 1) {
  return message.channel.send({
    embed: {
      description: 'There is no member in the origin channel!'
    }
  }).then(message => message.delete(10000));
}

// Set voice channel
originChannel.members.forEach(member => {
  member.setVoiceChannel(destinationChannel);
});

// Give some feedback
message.channel.send({
  embed: {
    description: `${originChannel.members.size} people moved from ${originChannel.toString()} to ${destinationChannel.toString()}!`
  }
}).then(message => message.delete(10000));

v12

If your Discord.js is v12, things change a little. 'message.guild.channels' is now a GuildChannelManager and the channels are inside its cache, so there is no .get() method in it.

The .get() is now in its cache (which is a Collection). So considering args[1] and args[2] are channels id, just adding the '.cache.' on line 3 and 4 should do trick.

// changes this
let oldChannel = message.guild.channels.get(args[1]);
let newChannel = message.guild.channels.get(args[2]);
// to this
let oldChannel = message.guild.channels.cache.get(args1);
let newChannel = message.guild.channels.cache.get(args2);

And VoiceState is now a separated object, so instead of calling .setVoiceChannel() directly from the member, now, you would have to set the voice state of the member though his voice state object.

originChannel.members.forEach(member => {
  member.voice.setChannel(destinationChannel);
});

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