K
K
Kirill Dremin2021-06-27 08:52:24
Node.js
Kirill Dremin, 2021-06-27 08:52:24

Node index.js error?

I'm making a discord bot for music, I write a command to run it, it gives an error
PS C:\Users\111\Desktop\PMus♪> node index.js
internal/modules/cjs/loader.js:905
throw err;
^

Error: Cannot find module 'discord.js'
Require stack:
- C:\Users\111\Desktop\PMus♪\index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15 )
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/ helpers.js:92:18)
at Object. (C:\Users\111\Desktop\PMus♪\index.js:1:17)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load( internal/modules/cjs/loader.js:790:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'C:\\ Users\\111\\Desktop\\PMus♪\\index.js' ]
}

code

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}skip`)) {
skip( message, serverQueue);
return;
} else if (message.content.startsWith(`${prefix}stop`)) {
stop(message, serverQueue);
return;
} else {
message.channel.send('You need to enter a valid command!')
}
});

async function execute(message, serverQueue) {
const args = message.content.split(' ');

const voiceChannel = message.member.voiceChannel;
if (!voiceChannel) return message.channel.send('You need to be in a voice channel to play music!');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) {
return message.channel.send('I need the permissions to join and speak in your voice channel!');
}

const songInfo = await ytdl.getInfo(args[1]);
title: songInfo.title,
url: songInfo.video_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);
console.log(serverQueue.songs);
return message.channel.send(`${song.title} has been added to the queue!`);
}

}

function skip(message, serverQueue) {
if (!message.member.voiceChannel) return message.channel.send('You have to be in a voice channel to stop the music!');
if (!serverQueue) return message.channel.send('There is no song that I could skip!');
serverQueue.connection.dispatcher.end();
}

function stop(message, serverQueue) {
if (!message.member.voiceChannel) return message.channel.send('You have to be in a voice channel to stop the music!');
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.playStream(ytdl(song.url))
.on('end', () => {
console.log('Music ended!');
serverQueue.songs.shift();
play( guild, serverQueue.songs[0]);
})
.on('error', error => {
console.error(error);
});
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question