简体   繁体   中英

Welcome message in Discord.js

I'm programming a discord bot and I'm having a problem in one of the main functionalities - the welcome message.
I've tried several methods but none of them worked, except for one: the one that needs a channel ID .

I dont want to use that method because I want to use my bot on multiple servers, and that would mean that I have to change the code for each one of them - and I really dont want to.

client.on('guildMemberAdd', function(message) {
 member.guild.channels.cache.get('MY CHANNEL ID').send('welcome dadadadada');
});

This code worked (actually, I just tried it and it doesn't - but it did. It was something like that). Now I'm trying more 'advanced' coding to tell the bot to automatically get the channel ID from #general (for this I did npm install long )

const getDefaultChannel = (guild) => {

  if(guild.channel.has(guild.id))
    return guild.channels.get(guild.id)

    const generalChannel = guild.channels.find(channel => channel.name === 'general');
    if (generalChannel)
      return generalChannel;
    return guild.channels
      .filter(c => c.type === 'text' &&
      c.permissionsFor(guild.client.user).has('SEND_MESSAGES'))
    .sort((a, b) => a.position - b.position) ||
      Long.fromString(a.id).sub(Long.fromString(b.id)).toNumber()
    .first();
}

client.on("guildMemberAdd", member => {

  const channel = getDefaultChannel(member.guild);

  channel.send(`Welcome ${member} to the server, wooh!`);
});

I didn't find anything useful and I don't know what to do. Also, english is not my first language.

What else could I try? Thank you.

I think this could help you!

The guildMemberAdd listener can send messages

client.on('guildMemberAdd', function(member, message) => {
  let generalChannelName = message.guild.channels.find(channel => channel.name === 'general');
  let generalChannel = generalChannel.id;

  generalChannel.send(`Welcome ${member.tag} to the server!`);
});

It's hard to insert your filtering here, may this help you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM