Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
55
rated 0 times [  58] [ 3]  / answers: 1 / hits: 5535  / 4 Years ago, wed, november 25, 2020, 12:00:00

Hi I'm creating a bot for my server and I was trying to make a member counter in voice channels and when I finished doing it I wrote in the terminal: node index.js and he started saying that the token was not available to my client.


ERROR:


(node:9596) UnhandledPromiseRejectionWarning: DiscordjsError: Request
to use token, but token was unavailable to the client. at RequestHandler.execute

Here is my code


Note: I did all the bot code in a file just to be able to use it in a .bat file.


More From » node.js

 Answers
7

It's because on line 6 where you try to fetch, you're not logged in. You're calling bot.login() later (and it's async)


const guild = bot.guilds.fetch('222078108977594368')

Check https://discordjs.guide/popular-topics/errors.html#request-to-use-token-but-token-was-unavailable-to-the-client


If you want to fetch it only once, you can probably move this to the ready state's handler:


const Discord = require('discord.js')
const { send, stdout } = require('process')

const bot = new Discord.Client()
const token = 'XXX'
const prefix = 'a!'
// use let, so you can change it later in bot.on('ready')
let guild = null

// this event will only trigger after logging in
bot.on('ready', () => {
const ping = new Date()
// fetch here
guild = bot.guilds.fetch('222078108977594368')

ping.setHours(ping.getHours() - 3)
console.log(`BOT INICIADO AS ${ping.getUTCHours()}:${ping.getUTCMinutes()}:${ping.getUTCSeconds()}`)
console.log('GuruGuru Está Online!')

bot.user.setActivity(`${bot.guilds.cache.size} servidores`, { type: 'WATCHING' });
})

[#2233] Saturday, November 21, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darennevina

Total Points: 422
Total Questions: 128
Total Answers: 105

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
darennevina questions
;