【问题标题】:How to make a discord.js bot say custom messages in a specific channel & tag a user in a message如何让 discord.js 机器人在特定频道中说出自定义消息并在消息中标记用户
【发布时间】:2021-02-02 14:26:07
【问题描述】:

所以,我想为我和我的朋友创建自己的不和谐服务器,并且我希望它具有一些机器人功能......所以我开始编码......长话短说我搞砸了,经过几个小时的尝试找到我放弃的解决方案。

第一个是一个词过滤器,工作得很好,我无法标记任何人.. 它应该删除所说的消息并发出一条消息,就像“@example,不要这么说!”

client.on('message', async(msg) => {
    
    if(msg.author.bot) return;
    if(!msg.guild) return;

    var array = ['example1, 'example2'];

    if(!msg.member.hasPermission('MANAGE_MESSAGES')){
        if(array.some(w => ` ${msg.content.toLowerCase()} `.includes(` ${w}`))){
            msg.delete();
            msg.channel.send(`${client.user.tag}`)
            return;
        }
    }
        
});

我的第二个问题: 我尝试创建一个命令,每次您在 Discord 上输入 (prefix)say + "text" 时,机器人都会说出该消息。最好的办法是您可以在一个文本通道中输入所有命令,并且能够选择我希望机器人也输入该消息的通道。简单的变体对我来说也很好。这是我的代码:

const isValidCommand = (message, cmdName) => message.content.toLowerCase().startsWith(prefix + cmdName);

require('dotenv').config();

client.on('message', function(message) {
    if(message.author.bot) return;

    else if(isValidCommand(message, "say")) {
        let marketing = message.content.substring(5);
        let marketingchannel = client.channels.cache.get('766732225185054740');
        let logsChannel = client.channels.cache.find(channel => channel.name.toLowerCase() === 'logs');
        if(logsChannel)
            logsChannel.send(marketing);
    }
});

我真的希望我能让它工作..

提前致谢!

【问题讨论】:

  • 嘿,您在第一个代码块中缺少一个结束 '。就在 example1 之后和 , 之前在您的 array 变量中。它应该看起来像: var array = ['example1', 'example2'];

标签: javascript discord.js bots


【解决方案1】:

第一个问题

User.tag 实际上并没有提及用户。用户的usernamediscriminator 组合标签。

'Lioness100'; // my username
'4566'; // my discriminator

'Lioness100#4566'; // my tag

有几种方法可以在消息中提及某人。第一个是内置的Message.reply() 函数,它将发送给定的消息,前面加上<mention of author>,

第二种方法是将User/GuildMember 对象转换为字符串。 Discord 会自动将其解析为提及。

第三种也是最通用的方法是使用提及语法。对于用户,它是<@id>


你的第二个问题

我不太确定您的代码是否有问题,或者您的要求究竟是什么,但我认为您希望根据消息动态选择频道。您可以通过创建一个args 变量来做到这一点。

// new usage:
// {prefix}say channelName message

client.on('message', function(message) {
 if (message.author.bot) return;

 // split string by every space => returns an array
 const args = message.content.slice(prefix.length).split(/ +/);

 // removes the first element (the command name) and stores it in this variable => returns a string
 const command = args.shift().toLowerCase();

 // now it's much easier to check the command
 if (command === 'say') {
  // the first element of `args` is the first word after the command, which can be the channel name
  // we can then find a channel by that name => returns a Channel or undefined
  const channel = message.guild.channels.cache.find(
   (c) => c.name === args[0]
  );

  if (!channel)
   return message.channel.send(`There is no channel with the name ${args[0]}`);

  // join all args besides the first by a space and send it => returns a string
  channel.send(args.slice(1).join(' '));
 }
});

【讨论】:

  • 哇,非常感谢!它只是说如果我尝试输入频道名称并且我确定我做错了,则没有定义频道。我必须使用引号还是..?我的标准前缀是“-”,我告诉机器人用“对不起,我不知道这个命令”对每个不存在的命令做出反应,所以当我输入“说”时,它总是告诉我它不知道命令。 .我想只将该命令的前缀更改为“?”。我该怎么做?
猜你喜欢
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 2020-02-26
  • 2020-11-10
  • 2021-10-19
相关资源
最近更新 更多