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