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 toolkit8mo 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
monbrey8mo ago
Why are you trying to login a client inside a message event This doesnt make sense
rec
recOP8mo ago
i do not know
monbrey
monbrey8mo ago
user.rename isnt a thing You cant login as other users at all
rec
recOP8mo ago
wait hold wait
dank
dank8mo ago
it looks like they are calling client.login() outside the event though
rec
recOP8mo ago
am stupid sen t wrong code
monbrey
monbrey8mo ago
But also inside the event Which will break it
dank
dank8mo ago
ah
monbrey
monbrey8mo ago
Because you're logging in with not-a-token
dank
dank8mo ago
i see now
rec
recOP8mo 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
dank8mo ago
chatgpt is not a good source
monbrey
monbrey8mo ago
Yeah gpt is not for coding
rec
recOP8mo ago
yeah i can tell 8) i just didnt know djs
dank
dank8mo ago
you should learn how to code and use the library first
monbrey
monbrey8mo ago
They can change their own username, bots dont change people's usernames
rec
recOP8mo 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
monbrey8mo ago
whats that got to do with discord
rec
recOP8mo 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
monbrey8mo ago
change their nickname in a game via discord bot
rec
recOP8mo ago
yes
monbrey
monbrey8mo ago
Or in Discord?
rec
recOP8mo ago
da game i have the api endpoints in a client.js
monbrey
monbrey8mo ago
Well that part isnt related, but you can follow our guide on commands
d.js docs
d.js docs8mo ago
:guide: Creating Your Bot: Registering slash commands read more
rec
recOP8mo 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
dank8mo ago
that is not discord js related #other-js-ts
rec
recOP8mo ago
but i want it on the discord bot
dank
dank8mo ago
accessing an api isn't discord.js related though if you have a discord.js question yes you can ask that
rec
recOP8mo ago
so i put my support there?
Zerls
Zerls8mo 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