Home > Software design >  Is there a way to access message.guild.name in a ready event?
Is there a way to access message.guild.name in a ready event?

Time:11-06

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')
})

CodePudding user response:

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 */);
      }
   });
});

CodePudding user response:

Yes, you just have to use bot.guilds.cache.get("GUILD_ID").name instead of message.guild.name! Documentation

To change your bot nickname on server use:

bot.guilds.cache.get("GUILD_ID").me.setNickname(`${bot.guilds.cache.get("GUILD_ID").name} Utilities`)

So full code will be:

bot.guilds.cache.forEach(guild => {
bot.guilds.cache.get(guild.id).me.setNickname(`${guild.name} Utilities`)
})
  • Related