loader.js:800 throw err: node.js - node.js

I am trying to make a discord bot for my server and everytime I try to start the bot it gives me the same error every time.
I haven't tried anything else because a google search does not find the error.
I was following a YouTube video that was made in 2017.
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
var prefix = config.prefix;
//Startup
client.login(config.token);
client.on("ready", () => {
console.log(`Online ${new Date()}`);
client.user.setGame("Online");
});
client.on("message", async message => {
//Ingore bots
if(message.author.bot) return;
//Prefix in command
if(message.content.indexOf(config.prefix) !==0) return;
const args = message.content.slice(config.prefix.legth).trim().split(/
+/g);
const command = args.shift().toLowerCase();
if (command === "ping") {
message.reply("Pong!");
}
});
I expected the bot to start fully but gave me the following error
"internal/modules/cjs/loader.js:800
throw err;
^
SyntaxError: C:\Users\adoss\Desktop\DiscordBot\config.json: Unexpected
string in JSON at position 37
at JSON.parse (<anonymous>)
at Object.Module._extensions..json
(internal/modules/cjs/loader.js:797:27)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (C:\Users\adoss\Desktop\DiscordBot\bot.js:3:16)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)"

Related

Error [TOKEN_INVALID], Turns valid token into invalid token

My Code
const Discord = require("discord.js")
require("dotenv").config()
const Token = "*MY VALID TOKEN*"
const client = new Discord.Client({
intents: [
"GUILDS",
"GUILD_MESSAGES",
"GUILD_MEMBERS"
]
})
client.on("ready", () =>{
console.log(`Logged in as ${client.user.tag}`)
})
client.on("messageCreate", (message) => {
if (message.content == "hi!"){
message.reply("Hello")
}
})
const welcomeChannelID = "935422032458444901"
client.on("guildMemberAdd", (member) =>{
member.guild.channels.cache.get(welcomeChannelID).send(`<#${member.id}> Welcome to the Server!`)
})
client.login(process.env.Token)
The Error
D:\DBS\node_modules\discord.js\src\client\websocket\WebSocketManager.js:129
const invalidToken = new Error(WSCodes[4004]);
^
Error [TOKEN_INVALID]: An invalid token was provided.
at WebSocketManager.connect (D:\DBS\node_modules\discord.js\src\client\websocket\WebSocketManager.js:129:26)
at Client.login (D:\DBS\node_modules\discord.js\src\client\Client.js:254:21)
at Object.<anonymous> (D:\DBS\index.js:29:8)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
[Symbol(code)]: 'TOKEN_INVALID'
}
Node.js v17.3.0
I have Checked and Double checked my token, and it IS valid, There is something wrong in my code, can you please help me figure it out? thanks!
.......................................................
If you are using client.login(process.env.Token), then you should have a .env file where you put your token inside. If you're putting your token at
const Token = "*MY VALID TOKEN*"
Then simply just do client.login(Token);
Your token should be in .env file following format:
Token=YourTokenIsHere
For example:
Token=cjKFmWWHeabkY5cjKFmWWHeabkY5cjKFmWW
Not real token in example
Make sure you have the correct token from the Developer Portal

An issue in making my bot online in discord

(Node version: 14.17.6)
When I type node . in terminal, it gives me this issue:
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
at Client._validateOptions (C:\Users\DELL\Desktop\Discord Bot\node_modules\discord.js\src\client\Client.js:544:13)
at new Client (C:\Users\DELL\Desktop\Discord Bot\node_modules\discord.js\src\client\Client.js:73:10)
at Object. (C:\Users\DELL\Desktop\Discord Bot\index.js:2:13)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
[Symbol(code)]: 'CLIENT_MISSING_INTENTS'
}
what I wrote in VSC:
const Discord = require('discord.js');
const bot = new Discord.Client();
const token ='MY TOKEN';
bot.on('ready', () =>{
console.log('This bot is online!');
})
bot.on("message", msg=>{
if (msg.content === "HELLO"){
msg.reply('HELLO FRIEND!');
}
})
bot.login(token);
Client doesn't have intents
These would be your main ones
let bot = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })

