Skip to content
Advertisement

Send a message to my private channel on join & leave

Heyo,

I want my bot to send a embed message to my private discord server when it joins & leaves a server. But the problem is that it does not send anything anywhere. My code looks like this:

exports.run = async (client, guild) => { 

    if(!guild.available) return
    
    
    if(!guild.owner && guild.ownerID) await guild.members.fetch(guild.ownerID);

if(!channel) return;

const embed = new MessageEmbed()
  .setTitle(`Bot joined a server`)
  .setDescription(`${guild.name}`)
  .setColor(0x9590EE)
  .setThumbnail(guild.iconURL())
  .addField(`Owner", "${guild.owner.user.tag}`)
  .addField(`Member Count", "${guild.memberCount}`)
  .setFooter(`${guild.id}`)
client.channels.cache.get('ID').send(embed)
    
  }

Advertisement

Answer

Your code doesn’t activate upon joining the server. For that you have a nice event (that has a misleading name) guildCreate – it is emitted whenever the client joins a guild.

So, your code should look something like this

client.on('guildCreate', async guild => {
    let YourChannel = await client.channels.fetch('channelid');
  const embed = new Discord.MessageEmbed()
.setTitle(`Bot joined a server`)
.setDescription(`${guild.name}`)
.setColor(0x9590EE)
.setThumbnail(guild.iconURL())
.addField(`Owner`, `${guild.owner.user.tag}`)
.addField(`Member Count`, `${guild.memberCount}`)
.setFooter(`${guild.id}`)
YourChannel.send(embed);
});

Same works for leaving the guild, use guildDelete event.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement