【问题标题】:My ship command doesn't work with mentions我的舰船命令不适用于提及
【发布时间】:2021-05-25 22:07:05
【问题描述】:

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

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

【问题讨论】:

    标签: javascript discord arguments discord.js


    【解决方案1】:

    让我来回答你的问题:

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

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

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

    因为你有那个 if 语句 if (message.mentions.members.first()) 没有进入 MessageEmbed,所以如果你提到某人,它会转到第一个 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)
    

    【讨论】:

    • 感谢您的帮助,但下一个问题是您不能使用此命令未提及的两个参数,但它仍然很有帮助,我想我将能够在我的代码
    • 更新了我的答案,看看你想要什么
    • 当我使用你的代码时,无论我使用 mantions 还是普通文本作为参数 bot 都会说“你必须输入两个参数”(即使我输入其中 2 个)
    • if(!args.length &lt; 2) 改成if(args.length &lt; 2) 我打错了
    • 当我输入 args: TypeError: Cannot read property '1' of null 当我输入两个提及时: ReferenceError: FirstUser is not defined 关于第一个我什至不知道这是什么意思关于第二个,我想将 FirstUser 更改为 FirstArg 但它无法从 args 获取用户的用户名(例如:m!ship 1234 5678)没有任何用户名可以使用,所以它仍然可以工作有提及
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2021-08-05
    • 2014-01-18
    相关资源
    最近更新 更多