Search code examples
javascriptnode.jsdiscorddiscord.js

Is there a way to access message.guild.name in a ready event?


I'm coding a utilities bot and I want it to change its nickname on every server to <server_name> Utilities automatically when I run my bot.

Heres my ready event:

bot.on('ready',async  () => {
  console.log('|| MaRa Utilities, Console')
  console.log(`    Logged in as "#${bot.user.discriminator}"`)
  console.log(`\nBot "#${bot.user.discriminator}" has been loaded successfully...\n`)
  console.log(`The Prefix Is: ${PREFIX}`)
  console.log(`Bot "#${bot.user.discriminator}" Is in ${bot.guilds.cache.size} guild/s\n`)
  console.log(`|| Logs/Errors:`)
db.get("Prefix").then(value => {if(value != PREFIX){
  console.log('A Dev has changed the "PREFIX" on startup.')
  db.set("Prefix", PREFIX);
}})
db.get("BotAuthorID").then(value => {if(value != BotAuthorID){
  console.log('A Dev has changed the "BotAuthorID" on startup.')
  db.set("BotAuthorID", BotAuthorID);
}})
db.get("TOKEN").then(value => {if(value != TOKEN){
  console.log('A Dev has changed the "TOKEN" on startup.')
  db.set("TOKEN", TOKEN);
}})

  bot.user.setActivity(`DM For Help!`)
  bot.user.setUsername(bot.Message.guild.name + ' Utilities')
})

Solution

  • Iterate through GuildManager#cache. Check if the client has permission to change it's own nickname (string constant "CHANGE_NICKNAME") if it does have the permission then use Member#setNickname()

    bot.on('ready', async() => {
       bot.guilds.cache.forEach(guild => { 
          const botMember = guild.me;
          if (botMember.permissions.has('CHANGE_NICKNAME')) {
             botMember
                .setNickname(`${guild.name} Utilities`)
                .catch(err => /* Handle Error */);
          }
       });
    });