Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

node.js - Change nickname from all servers getting the guilds ids

there is any way to change the NICKNAME of the bot with the guilds ids in all the servers that the bot is (the bots has the perm to change nickname).

This is the code that I used to get the guilds ids but idk how to change the nickname in all the guilds using the ids.

const { Client } = require('discord.js');
const client = new Client();

client.on("ready", () => {
    console.log(`Logged in as ${client.user.tag}!`);
    client.user.setPresence({
        status:"online",
        activity: {
            name: "Loading....",
            type: "WATCHING"
        }
    })
})


client.on("message", message => {

    const servers = client.guilds.cache.map(guild => guild.id);
    console.log(servers);


})

client.login(TOKEN)

question from:https://stackoverflow.com/questions/65650504/change-nickname-from-all-servers-getting-the-guilds-ids

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all, this code will console log every Guild#id when a new message is getting received by the bot.

client.on("message", message => {

    const servers = client.guilds.cache.map(guild => guild.id);
    console.log(servers);


})

I would recommend due to personal experience to map all guilds inside the ready event.

To change the nickname from the bot (You actually don't need mapping all guilds) you could do this by writing this code into your ready event:

client.guilds.cache.forEach((guild) => {
    guild.me.setNickname(/* Nickname as a string */);
});

You should write this code into your ready event because if you write it just into the file you would try to change your nickname while you haven't even fetched all guilds from the API.

If you only want to do this after you executed a specific command, you can use the code from above and paste it into the command.

Reference:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...