简体   繁体   English

当机器人在 discord.js v13 中准备好时加入特定的语音频道

[英]Join an specific voice channel when the bot is ready in discord.js v13

I found some articles about this question.我找到了一些关于这个问题的文章。 like this .这样

But it was in discord v12.但它在不和谐 v12 中。 I want discord.js v13我想要 discord.js v13

My code:我的代码:

client.on("ready", async() => {
const { joinVoiceChannel } = require('@discordjs/voice');
joinVoiceChannel({
            channelId: "863783336860975114",
            guildId: "847071265100791849",
            adapterCreator: channelId.guild.voiceAdapterCreator
        })

// It does not work
}

How can I make my bot that it joins a specific channel when it's ready?如何让我的机器人在准备好后加入特定频道?

I am using discord.js v13 and node.js v16.15我正在使用 discord.js v13 和 node.js v16.15

In the documentation for @discordjs/voice an in the discord.js guide we see:在 discord.js 指南中的@discordjs/voice文档中,我们看到:

const { joinVoiceChannel } = require('@discordjs/voice');

const connection = joinVoiceChannel({
    channelId: channel.id,
    guildId: channel.guild.id,
    adapterCreator: channel.guild.voiceAdapterCreator,
});

You need to fetch the guild or channel first since you need to use a voiceAdapterCreator .您需要先获取公会或频道,因为您需要使用voiceAdapterCreator

You can use something like client .您可以使用类似client的东西。 channels . 渠道 fetch to get the channel object. fetch获取通道对象。

Example code to put inside ready event:放入就绪事件的示例代码:

client.channels.fetch(id) // voice channel's id
    .then((channel) => { // channel object

        const VoiceConnection = joinVoiceChannel({
            channelId: channel.id, // the voice channel's id
            guildId: channel.guild.id, // the guild that the channel is in
            adapterCreator: channel.guild.voiceAdapterCreator // and setting the voice adapter creator
        });
    });

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

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