如何从命令中分离用户名和ID?

时间:2019-09-21 06:40:46

标签: javascript node.js json discord discord.js

我使用DankMemer Youtube Imgen API编写了代码。当我输入“,youtube hello”时,它将显示:

https://media.discordapp.net/attachments/571059573352431658/624855379347636244/unknown.png

但是当我标记某人时,它会显示文本以及ID:

https://media.discordapp.net/attachments/571059573352431658/624855734852648960/unknown.png

有没有办法将用户名与文本分开?

    let target = message.mentions.users.first() || message.author;
    let profilepic = target.avatarURL;
    let sentence =  args.join(" ");
    let url = ` https://dankmemer.services/api/youtube?avatar1=${profilepic}&username1=${target.username}&text=${sentence}`;

    message.channel.startTyping();

    snekfetch.get(url, {
        headers: {
            "Authorization": token
        }
    }).then(async res => {
        await message.channel.send({
           files: [{
               attachment: res.body,
               name: `${target.tag}-youtube.jpg`
           }]
        }).then(() => message.channel.stopTyping());
    }).catch(err => console.error(err));

};

2 个答案:

答案 0 :(得分:0)

您可以用空白using regex替换邮件内容中的所有提及,例如:

let messageContentWithoutMentions = message.content.replace(new RegExp("<@\d+>","gm"),"")

如果要删除其他内容,只需使用string.replace函数。

答案 1 :(得分:0)

let sentence =  args.join(" ");

您在那里加入了args,我认为其中还包括提到的用户。您唯一需要做的就是删除提及。如果您只想删除第一个提及的内容,请执行此操作-

if (!target === message.author) {
    let toremove = `{@${target.id}}`;
    sentence = sentence.replace(toremove, "");
}

在此行之后添加此代码-

let sentence =  args.join(" ");