I can't execute and debug my discord bot, having an error in debugging console.
This is my code in the bot:
const discord = require('discord.js');
const client = new discord.Client({
ws: { intents: discord.Intents.ALL },
});
import spotify from 'spotify-web-api-js';
const spotifyAPI = new spotify();
import { MessageEmbed } from 'discord.js';
//Spotify Credentials
spotifyAPI.setAccessToken('HIDDEN');
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', message => {
if (message.content.startsWith('!play')) {
let songName = message.content.split(" ").slice(1).join(" ");
spotifyAPI.searchTracks(songName)
.then(function(data) {
let song = data.tracks.items[0];
let songName =song.name;
let songUrl =song.preview_url;
let songAuthor =song.artists[0].name;
let songEmbed = new MessageEmbed()
.setTitle(`Playing ${songName}`)
.setURL(songUrl)
.setAuthor(songAuthor)
.setColor('#0099ff')
message.channel.send(songEmbed);
}, function(err) {
console.error(err);
message.channel.send("Sorry, I couldn't find the song you requested. Please try again with a different name or check your internet connection.");
});
}
});
client.login('HIDDEN');
And i have this error in my debugging console:
Uncaught SyntaxError C:\Users\MY_USERNAME\Proyectos de VS Code\spoticord\spoticord.js:6
import spotify from 'spotify-web-api-js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at compileFunction (vm:360:18)
at wrapSafe (internal/modules/cjs/loader:1088:15)
at Module._compile (internal/modules/cjs/loader:1123:27)
at Module._extensions..js (internal/modules/cjs/loader:1213:10)
at Module.load (internal/modules/cjs/loader:1037:32)
at Module._load (internal/modules/cjs/loader:878:12)
at executeUserEntryPoint (internal/modules/run_main:81:12)
at <anonymous> (internal/main/run_main_module:23:47)
Process exited with code 1
I tried update discord.js, spotify-web-api-js, reinstall both modules. But gives another errors like:
Your package's settings are for "CommonJS", when import
is allowed for "module" types.
// at line 6
import spotify from 'spotify-web-api-js'
// at line 9
// this must be replaced also, like at line 6
import { MessageEmbed } from 'discord.js';
There are two correct solutions.
import <name> from <module>
instead of const <name> = require("<module>")
import spotify from 'spotify-web-api-js';
with const spotify = require("spotify-web-api-js")
, because "CommonJS" doesn't support "import"