[英]cannot read properties of undefined (reading 'voice')
I am trying to make https://coderadio.freecodecamp.org/ play in a discord voice channel.我正在尝试让https://coderadio.freecodecamp.org/在不和谐的语音频道中播放。 I want to add a command called /coderadio that calls the bot into the current voice channel and streams the current song on the radio.
我想添加一个名为 /coderadio 的命令,它将机器人调用到当前语音频道并在收音机上播放当前歌曲。
Here is the repo I used originally - https://github.com/TannerGabriel/discord-bot这是我最初使用的回购 - https://github.com/TannerGabriel/discord-bot
I have added a new file to the commands folder, play-coderadio.js.我在命令文件夹中添加了一个新文件 play-coderadio.js。 I am hoping to have one slash command to play the radio and another to stop it.
我希望有一个斜杠命令来播放收音机,另一个来停止它。
index.js - index.js -
const fs = require('fs');
const Discord = require('discord.js');
const Client = require('./client/Client');
const config = require('./config.json');
const {Player} = require('discord-player');
const client = new Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
console.log(client.commands);
const player = new Player(client);
player.on('error', (queue, error) => {
console.log(`[${queue.guild.name}] Error emitted from the queue: ${error.message}`);
});
player.on('connectionError', (queue, error) => {
console.log(`[${queue.guild.name}] Error emitted from the connection: ${error.message}`);
});
player.on('trackStart', (queue, track) => {
queue.metadata.send(`▶ | Started playing: **${track.title}** in **${queue.connection.channel.name}**!`);
});
player.on('trackAdd', (queue, track) => {
queue.metadata.send(`🎶 | Track **${track.title}** queued!`);
});
player.on('botDisconnect', queue => {
queue.metadata.send('❌ | I was manually disconnected from the voice channel, clearing queue!');
});
player.on('channelEmpty', queue => {
queue.metadata.send('❌ | Nobody is in the voice channel, leaving...');
});
player.on('queueEnd', queue => {
queue.metadata.send('✅ | Queue finished!');
});
client.once('ready', async () => {
console.log('Ready!');
});
client.on('ready', function() {
client.user.setActivity(config.activity, { type: config.activityType });
});
client.once('reconnecting', () => {
console.log('Reconnecting!');
});
client.once('disconnect', () => {
console.log('Disconnect!');
});
client.on('messageCreate', async message => {
if (message.author.bot || !message.guild) return;
if (!client.application?.owner) await client.application?.fetch();
if (message.content === '!deploy' && message.author.id === client.application?.owner?.id) {
await message.guild.commands
.set(client.commands)
.then(() => {
message.reply('Deployed!');
})
.catch(err => {
message.reply('Could not deploy commands! Make sure the bot has the application.commands permission!');
console.error(err);
});
}
});
client.on('interactionCreate', async interaction => {
const command = client.commands.get(interaction.commandName.toLowerCase());
try {
if (interaction.commandName == 'ban' || interaction.commandName == 'userinfo') {
command.execute(interaction, client);
} else {
command.execute(interaction, player);
}
} catch (error) {
console.error(error);
interaction.followUp({
content: 'There was an error trying to execute that command!',
});
}
});
client.login(config.token);
play-coderadio.js -播放-coderadio.js -
const Discord = require('discord.js')
const { MessageEmbed } = require('discord.js')
module.exports = {
name: 'coderadio',
description: 'play FCC radio',
async execute(client, message, args) {
const voiceChannel = message.member.voice.channel;
if(!voiceChannel)
return message.channel.send("You must be in a voice channel!");
const permissions = voiceChannel.permissionsFor(message.client.user);
if(!permissions.has('CONNECT') || !permissions.has('SPEAK')) return message.reply('I am missing permission!');
var connection = await voiceChannel.join();
await connection.voice.setSelfDeaf(true);
connection.play("https://coderadio-relay-blr.freecodecamp.org/radio/8010/radio.mp3").setVolumeLogarithmic(1);
const embed = new MessageEmbed()
.setTitle(`Radio Now Playing!`)
.setDescription(`Now Playing radio!\n\nPlaying into ${voiceChannel.name}`)
message.channel.send(embed)
}
}
Here is the error I am getting when I run /coderadio -这是我在运行 /coderadio 时遇到的错误 -
C:\Users\Rudy\musicbot\discord-bot\commands\play-coderadio.js:9 const voiceChannel = message.member.voice.channel; C:\Users\Rudy\musicbot\discord-bot\commands\play-coderadio.js:9 const voiceChannel = message.member.voice.channel; ^ TypeError: Cannot read properties of undefined (reading 'voice') at Object.execute (C:\Users--\musicbot\discord-bot\commands\play-coderadio.js:9:41) at module.exports.
^ 类型错误:无法在 module.exports 的 Object.execute (C:\Users--\musicbot\discord-bot\commands\play-coderadio.js:9:41) 处读取未定义的属性(读取“语音”)。 (C:\Users--\musicbot\discord-bot\index.js:89:15) at module.exports.emit (node:events:527:28) at InteractionCreateAction.handle (C:\Users--\musicbot\discord-bot\node_modules\discord.js\src\client\actions\InteractionCreate.js:74:12) at module.exports [as INTERACTION_CREATE] (C:\Users--\musicbot\discord-bot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36) at WebSocketManager.handlePacket (C:\Users--\musicbot\discord-bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31) at WebSocketShard.onPacket (C:\Users--\musicbot\discord-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22) at WebSocketShard.onMessage (C:\Users--\musicbot\discord-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10) at WebSocket.onMessage (C:\Users--\musicbot\discord-bot\node_modules\ws\lib\event-target.js:199:18) at WebSocket.emit (node:events:527:28)
(C:\Users--\musicbot\discord-bot\index.js:89:15) 在模块.exports.emit (node:events:527:28) 在 InteractionCreateAction.handle (C:\Users--\musicbot \discord-bot\node_modules\discord.js\src\client\actions\InteractionCreate.js:74:12) 在 module.exports [as INTERACTION_CREATE] (C:\Users--\musicbot\discord-bot\node_modules\discord .js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36) 在 WebSocketManager.handlePacket (C:\Users--\musicbot\discord-bot\node_modules\discord.js\src\client\websocket\WebSocketManager .js:351:31) 在 WebSocketShard.onPacket (C:\Users--\musicbot\discord-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22) 在 WebSocketShard.onMessage ( C:\Users--\musicbot\discord-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10) 在 WebSocket.onMessage (C:\Users--\musicbot\discord-bot \node_modules\ws\lib\event-target.js:199:18) 在 WebSocket.emit (node:events:527:28)
Try using optional chaining for your assignment to voiceChannel.尝试使用可选链接来分配到 voiceChannel。
Something like const voiceChannel = message?.member?.voice?.channel;
类似
const voiceChannel = message?.member?.voice?.channel;
In cases where there is no message, voiceChannel will render false
and you will send the error message you stated in your if statement在没有消息的情况下,voiceChannel 将呈现
false
并且您将发送您在 if 语句中声明的错误消息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.