Node app.js syntax error: unexpected token - bots

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;

Related

My code is giving an error- Error [TOKEN_INVALID]: An invalid token was provided

I am trying to create a Discord Bot. I just added some basic code, and then tried to run it.
Basically, I am trying to create a Discord Bot for my server. I learned a bit of a JavaScript and decided to give it a try.
I tried searching for similar problems on the internet, but nothing worked.
Command Prompt
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
PS C:\Users\RadoN\Desktop\Desktop items\RadoN's folder\Coding\a! RL Utilities - V2> npm run test
> rl-utilitiesv2#1.0.0 test
> node src/bot.js
C:\Users\RadoN\Desktop\Desktop items\RadoN's folder\Coding\a! RL Utilities - V2\node_modules\discord.js\src\client\Client.js:237
if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');
^
Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (C:\Users\RadoN\Desktop\Desktop items\RadoN's folder\Coding\a! RL Utilities - V2\node_modules\discord.js\src\client\Client.js:237:52)
at Object.<anonymous> (C:\Users\RadoN\Desktop\Desktop items\RadoN's folder\Coding\a! RL Utilities - V2\src\bot.js:16: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:77:12)
at node:internal/main/run_main_module:17:47 {
[Symbol(code)]: 'TOKEN_INVALID'
}
Node.js v17.4.0
bot.js
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] })
require('dotenv').config();
client.on('ready', () => {
// console.log(`Bot ready for work. Logged in as ${client.user.tag}!`)
console.log(process.env.token)
});
(async () => {
client.login(process.env.token);
})();

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

loader.js:800 throw err: 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)"

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.js, Express.js - unexpected token {

My app crashes every time it reaches this line:
const {name, price} = req.query;
^
can't seem to locate the exact answer..here is the error log
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:140:18)
at node.js:1043:3
context:
app.get('/products/add' , (req, res) => {
const {name, price} = req.query;
const INSERT_PRODUCTS_QUERY = `INSERT INTO products (name, price) VALUES ('${ name }', ${ price })`;
connection.query(INSERT_PRODUCTS_QUERY, (err,results) => {
if(err)
{
return res.send(err);
}
else
{
return res.send('succesfully added product');
}
});
});
According to node.green, the object destructuring with primitives syntax works after Node.JS v6.4.0, and throws the Unexpected Token { on Node.js versions below that.
Also, the object rest/spread properties only works out of the box from Node v8.6.0. It works in v8.2.1 with the --harmony flag, and throws the Unexpected Token ... on Node.js versions below that.
You tried to use destructuring assignment. AFAIK its support by nodejs v.6+ from a box and from 4.2.2 with flag --harmony_destructuring

Resources