const Discord = require("discord.js")
require("dotenv").config()
const client = new Discord.Client({ intents: [] })
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on("message", msg => {
if (msg.content === "ping") {
msg.reply("pong");
}
})
client.login(process.env.TOKEN)
if (!token || typeof token !== 'string') throw new DiscordjsError(ErrorCodes.TokenInvalid);
^
Error [TokenInvalid]: An invalid token was provided.
at Client.login (C:\Users\johnw\node_modules\discord.js\src\client\Client.js:214:52)
at Object.<anonymous> (C:\Users\johnw\WebstormProjects\DiscordBot\index.js:15:8)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
at node:internal/main/run_main_module:23:47 {
code: 'TokenInvalid'
}
Node.js v19.0.0
ENV FILE - This is an old Token
TOKEN=MTA0NTI0ODI0NjIyMzc0NTAzNA.G36OM9.q2GxfF8ZOXqIjkKcAAnOsH_XbuC_vbgLDuOLT8
I am trying to run my bot but it always tells me that my token is invalid.
I tried refreshing my token and using the new one but even this won't help.
Folder Structure
Rename process.env (the file) to .env. That should fix the problem.
First of all, you shouldnt post your actual bots token online. Also it seems you are using env file, i personally just recommend using a json file for it since its much more convinient.
that way you can just do
const { token } = require('./config.json');
it looks like the error prompts that the TOKEN var isnt a string, so if you want to use env file. Cast it to string before parsing it into the param.
Related
I'm currently working on a new personal project and it seems that I can't retrieve the images that I want from my profile since I can't authenticate properly.
My code to authenticate is the following:
var dotenv = require('dotenv');
var unsplash = require('unsplash-api');
dotenv.config()
var clientId = new unsplash({ clientId: process.env.myAccessKey }); //this is required to verify your application's requests
unsplash.init(clientId);
The issue that I'm getting is the following:
var clientId = new unsplash({ clientId: process.env.myAccessKey }); //this is required to verify your application's requests
^
TypeError: unsplash is not a constructor
at Object.<anonymous> (/Users/aharo5/Desktop/javascript/unsplash_portfolio/main.js:6:16)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Module._load (node:internal/modules/cjs/loader:922:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
at node:internal/main/run_main_module:23:47
Could some one help me out with this request?
This question already has answers here:
How do I fix CLIENT_MISSING_INTENTS error?
(5 answers)
Discord.js v12 code breaks when upgrading to v13
(1 answer)
Closed 1 year ago.
I've tried to create a bot on discord, and with multiple tutorials, I've followed all of the instructions, and it seems to work fine in the beginning. However, with all of the tutorials, I'm met with one problem. In my index.js file for my bot, when I open the VSCode terminal and type in the command node index.js (which is supposed to make my bot go online, I get the following error messages:
/Users/myname/Desktop/testbot/node_modules/discord.js/src/client/Client.js:544
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
at Client._validateOptions (/Users/myname/Desktop/testbot/node_modules/discord.js/src/client/Client.js:544:13)
at new Client (/Users/myname/Desktop/testbot/node_modules/discord.js/src/client/Client.js:73:10)
at Object.<anonymous> (/Users/myname/Desktop/testbot/index.js:2:13)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981: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)]: 'CLIENT_MISSING_INTENTS'
}
I have node.js 16.13.0, and discord.js installed. My bot is a member of my server, but it's always offline. What can I do to fix it?
Here's the code for index.js:
const Discord = require('discord.js');
const bot = new Discord.Client();
const token = '[not putting my token online]';
const PREFIX = '';
bot.on('ready', () => {
console.log('Online!');
});
bot.login(token);
It seems like you don't have intents added with your bot which is needed to get it running according to the discord.js starter guide
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const token = 'your token here';
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
// Login to Discord with your client's token
client.login(token);
I'm started to make a bot. But when I try to node, it giving that.
Here is the source code and I tried to solve with intents. I solved one error but it gives bitfield error now.
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["BOT"] })
const Token = "censor";
client.once('ready', () => {
console.log('Agres is online!');
});
client.login(Token);
C:\Users\Mustikk\Desktop\Projects\AgresBot\node_modules\discord.js\src\util\BitField.js:152
throw new RangeError('BITFIELD_INVALID', bit);
^
RangeError [BITFIELD_INVALID]: Invalid bitfield flag or number: BOT.
at Function.resolve (C:\Users\Mustikk\Desktop\Projects\AgresBot\node_modules\discord.js\src\util\BitField.js:152:11)
at C:\Users\Mustikk\Desktop\Projects\AgresBot\node_modules\discord.js\src\util\BitField.js:147:54
at Array.map (<anonymous>)
at Function.resolve (C:\Users\Mustikk\Desktop\Projects\AgresBot\node_modules\discord.js\src\util\BitField.js:147:40)
at Client._validateOptions (C:\Users\Mustikk\Desktop\Projects\AgresBot\node_modules\discord.js\src\client\Client.js:546:33)
at new Client (C:\Users\Mustikk\Desktop\Projects\AgresBot\node_modules\discord.js\src\client\Client.js:73:10)
at Object.<anonymous> (C:\Users\Mustikk\Desktop\Projects\AgresBot\bot.js:2:16)
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) {
[Symbol(code)]: 'BITFIELD_INVALID'
}
"BOT" isn't an intent. The right way to put intents are like this :
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
In discord.js v12, setting intents in Client options is not necessary; you can do it if you want but by default, Discord will just send events for all intents you qualify for (all non-privileged intents + privileged intents you are whitelisted for/enable in the dev portal).
In discord.js v13, you will have to specify the intents you need.
Take a look at the example I gave above, you can use this example for adding the intents you need!
Also, you technically can request all intents with intents: Discord.Intents.ALL, however you really should not do this because that removes the whole point of intents; not sending the bot events it doesn't need to reduce bandwidth, CPU, and RAM consumption.
i am currently trying to connect to a MySQL server on the internet using Node.Js with the mysql or the mysql2 NPM dependencies to use queries and other related stuff.
the code is simple...
//i import my dependency
const mysql = require('mysql2') //either 'mysql' or 'mysql2'
//i create my pool to create connections as needed
var conn = mysql.createPool({
host: 'some_database_i_have_access_to.mysql.uhserver.com',
user: 'valid_user',
password: 'valid_password',
database: 'some_existing_database'
})
//i try to connect (this is the part where it fails)
conn.getConnection((err,conn) => {
if (err) throw err //<- the error is thrown here
//i do query stuff
conn.query("SELECT * FROM atable",(err,res,firlds) => {
if(err) throw err
console.log(JSON.stringify(res))
})
//i close the connection
conn.end()
})
yet i always get an Error like this:
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
--------------------
at Protocol._enqueue (C:\Users\Aluno\Desktop\my-project\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\Aluno\Desktop\my-project\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\Aluno\Desktop\my-project\node_modules\mysql\lib\Connection.js:118:18)
at Object.<anonymous> (C:\Users\Aluno\Desktop\my-project\private\dtp-mysql.js:13:6)
at Module._compile (internal/modules/cjs/loader.js:707:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
at Module.load (internal/modules/cjs/loader.js:605:32)
at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
at Function.Module._load (internal/modules/cjs/loader.js:536:3)
at Module.require (internal/modules/cjs/loader.js:643:17)
all i know about the error is that the connection abruptly closes in one of the sides as stated in this question (Node js ECONNRESET), but nothing more, and creating singular connections does not solve this issue for me either.
any fixes to that?
you can also ref below url.
error while inserting LARGE volume data in mysql by using node.js (error code: 'ECONNRESET')
I have fixed this issue. It is caused by the default definition max_allowed_packet. Find max_allowed_packet in my.ini (C:\ProgramData\MySQL\MySQL Server 5.7). Update to 'max_allowed_packet=64M'. Restart mysql. Done.
Hi I know this was asked some time ago, but is it possibly because you're using:
conn.end()
Since you're using a Pooled connection, I think you can release the connection using
conn.release()
Or
conn.destroy()
I'm trying to create an sort of plugin that users can simply add to a website and it will make COR calls to my app and return JSON that will be handled by the client side javascript.
This is working how I want it to, but now I'm trying to make sure that the user logs into my app before being allowed to receive any JSON from my server side app.
From here on I'll refer to my Node.js API as Server and the straight JS plugin as Client
I found a npm plugin for node that handles OAuth2 on the Server, but I'm not sure I'm really understanding how to use it. Here's the link and I found this for taking care of it on the Client side.
Client -> App initializer:
define [
'oauth2'
], (oauth2) ->
App =
Models: {}
Collections: {}
Views: {}
initialize: () ->
$.get "/javascripts/mu-config.json", (config) =>
#api_url = config.api
#site = config.site
#credentials = config.credentials
#make_oauth_call()
make_oauth_call: ->
#xhr = new oauth2.OAuth2XMLHttpRequest
authorizeEndpoint: "#{this.api_url}/callback"
tokenEndpoint: "#{this.api_url}/oauth/access_token"
clientID: this.credentials.clientID
clientSecret: this.credentials.clientSecret
localStoragePrefix: "oauth2.#{this.site.name}"
requestAuthorization: (callback) ->
console.log 'what?'
console.log callback
#xhr.onreadystatechange = () ->
console.log "do something"
#xhr.open "GET", "#{this.api_url}/notes?site=1&user=1"
#xhr.setRequestHeader 'Content-type', 'application/x-www-form-urlencoded'
#xhr.send "site=1&user=1"
So what works here? Well the #xhr.open ... does in fact grab JSON from the Server, but that's about it. I'm not getting any errors from the Client, but the console.log 'what?' does not fire and I don't believe anything is getting authenticated.
Server -> oauth.coffee
token = null
credentials =
clientID: "sparkmasterflex"
clientSecret: "bob_the_builder"
site: 'http://marking_up.dev'
OAuth2 = require('simple-oauth2') credentials
authorization_uri = OAuth2.AuthCode.authorizeURL
redirect_uri: 'http://localhost:3000/callback'
scope: 'sites'
state: '55fce6241c8e6432e8dfee583141aa58'
res.redirect(authorization_uri)
OAuth2.AuthCode.getToken
code: "something here"
redirect_uri: "http://localhost:3000/callback"
, saveToken
saveToken = (error, result) ->
console.log('Access Token Error', error.message) if error
token = OAuth2.AccessToken.create(result)
module.exports = OAuth2
Server -> router
express = require("express")
db = require "../database"
oauth2 = require "../oauth"
router = express.Router()
# GET home page.
router.get "/", (req, res) ->
res.render 'index',
title: "Hello world"
# Initial page redirecting to Github
router.get '/auth', (req, res) ->
res.redirect authorization_uri
# Callback service parsing the authorization token and asking for the access token
# router.get '/callback', (req, res) ->
router.route('/callback')
.get (req, res) ->
code = req.query.code
console.log '/callback'
oauth2.AuthCode.getToken
code: code
redirect_uri: 'http://localhost:3000/callback'
, saveToken
saveToken = (error, result) ->
console.log('Access Token Error', error.message) if error
token = oauth2.AccessToken.create(result)
module.exports = router
Running the node server I get this error:
/Users/raymondke99/Sites/marking_up_api/oauth.js:19
res.redirect(authorization_uri);
^
ReferenceError: res is not defined
at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/oauth.js:19:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/routes/index.js:7:10)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
I'm kinda at a loss here. The documentation for both of these seem pretty thorough but I still feel like I'm missing a huge chunk of information. Can anyone help and/or lead me to help?
Thank you
EDIT
I removed res.redirect() from oauth.coffee and I get the following error:
/Users/raymondke99/Sites/marking_up_api/node_modules/simple-oauth2/lib/core.js:16
throw new Error('Callback not provided on API call');
^
Error: Callback not provided on API call
at Object.api (/Users/raymondke99/Sites/marking_up_api/node_modules/simple-oauth2/lib/core.js:16:13)
at Object.getToken (/Users/raymondke99/Sites/marking_up_api/node_modules/simple-oauth2/lib/client/auth-code.js:34:8)
at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/oauth.js:19:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/routes/index.js:7:10)
I have more than one router because I'm using expressjs and I'm not sure where I'm supposed to have the 'catch-all' redirect. Does it need to go into every router?
Why do you have "res.redirect(authorization_uri)" in the oath file? You seem to already have the GET endpoint in your router?