node.js - TypeError:无法读取未定义的属性'get',如何?
问题描述
run: async (client, message, args) => {
const { channel } = message.member.voice;
const permissions = channel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT"))
return message.reply("Je n'est pas les permissions de rejoindre votre channel.");
if (!permissions.has("SPEAK"))
return message.reply("Je ne peux pas parler dans ce channel vocal.");
const search = args.join(" ");
const videoPattern = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/gi;
const playlistPattern = /^.*(list=)([^#\&\?]*).*/gi;
const url = args[0];
const urlValid = videoPattern.test(args[0]);
const serverQueue = message.client.queue.get(message.guild.id);
^^^^^ÊRROR HERE^^^^^^
const queueConstruct = {
textChannel: message.channel,
channel,
connection: null,
songs: [],
loop: false,
volume: 50,
playing: true
};
let songInfo = null;
let song = null;
if (urlValid) {
try {
songInfo = await ytdl.getInfo(url);
song = {
title: songInfo.title,
url: songInfo.video_url,
duration: songInfo.length_seconds,
};
} catch (error) {
if (error.message.includes("copyright")) {
return message
.reply("⛔ Cette vidéo est copyright, elle ne peut donc être jouée. ⛔")
.catch(console.error);
} else {
console.error(error);
}
}
} else {
try {
const results = await youtube.searchVideos(search, 1);
songInfo = await ytdl.getInfo(results[0].url);
song = {
title: songInfo.title,
url: songInfo.video_url,
duration: songInfo.length_seconds,
};
} catch (error) {
console.error(error);
}
}
if (serverQueue) {
serverQueue.songs.push(song);
return serverQueue.textChannel
.send(`✅ **${song.title}** a été ajoutée a la queue par ${message.author}`)
.catch(console.error);
} else {
queueConstruct.songs.push(song);
}
if (!serverQueue) message.client.queue.set(message.guild.id, queueConstruct);
当代码正常启动时,它应该播放一些音乐,但是当我运行代码时它没有按预期运行,它告诉我 get 没有定义......你能帮帮我吗?
我不明白为什么在它起作用之前就知道它为什么会这样做......如果你帮助我,它将非常有用!没有所有的代码,因为否则网站会问我更多的细节对不起......
解决方案
每当您想访问对象中不存在的属性时,就会出现此错误。你确定这个queue
属性的存在message.client
吗?根据discord.js doc,客户端对象中没有任何queue
可以通过消息对象访问的属性。为保证记录,message.client
以通过该对象查找可用属性。
推荐阅读
- variables - 使用字符串引用 Fortran 变量
- c# - 如何在Classic ASP.NET中实现UserControl的RenderPartial并将序列化结果发送到浏览器
- r - 循环遍历 xml 列表
- c# - microsoft Graph API 中使用“无需用户访问”方法时如何获取登录用户的用户个人资料详细信息
- kubernetes - 从 Rsyslog 发送到 Kafka 代理的消息在 Kubernetes 中被剪辑
- node.js - sqlite3 中的 BLOB 字段未更新
- swift - iOS 15 SwiftUI 键盘工具栏随机行为
- github - 从另一个仓库获取更新而不在更改后进行任何 PR 的最佳做法是什么?
- c++ - GetDiskFreeSpaceExA 麻烦
- python - 获取 dpkg:错误:在 PATH 中未找到 2 个预期程序或无法从我构建的 .deb 执行