【问题标题】:How to send messages to specific Discord channels using keywords?如何使用关键字将消息发送到特定的 Discord 频道?
【发布时间】:2021-04-06 13:02:38
【问题描述】:

目前,我的机器人将接收任何使用 !wts 命令的消息,并将消息镜像到指定的通道(在代码中设置)。

如果消息包含“UK8”,它将被发送到 UK8 频道,或者如果它包含“UK3.5”,它将被发送到 UK3.5 频道,我该如何做到这一点,所以它会寻找关键字并将消息分配给相应的频道(而不是像我所做的那样对其进行硬编码)。

我刚刚开始使用 Discord 机器人,所以我很感激任何帮助。

这是我目前所拥有的:

    client.on('message', message => {

    if (message.author.bot) return undefined //bot does not reply to itself
  
    let msg = message.content.toLowerCase()
    let args = message.content
      .slice(prefix.length)
      .trim()
      .split(' ') //arguments
    let command = args.shift().toLowerCase() //shifts args to lower case letters
  
    if (command === 'wts') {
      let say = args.join(' ') //space
      //message.delete() - remove // if you want to have each message deleted
      const generalChannel = message.guild.channels.cache.get('793494585123465875')
      generalChannel.send(message.author.toString() + ": " + say)

    }

【问题讨论】:

    标签: javascript discord discord.js bots


    【解决方案1】:
    let nameChannel = "";
    
    if(say.includes("uk8")){
        nameChannel = "UK8";
    }else if(say.includes("uk3.5")){
        nameChannel = "uk3.5";
    }else{
        nameChannel = "defaultChannel";
    }
    
    const selectedChannel = client.channels.cache.find(channel => channel.name === nameChannel );
    selectedChannel.send(say)
    

    【讨论】:

      猜你喜欢
      • 2021-10-28
      • 2020-10-01
      • 2020-05-18
      • 2019-03-01
      • 2023-03-27
      • 2020-07-06
      • 1970-01-01
      • 2023-01-17
      • 2021-03-24
      相关资源
      最近更新 更多