【问题标题】:Having different welcome channels有不同的欢迎渠道
【发布时间】:2020-09-28 08:08:09
【问题描述】:

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

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

我如何设置当机器人加入他们的公会时所有者可以使用的命令,该命令为每个公会设置一个独特的欢迎频道(显然只向加入他们的公会的人发送欢迎消息)。

哦,我该如何设置最终让人们更改公会欢迎信息的命令?

谢谢! :)

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    您需要有一个文件来存储每个公会的欢迎频道 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))
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2022-11-20
      • 1970-01-01
      • 2019-04-04
      • 2021-02-21
      • 2020-09-10
      相关资源
      最近更新 更多