Discord.js Move members to different voice channel

点点圈 提交于 2021-01-28 08:19:56

问题


So, I'm trying to make a command that, when said, provided with a mention and the name of a Voice Channel, moves the member you mentioned to that Voice Channel. I've done a bit of research on this but still can't get it work.

const Discord = require('discord.js');
const client = new Discord.Client();
const config = require('../config.json');

exports.run = function(client, message) {

const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();      

if (command === 'move') {
  const mem = message.mentions.members.first()
  const vc = args.join(" ")
  const chan = client.channels.find("name", vc)
    mem.setVoiceChannel(chan)
      .then(() => console.log(`Moved ${mem.displayName} to ${chan}`))
  .catch(console.error);
  }
}

Right now, when I try the command, using, let's just say, the Voice Channel "Music" (so, "d!move [mention] Music"), it returns "Moved [member] to null" to the console, and of course, doesn't move the member. It seems to find the member I mentioned just fine, but can't find the Voice Channel. Any help would be appreciated. Thanks.


回答1:


I might have the answer, so you are using d!move [mention] Music to move the user. After that you split into different arguments to handle it better, to something like this: ["move", "<@someID>", "Music"]. Then you remove the first one that its the command with slice, and you get ["<@someID>", "Music"].
Then when you wanna find the channel name you join the array with spaces to get the channel name. Problem is you still have the mention there. So it will stay <@someID> Music, and it is trying to find a channel with that name.
You probably need to remove the first argument of the array the same way you did with the command, and then join it, or make a new array with only the name of the channel.



来源:https://stackoverflow.com/questions/50052018/discord-js-move-members-to-different-voice-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!