Below you'll find my code, and then the error message I'm getting. I'm not sure why I'm getting this error or how to resolve the issue.
const Discord = require("discord.js");
const client = new Discord.Client();
const DisTube = require("distube");
const distube = new DisTube(client, { searchSongs: false, emitNewSongOnly: true });
const prefix = "!";
client.on("ready", () => {
console.log(`${client.user.tag} Está Online`);
});
client.on("message", async (message) => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift();
// Queue status template
const status = (queue) =>
`Volume: \`${queue.volume}%\` | Filter: \`${queue.filter || "Off"}\` | Loop: \`${queue.repeatMode ? (queue.repeatMode == 2 ? "All Queue" : "This Song") : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\``;
// DisTube event listeners, more in the documentation page
distube
.on("playSong", (message, queue, song) => message.channel.send(`Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}\n${status(queue)}`))
.on("addSong", (message, queue, song) => message.channel.send(`Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}`))
.on("playList", (message, queue, playlist, song) =>
message.channel.send(`Play \`${playlist.name}\` playlist (${playlist.songs.length} songs).\nRequested by: ${song.user}\nNow playing \`${song.name}\` - \`${song.formattedDuration}\`\n${status(queue)}`)
)
.on("addList", (message, queue, playlist) => message.channel.send(`Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to queue\n${status(queue)}`))
// DisTubeOptions.searchSongs = true
.on("searchResult", (message, result) => {
let i = 0;
message.channel.send(`**Choose an option from below**\n${result.map((song) => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}\n*Enter anything else or wait 60 seconds to cancel*`);
})
// DisTubeOptions.searchSongs = true
.on("searchCancel", (message) => message.channel.send(`Searching canceled`))
.on("error", (message, e) => {
console.error(e);
message.channel.send("An error encountered: " + e);
});
if ((command = "play")) {
if (!args[0]) return message.channel.send("Voce Precisa Falar Algo Para Eu Tocar");
if (!message.member.voice.channel) return message.channel.send("Entre Em Um Canal De Voz Primeiro");
distube.play(message, args.join(" "));
}
if ((command = "pause")) {
const bot = message.guild.members.cache.get(client.user.id);
if (!message.member.voice.channel) return message.channel.send("Entre Em Um Canal De Voz Primeiro");
if (bot.voice.channel !== message.member.voice.channel) return message.channel.send("Você Não Está No Mesmo Canal De Voz!");
distube.stop(message);
message.channel.send("Você Parou A Musica");
}
});
client.login(process.env.TOKEN);
The result is the following error message:
**(node:4976) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:4976) UnhandledPromiseRejectionWarning: TypeError: Assignment to constant variable.
(node:4976) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)**
Can someone explain to me:
As Skulaurun said in the comments, you need to use ==
or ===
for comparison instead of =
, which is the assignment operator.
The problem in your code is in these lines:
if ((command = "play")) { ... }
Since you're only using one =
, node interprets this as you wanting to assign the value of "play"
to command
, which is a constant and cannot be changed, hence the error. You want to be using ==
or ===
for comparison, like:
if (command === "play") { ... }
Same goes for if ((command = "pause"))
, which has the same issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.