首页 > 解决方案 > 我的舰船命令不适用于提及

问题描述

我正在尝试进行船舶指挥,我几乎已经完成了所有工作,但是当我使用它时有两个问题:

  1. 它从两个词中取前半部分(我想从第一个词中取前半部分,从第二个词中取后半部分)
  2. 当我提到 2 个人时,我的机器人没有回应

我的代码如下所示:

 function getRandomIntInclusive() {
            return Math.floor(Math.random() * (100 - 1 + 1)) + 1;
          }
        if (!args[0]) return message.channel.send("Podaj pierwszy argument!")
        if (!args[1]) return message.channel.send("Podaj drugi argument!")

        if (args[0] || args[1]) {
            var FirstUser = args[0]
            var SecondUser = args[1]

            if (message.mentions.members.first()) {
                const FirstUserSliced = FirstUser.user.username.slice(0, FirstUser.user.username.length / 2)
                const SecondUserSliced = SecondUser.map(user => { return user.user.username.slice(user.user.username.length / 2) })
                const SecondUserName = SecondUser.map(user => { return user.user.username })
            } else if (FirstUser || SecondUser) {
                const FirstUserSliced = FirstUser.slice(0, FirstUser.length / 2)
                const SecondUserSliced = SecondUser.slice (SecondUser.lenght / 2, SecondUser.length / 2)
                const SecondUserName = FirstUserSliced + SecondUserSliced 
            

                const embed = new MessageEmbed()
                .setTitle('Ship')
                .setDescription(`${SecondUserName}`)
                .addField(`Ocena shipu:`, `${getRandomIntInclusive()}%`)
                .setColor(0x0099ff)
              .setTimestamp()
              .setFooter(`${message.author.username}`);
                message.channel.send(embed)
            }
        }
    ```

标签: javascriptdiscordargumentsdiscord.js

解决方案


让我回答你的问题:

1.从两个词中取前半部分(我想从第一个词中取前半部分,从第二个词中取后半部分)

在您的代码中,您有这一行slice(SecondUser.lenght / 2, SecondUser.length / 2),假设您在lenght此处编写(StackOverflow),它的作用是削减前半部分,否则如果您有错字,它会通知您。

  1. 当我提到 2 个人时,我的机器人没有回应

因为你有那个if (message.mentions.members.first())没有进入 MessageEmbed 的 if 语句,所以如果你提到某人,它会转到第一个 if 语句并且因为没有消息而无法发送消息。

我为您的代码编写了一个更简单的版本,因此更容易理解。

function GetHalfText(first, second) {
    return first.substring(0, Math.floor(first.length / 2)) + second.substring(Math.floor(second.length / 2), second.length);
}
function Match(arg){
    return arg.match(/<@!?(\d{17,19})>/);
}
const {mentions, guild} = message
if(args.length < 2) return message.channel.send("You must enter two arguments")
const FirstArg = Match(args[0]) ? guild.members.cache.get(Match(args[0])[1]).user.username : args[0];
const SecondArg =  Match(args[1])|| guild.members.cache.get(Match(args[1])[1]).user.username || args[1];
const embed = new MessageEmbed()
                .setTitle('Ship')
                .setDescription(`${GetHalfText(FirstArg, SecondArg)}`)
                .addField(`Ocena shipu:`, `${getRandomIntInclusive()}%`)
                .setColor(0x0099ff)
              .setTimestamp()
              .setFooter(`${message.author.username}`);
                message.channel.send(embed)

推荐阅读