0
votes

I need my discord bot to remember which channel to send a greeting to in different guilds. For now, I have the channel name as a prefix and I use that to recall where to send it:

//greeting new users script
bot.on('guildMemberAdd', member => {
  // Send the message to a designated channel on a server:
  const WelcomeChannel = member.guild.channels.cache.find(ch => ch.name === config.WelcomeChannelVar);
  // Do nothing if the channel wasn't found on this server
  if (!WelcomeChannel) return;
  const welcomeEmbed = new Discord.MessageEmbed()
    .setAuthor(member.displayName.toString() + '#' + member.user.discriminator, member.user.displayAvatarURL())
    .setTitle('someone joined!')
    .setDescription('welcome to **' + member.guild.name + '**, <@' + member.id + '> !')
    .setColor(0x348a58)
    .setThumbnail(member.user.avatarURL())
    .setFooter('you\'re member #' + member.guild.memberCount + '!')
  setTimeout(() => {
    WelcomeChannel.send(welcomeEmbed)
  }, 200);

  member.send("welcome to " + member.guild.name + "! please **read the rules**, and *follow them* :) if you need any help, please **ping a staff member**.");
});

How do I set a command that owners can use when the bot joins their guild that sets a unique welcome channel for each guild (and obviously only send welcome messages to people who join in their guild).

Oh, and how do I set a command that eventually lets people change the welcome message for their guild?

Thanks! :)

1

1 Answers

0
votes

You need to have a file where you store the welcome channel IDs for every guild, so that you can later check them. You can just use a JSON file:

// Define it one time
const welcomeChannels = require('./path/to/your/file.json')

// When you want to set the channel for a guild
welcomeChannels[guild.id] = channel.id
fs.writeFileSync('./path/to/your/file.json', JSON.stringify(welcomeChannels))

// When you need to read a property
let welcomeChannelID = welcomeChannels[guild.id]

You can use a variable to store the object, which is saved to a file that you can update with fs.writeFileSync.
Inside the guildMemberAdd handler, you can get the guild id from the new member and then use it to get the channel id:

bot.on('guildMemberAdd', member => {
  let id = welcomeChannels[member.guild.id]
  let welcomeChannel = member.guild.channels.cache.get(id)

  // The rest is the same as in your code
})

Example

In the main file you just need to require it for the first time and use it in your guildMemberAdd handler.

// main file

const welcomeChannels = require('./path/to/your/file.json')

bot.on('guildMemberAdd', member => {
  let id = welcomeChannels[member.guild.id]
  let welcomeChannel = member.guild.channels.cache.get(id)

  // The rest is the same as in your code
})

For the command it really depends on your command system: you need to find a way of sharing the welcomeChannels variable across the files, which is typically done though import/export Here's a mock function on how to set a new value.

// command file

function setId(guildID, channelID) {
  welcomeChannels[guild.id] = channel.id
  fs.writeFileSync('./path/to/your/file.json', JSON.stringify(welcomeChannels))
}