Error while saving syntax, and JSON.parse code

const Discord = require('discord.js');
const bot = new Discord.Client();
let cofig = require('./botconfig.json');
let token = config.token;
let prefix = config.prefix;
bot.on('ready', () => {
console.log(`Запустился бот ${bot.user.username}`);
});
bot.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('Pong!');
}
});
bot.login(token);`
My bot is supposed to answer by Pong! when I type the ping in a channel. It's a simple ping test to check if the bot is alive and behave correctly. However I have an error when I try to make it work.
Error:
SyntaxError: C:\Users\mrakp\OneDrive\Рабочийстол\mamapapads\botconfig.json: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.Module._extensions..json (internal/modules/cjs/loader.js:801:27)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:683:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object.<anonymous> (C:\Users\mrakp\OneDrive\Рабочий стол\mamapapads\bot.js:3:13)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
Well first off, you can't do
const cofig = require("./botconfig.json")
Then not use the declaration in order to get the token.
In your case, bot.login(token) would actually be bot.login(cofig.token)
But you can also do
const { token } = require("./botconfig.json")
//Rest of code
bot.login(token)
Which would get the token directly, instead of having to use a declaration in order to reference it.

Node app.js syntax error: unexpected token

I'm new to programming and I wanted to make a discord bot using Atom and Javascript. I had some previous programming experience, but not much. I tried to find help through other Q/A's with the same error code but I still can't figure out why my program doesn't work. I found how to write this online.
My program is:
const Discord = require("discord.js");
const Config = require("./config.json");
const Token = require("./token.json");
const bot = new Discord.Client({disableEveryone: true})
bot.on("ready", async () =>{
console.log(`${bot.user.username} is online! It's running on
${bot.guild.size} servers!`);
bot.user.setActivity("Work in Progress", {type: "WATCHING"}); })
bot.on("message", async message => {
if{message.author.bot} return;
if(message.channel.type === "dm") return;
message.channel.send("Swift bot testing...") })
bot.login(Token.token);
When I tried to run it with node on Windows Powershell by entering "node app.js", it returned with the error message:
SyntaxError: Unexpected token { at createScript (vm.js:80:10) at
Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10) at Module.load
(module.js:566:32) at Function.Module._load (module.js:498:3) at
Function.Module.runMain (module.js:694:10) at startup
(bootstrap_node.js:204:16) at bootstrap_node.js:625:3
I don't understand what is wrong! Somebody please help me get my program running.
You have a problem in if:
if{message.author.bot} return;
vs
if(message.author.bot) return;

Error in sending message from an Node.js app to Slack channel

Trying to send message to slack from Node.js app
I have used following package https://github.com/xoxco/node-slack
For Detailed reference full source code is in https://github.com/sudanvellakovilkanakavel/attendence-app
I couldn't understand
what is hook_url
How to define it
Error output
CJBTDLDD0003:backend kanaks$ node readingFile.js
/Users/kanaks/Desktop/backend/readingFile.js:2
var slack = new Slack(hook_url,options);
^
ReferenceError: hook_url is not defined
at Object.<anonymous> (/Users/kanaks/Desktop/backend/readingFile.js:2:23)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3
My Code
var Slack = require('node-slack');
var slack = new Slack(hook_url,options);
//var slack = new Slack(hook_url,{proxy: http_proxy});
//Trying to send message to slack from Node.js app
//I have used following package https://github.com/xoxco/node-slack
slack.send({
text: 'Meassage from node.js app to slack!',
channel: '#attendance',
username: 'Bot'
});
var fs = require("fs");
//reading the file
fs.readFile('Attendance01-04-2016.dat','utf8',function (err,data){
if (err) {
return console.error(err);
}
//spliting each line
var lines = data.split("\n");
//storeing in array and printing the file
for (i=0;i<=lines.length;)
{
console.log(lines[i]);
i=i+1;
}
});

Resources