![](/img/trans.png)
[英]discord.js v13 temp voice channels - joinVoiceChannel() @discordjs/voice
[英]Discord bot 'connect' command with discordjs v13, @discordjs/voice & SlashCommandBuilder
我目前遇到以下問題:如果我嘗試在 Discord 中執行它,我的測試“連接”命令將不起作用。正常回復命令在當前版本中運行良好,但我不知道如何進行語音通道惡作劇。 這是我的 2 個文件的代碼:(如果需要,也可以發布deploy-commands.js ,與 discord.js 指南中的代碼相同)
編輯 #1:在GentleAutumnRain的幫助下,我可以解決第一個錯誤,我相應地更改了所有代碼片段。
編輯 #2:已修復,機器人現在正在加入 VC
connect.js v3 //測試命令
const { SlashCommandBuilder } = require('@discordjs/builders');
const voiceDiscord = require('@discordjs/voice'); //3.change[GentleAutumnRain]
module.exports = {
data: new SlashCommandBuilder()
.setName('connect')
.setDescription('The bot will connect to your voice channel!'),
execute: async (interaction, client) => { //1.change[GentleAutumnRain]
const channel = interaction.member.voice.channel;
//Error case handling
if(!channel) return interaction.channel.send('Please join a Voice Channel first!');
const player = voiceDiscord.createAudioPlayer();
const resource = voiceDiscord.createAudioResource('');
const connection = voiceDiscord.joinVoiceChannel({
channelId: channel.id,
guildId: interaction.guild.id,
adapterCreator: interaction.guild.voiceAdapterCreator,
});
player.play(resource);
connection.subscribe(player);
// checking for ending, leaving VC if true
/*player.on(voiceDiscord.AudioPlayerStatus.Idle, () => {
connection.destroy();
});*/
},
};
index.js v2 //主要
// Require the necessary discord.js classes
const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES] }); //2.change[GentleAutumnRain]
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
}
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
client.user.setActivity(" YOU!", {type: "WATCHING"});
});
// Main - waiting for interaction
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
// Return error if a false command was typed
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
// Login to Discord with your client's token - MUST be always at the end of this script!
client.login(token);
終端 OUTPUT如果執行“connect”:
已經嘗試將“用戶”替換為“成員”,但結果是一樣的。
新的:
PS C:\Users\User\Documents\GitHub\DiscordBot_REPO> node .\index.js
Ready!
TypeError: Cannot read properties of undefined (reading 'createAudioPlayer')
at Object.execute (C:\Users\User\Documents\GitHub\DiscordBot_REPO\commands\connect.js:13:41)
at Client.<anonymous> (C:\Users\User\Documents\GitHub\DiscordBot_REPO\index.js:34:17)
at Client.emit (node:events:390:28)
at InteractionCreateAction.handle (C:\Users\User\Documents\GitHub\DiscordBot_REPO\node_modules\discord.js\src\client\actions\InteractionCreate.js:66:12)
at Object.module.exports [as INTERACTION_CREATE] (C:\Users\User\Documents\GitHub\DiscordBot_REPO\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (C:\Users\User\Documents\GitHub\DiscordBot_REPO\node_modules\discord.js\src\client\websocket\WebSocketManager.js:350:31) at WebSocketShard.onPacket (C:\Users\User\Documents\GitHub\DiscordBot_REPO\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)
at WebSocketShard.onMessage (C:\Users\User\Documents\GitHub\DiscordBot_REPO\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10)
at WebSocket.onMessage (C:\Users\User\Documents\GitHub\DiscordBot_REPO\node_modules\ws\lib\event-target.js:199:18)
at WebSocket.emit (node:events:390:28)
這是我在這個網站上的第一個問題,所以如果有人批評這個問題是如何寫的,請告訴我。 無論如何,如果有人能在這種情況下幫助我,我將不勝感激。
您將其更改為interaction.member.voice.channel
是正確的,這就是它的工作方式,但您必須為connect
命令正確提供interaction
。
現在,正如您在這一行中看到的那樣, execute: async (client, interaction) => {
在您的connect.js
中,您要求提供兩個參數,但是當您嘗試在此行上執行此 function 時, await command.execute(interaction);
你只提供一個。 在 javascript 中,這很好,代碼仍然有效,但這是一種先到先得的交易。 您提供的交互參數將被提供給您在execute
命令中需要的第一個參數,或者更簡單地說:您作為參數提供的interaction
被存儲在您執行 function 的client
變量中。
您只需在execute
命令中切換client
和interaction
參數即可解決此問題,從而使您的interaction
進入正確的變量。
此外,為了連接到語音通道並執行大量相關工作,您需要為您的客戶端提供GUILD_VOICE_STATES
意圖。 只需在index.js
文件中的客戶端意圖數組中添加Intents.FLAGS.GUILD_VOICE_STATES
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.