所以我是 js 的新手,不久前发布了一个问题,我得到了答案,但我还有另一个问题。我正在制作一个用于不和谐的音乐机器人,一切都很好,除了机器人无法识别我在语音频道中,并且无论我是否在其中都输出相同的消息。锯在一个线程而回即message.member.voiceChannel;
不工作,而应使用message.member.voice.channel.
,但这只给我一个错误消息̶(̶U̶n̶h̶a̶n̶d̶l̶e̶d̶P̶r̶o̶m̶i̶s̶e̶R̶e̶j̶e̶c̶t̶i̶o̶n̶W̶a̶r̶n̶i̶n̶g̶:̶未处理的承诺̶r̶e̶j̶e̶c̶t̶i̶o̶n̶.̶)̶>
以下是全文供参考:
编辑:好的,所以更正。我更正了@Zsolt Meszaros 添加的内容,但这次我没有收到错误消息,但问题仍然存在。不知道我上次做了什么,但不管怎样,不幸的是它仍然不起作用。
const Discord = require('discord.js');
const {
prefix,
token,
} = require('./config.json');
const ytdl = require('ytdl-core');
const client = new Discord.Client();
const queue = new Map();
client.once('ready', () => {
console.log('Ready!');
});
client.once('reconnecting', () => {
console.log('Reconnecting!');
});
client.once('disconnect', () => {
console.log('¨Disconnect!');
});
client.on('message', async message => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const serverQueue = queue.get(message.guild.id);
if (message.content.startsWith(`${prefix}play`)) {
execute(message, serverQueue);
return;
} else if (message.content.startsWith(`${prefix}play`)) {
skip(message, serverQueue);
} else if (message.content.startsWith(`${prefix}stop`)) {
stop(message, serverQueue);
return;
}
});
async function execute(message, serverQueue) {
const args = message.content.split(" ");
const voiceChannel = message.member.voiceChannel;
if (!voiceChannel)
return message.channel.send(
"Hur fan ska jag kunna spela musik om du inte är i en kanal? Jävla mongo."
);
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
return message.channel.send(
"Ok, så du vill att jag spelar upp musik, men ger mig inte tillåtelse att spela upp musik? Fixa dina perms dumfan."
);
}
let song;
if (ytdl.validateURL(args[1])) {
const songInfo = await ytdl.getInfo(args[1]);
song = {
title: songInfo.title,
url: songInfo.video_url
};
} else {
const {videos} = await yts(args.slice(1).join(" "));
if (!videos.length) return message.channel.send("No songs were found!");
song = {
title: videos[0].title,
url: videos[0].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;
play(message.guild, 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}? Ok, lägger till den i kön.`);
}
}
function skip(message, serverQueue) {
if (!message.member.voice.Channel)
return message.channel.send(
"Du måste var i en röstkanal för att stoppa musik din dumma jävel."
);
if (!serverQueue)
return message.channel.send("Det finns ingen låt att skippa dtt sär.");
serverQueue.connection.dispatcher.end();
}
function stop(message, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send(
"Du måste var i en röstkanal för att stoppa musik din dumma jävel."
);
if (!serverQueue)
return message.channel.send("Det finns ingen låt att stoppa dtt sär.");
serverQueue.songs = [];
serverQueue.connection.dispatcher.end();
}
function play(guild, song) {
const serverQueue = queue.get(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();
play(guild, serverQueue.songs[0]);
})
.on("error", error => console.error(error));
dispatcher.setVolume.Logarithmic(serverQueue.volume / 5);
serverQueue.textChannel.send(`Nu spelar jag: **${song.title}** din hora`);
}
client.login(token);