-1

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:

  • TypeError: Cannot read properties of undefined (reading 'ALL')
  • SyntaxError: Identifier 'Discord' has already been declared
  • Valid intents must be provided for the Client.
1

1 Answer 1

0

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.

  1. Set "type" as "module" in "package.json" and use import <name> from <module> instead of const <name> = require("<module>")
  2. Replace import spotify from 'spotify-web-api-js'; with const spotify = require("spotify-web-api-js"), because "CommonJS" doesn't support "import"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.