您好,我一直有一个播放命令与不一致的问题。我不确定是什么问题,如果可能的话,请帮助我。我将诚实地说,我从其他地方得到了这个代码,但它是为一个私人服务器。请帮我修一下这个谢谢。无论如何,我已经尝试创建一个播放,停止,跳过,现在播放命令很久了,如果它工作,它将是令人惊叹的谢谢。
控制台错误:
TypeError: Cannot read property 'get' of undefined
代码:
const ytdl = require("ytdl-core");
module.exports = {
name: "play",
description: "Play a song in your channel!",
async execute(message) {
try {
const args = message.content.split(" ");
const queue = message.client.queue;
const serverQueue = message.client.queue.get(message.guild.id);
const voiceChannel = message.member.voice.channel;
if (!voiceChannel)
return message.channel.send(
"You need to be in a voice channel to play music!"
);
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
return message.channel.send(
"I need the permissions to join and speak in your voice channel!"
);
}
const songInfo = await ytdl.getInfo(args[1]);
const song = {
title: songInfo.videoDetails.title,
url: songInfo.videoDetails.video_url
};
if (!serverQueue) {
const queueContruct = {
textChannel: message.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: 5,
playing: true
};
queue.set(message.guild.id, queueContruct);
queueContruct.songs.push(song);
try {
var connection = await voiceChannel.join();
queueContruct.connection = connection;
this.play(message, queueContruct.songs[0]);
} catch (err) {
console.log(err);
queue.delete(message.guild.id);
return message.channel.send(err);
}
} else {
serverQueue.songs.push(song);
return message.channel.send(
`${song.title} has been added to the queue!`
);
}
} catch (error) {
console.log(error);
message.channel.send(error.message);
}
},
play(message, song) {
const queue = message.client.queue;
const guild = message.guild;
const serverQueue = queue.get(message.guild.id);
if (!song) {
serverQueue.voiceChannel.leave();
queue.delete(guild.id);
return;
}
const dispatcher = serverQueue.connection
.play(ytdl(song.url))
.on("finish", () => {
serverQueue.songs.shift();
this.play(message, serverQueue.songs[0]);
})
.on("error", error => console.error(error));
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
serverQueue.textChannel.send(`Start playing: **${song.title}**`);
}
};
发布于 2021-04-29 08:51:46
你应该看看你是从哪里得到代码的,也许你漏掉了一个部分?特别是在他们定义client.queue
的地方,大多数人通常这样定义它
client.queue = new Map()
或discord.js collection class (从map扩展)
在主机器人文件(index.js、bot.js等)中。
您可以检查代码的来源,或者尝试将此代码添加到主bot文件中定义客户端的位置
https://stackoverflow.com/questions/67297673
复制相似问题