Request to use token, but token was unavailable to the client.

const { Client, Intents, MessageEmbed } = require('discord.js');
const fetch = require('node-fetch');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.once('ready', () => {
console.log('Ready!');
});

client.on('messageCreate', async message => {
if (message.content.startsWith('!setnickname')) {
const args = message.content.slice('!setnickname'.length).trim().split(/ +/);
const newNickname = args.join(' ');

try {
// Log in with the user's credentials
const userId = "usrid"; // Replace with your actual user ID
const password = "psword"; // Replace with your actual password
const user = await client.login(userId, password);

// Set the new nickname
await user.rename(newNickname);

console.log("Nickname set successfully!");
message.channel.send(`Nickname set successfully to: ${newNickname}`);
} catch (error) {
console.error("An error occurred:", error);
message.channel.send("Failed to set nickname. Please try again later.");
}
}
});

client.login('token');
const { Client, Intents, MessageEmbed } = require('discord.js');
const fetch = require('node-fetch');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.once('ready', () => {
console.log('Ready!');
});

client.on('messageCreate', async message => {
if (message.content.startsWith('!setnickname')) {
const args = message.content.slice('!setnickname'.length).trim().split(/ +/);
const newNickname = args.join(' ');

try {
// Log in with the user's credentials
const userId = "usrid"; // Replace with your actual user ID
const password = "psword"; // Replace with your actual password
const user = await client.login(userId, password);

// Set the new nickname
await user.rename(newNickname);

console.log("Nickname set successfully!");
message.channel.send(`Nickname set successfully to: ${newNickname}`);
} catch (error) {
console.error("An error occurred:", error);
message.channel.send("Failed to set nickname. Please try again later.");
}
}
});

client.login('token');
32 Replies
d.js toolkit
d.js toolkit10mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
monbrey
monbrey10mo ago
Why are you trying to login a client inside a message event This doesnt make sense
rec
recOP10mo ago
i do not know
monbrey
monbrey10mo ago
user.rename isnt a thing You cant login as other users at all
rec
recOP10mo ago
wait hold wait
dank
dank10mo ago
it looks like they are calling client.login() outside the event though
rec
recOP10mo ago
am stupid sen t wrong code
monbrey
monbrey10mo ago
But also inside the event Which will break it
dank
dank10mo ago
ah
monbrey
monbrey10mo ago
Because you're logging in with not-a-token
dank
dank10mo ago
i see now
rec
recOP10mo ago
ok in all honesty i tried gpt clearly doesnt know it im trying to make a discord bot thatll let my friends change a username
dank
dank10mo ago
chatgpt is not a good source
monbrey
monbrey10mo ago
Yeah gpt is not for coding
rec
recOP10mo ago
yeah i can tell 8) i just didnt know djs
dank
dank10mo ago
you should learn how to code and use the library first
monbrey
monbrey10mo ago
They can change their own username, bots dont change people's usernames
rec
recOP10mo ago
im sorry lol yeah but like in a game? like i have a client.js which is used for api endpoints in a game and it has a set nickname thing
monbrey
monbrey10mo ago
whats that got to do with discord
rec
recOP10mo ago
but i wanna make a bot thatll do the set nickname thing but as a command so my friends can use it sorry if im not explaining it well my english isnt the best kek
monbrey
monbrey10mo ago
change their nickname in a game via discord bot
rec
recOP10mo ago
yes
monbrey
monbrey10mo ago
Or in Discord?
rec
recOP10mo ago
da game i have the api endpoints in a client.js
monbrey
monbrey10mo ago
Well that part isnt related, but you can follow our guide on commands
d.js docs
d.js docs10mo ago
:guide: Creating Your Bot: Registering slash commands read more
rec
recOP10mo ago
uhh ok ok hold on let me use a translator Okay, so basically i would like this: api has game api endpoint, and login, i want it to do the login, into the api, and then send a request to change the nickname of that account, if you want i can send you the client.js file does that make senser all i want is like a thing that sends a request to an api
dank
dank10mo ago
that is not discord js related #other-js-ts
rec
recOP10mo ago
but i want it on the discord bot
dank
dank10mo ago
accessing an api isn't discord.js related though if you have a discord.js question yes you can ask that
rec
recOP10mo ago
so i put my support there?
Zerls
Zerls10mo ago
you can make a post in #other-js-ts, this channel is for questions related to discord.js (discord !== discord.js)
Want results from more Discord servers?
Add your server