简体   繁体   English

(Discord.js) 从语音通道中随机选择一个用户

[英](Discord.js) Select a random user from voice channel

I'm trying to make a bot for voice applications for my server.我正在尝试为我的服务器制作一个用于语音应用程序的机器人。 I want it to have a command that picks a random user from a voice channel.我希望它有一个从语音通道中随机选择用户的命令。 I tried to do it in a few different ways, but none of them have worked, please help.我尝试以几种不同的方式来做到这一点,但都没有奏效,请帮忙。

(edit) Here is my not pretty code (编辑)这是我不漂亮的代码

else if (args[0].toLowerCase() === 'pick') {
    const GuildMember = message.guild.members;

    const channels = message.guild.channels.filter(c => c.ID === config.voiceChannel && c.type === 'voice');

    const toCheck = Discord.Collection.array(channels.members);

    message.reply(message.author + ', now pick ' + toCheck.random());
}
message.delete().catch(O_o=>{});

(another edit) This is an error i'm getting "TypeError: Discord.Collection.array is not a function" (另一个编辑)这是一个错误,我收到“TypeError:Discord.Collection.array 不是函数”

Your error is telling you that you are not using Collecion.array() properly.您的错误告诉您没有正确使用 Collecion.array()。 It looks as though you are trying to use it as a constructor or factory method, but it's a converter function.看起来好像您试图将它用作构造函数或工厂方法,但它是一个转换器函数。 It needs an existing collection to work.它需要一个现有的集合才能工作。

Secondly, your filter method will return a collection.其次,您的过滤器方法将返回一个集合。 Since you are filtering by an ID, it should be a collection of 1, but still a collection.由于您是按 ID 过滤,因此它应该是 1 的集合,但仍然是一个集合。 Collections do not have a members property so channels.members is undefined.集合没有 members 属性,所以channels.members是未定义的。 You need to extract the first (and what should be only) channel from the collection.您需要从集合中提取第一个(应该是唯一的)频道。

Fortunately, all of this can be handled by replacing a single line.幸运的是,所有这些都可以通过替换一行来处理。 You don't even need to convert it to an array because VoiceChannel.members is also a collection, and Collection has a random function of it's own.您甚至不需要将其转换为数组,因为VoiceChannel.members也是一个集合,而Collection具有它自己的随机函数。

const toCheck = channels.first().members;

On another note, because you are filtering the channel by it's unique ID, filtering by c.type is redundant.另一方面,因为您是按唯一 ID 过滤通道,所以按 c.type 过滤是多余的。 It will always be a voice channel unless you misconfigured config.voiceChannel.除非您错误配置了 config.voiceChannel,否则它将始终是语音通道。

Update It also occured to me after posting that you could use this instead of your filter:更新发布后我也发现您可以使用它代替过滤器:

const channel = message.guild.channels.get(config.voiceChannel);

This returns the single channel, rather than a collection, by ID and is better because you aren't iterating over the collection and it simplifies the code.这将通过 ID 返回单个通道,而不是集合,并且更好,因为您没有迭代集合并且它简化了代码。 If you do this you don't need to use the changed line above at all, remove that line and use the following:如果您这样做,您根本不需要使用上面更改的行,删除该行并使用以下内容:

message.reply(message.author + ', now pick ' + channel.members.random()); 

Tarazed, thanks for your answer it helped me to figure it out. Tarazed,感谢您的回答,它帮助我弄清楚了。

For people that want to pick a random person from a voice channel too, here is the code that you can put in you command:对于也想从语音频道中随机挑选一个人的人,这里是您可以放入命令的代码:

const GuildMember = message.guild.members;

const channel = message.guild.channels.get('voiceChannelID'); //replace voiceChannelID with your voice Channel ID

let toCheck = channel.members;

message.reply('a random person: ' + toCheck.random());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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