Error [TOKEN_INVALID], Turns valid token into invalid token - node.js

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

Related

DiscordJS - Send Message

Im Trying To Send A Message Through DiscordJS
Can Someone Help Me
I Tried This Code
const channel = client.channels.cache.get('example discord guild');
channel.send('content');
But It Doesent Work
index.js
// Credetials
const { token } = require('./json/token.json');
const { guild } = require('./json/guild.json');
const { client } = require('./json/client.json')
// Init
const { Client, Intents } = require('discord.js');
const bot = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_>
bot.login(guild);
console.log("Logged In As DeltaBOT");
const channel = client.channels.cache.get(guild);
channel.send('content');
Error
const channel = client.channels.cache.get(guild);
^
TypeError: Cannot read properties of undefined (reading 'cache')
at Object.<anonymous> (/storage/emulated/0/Download/node/index.js:15:33)
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
You should wait for your client to login before attempting to send a message / fetching a channel. You can do this by using the ready event. For example:
bot.on('ready', async () => {
console.log('Logged in as: ' + bot.user.tag);
const channel = await bot.channels.fetch(guild);
await channel.send('content');
})
Another thing I noticed is that you use client instead of bot. client is a json object but you defined your discord bot object as the bot variable. So use the bot variable and not the client one.
Make sure that the guild is a valid guild ID. I don't know what is in your client json file but you don't appear to be using it.

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"] })

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 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;

Resources