当用户不是字符串时如何修复 {user.username}?

How to fix {user.username} when user isn't a string?

我实际上正在编写一个 Js Discord Bot,我正在创建一个 XP 系统。

我希望我的机器人在输入 &level 时给出作者的级别,并且它有效。

但如果我这样做 &level @DiscordUser,我可以获得此 Discord 用户的用户 ID,除非我在这样的嵌入中使用它:

const embed = {
                  "title": "Fiche Niveau d'Exagide",
                  "description": 'Utilisateur : **' + `${userid.username}` + "**",
                  "color": 10384204
               }

在这种情况下 returns : Utilisateur : undefined

userid 是从将 Mention 转换为 ID

的函数中获取的 const

例如,它将 <@549317568339640336> 变为 549317568339640336

function getUserFromMention(mention) {
              if (!mention) return;

              if (mention.startsWith('<@') && mention.endsWith('>')) {
                mention = mention.slice(2, -1);

                if (mention.startsWith('!')) {
                  mention = mention.slice(1);
                }
                return mention;
              }}

我只是希望嵌入 return Utilisateur : Discord User 如果 @Discord User 被提及。

您可以使用此代码

 function mentionsRegex (dot) {
if (dot) {
// e.g. @google.com will match `google.com`
return /(?:^|[^a-zA-Z0-9_@!@#$%&*])(?:(?:@|@)(?!\/))([a-zA-Z0-9/_.]{1,15}) 
 (?:\b(?!@|@)|$)/
}
 // e.g. @google.com will match `google`
return /(?:^|[^a-zA-Z0-9_@!@#$%&*])(?:(?:@|@)(?!\/))([a-zA-Z0-9/_]{1,15})(?:\b(?!@| 
@)|$)/
}

取自mentions-regex 你也可以看到 mentions out of strings like twitter in javascript

只需简单地使用以下方法获取提到的用户的用户对象:

message.mentions.users.first()

或提及用户的成员对象:

message.mentions.members.first()