有不同的欢迎频道

时间:2020-06-09 07:55:17

标签: node.js discord.js

我需要我的不和谐机器人记住在不同行会中向哪个频道发送问候。 现在,我将频道名称作为前缀,并使用它来回忆将其发送到哪里:

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

当机器人加入其行会时,如何设置所有者可以使用的命令,该命令为每个行会设置一个唯一的欢迎渠道(并且显然仅向加入其行会的人发送欢迎消息)。

哦,我该如何设置一个命令,最终使人们更改其行会的欢迎信息?

谢谢! :)

1 个答案:

答案 0 :(得分:0)

您需要有一个文件,用于存储每个行会的欢迎频道ID,以便以后进行检查。您可以只使用JSON文件:

// 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]

您可以使用变量存储对象,该对象将保存到可以用fs.writeFileSync更新的文件中。
guildMemberAdd处理程序中,您可以从新成员那里获取行会ID,然后使用它来获取频道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
})

示例

在主文件中,您只需要第一次使用它,并在guildMemberAdd处理程序中使用它。

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

对于该命令,它实际上取决于您的命令系统:您需要找到一种在文件之间共享welcomeChannels变量的方法,通常通过import/export来完成。设置一个新值。

// command file

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