【问题标题】:How to fix {user.username} when user isn't a string?当用户不是字符串时如何修复 {user.username}?
【发布时间】:2019-08-29 05:49:25
【问题描述】:

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

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

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

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

在这种情况下,它返回:Utilisateur : undefined

userid 是从将 Mention 转换为 ID 的函数中获取的常量

例如,它将<@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;
              }}

如果提到@Discord User,我只是希望嵌入返回Utilisateur : Discord User

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    只需简单地使用以下内容即可获取所提及用户的用户对象:

    message.mentions.users.first()
    

    或提及用户的成员对象:

    message.mentions.members.first()
    

    【讨论】:

      【解决方案2】:

      你可以使用这个代码

       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

      【讨论】:

      • 它不工作它返回这个 /(?:^|[^a-zA-Z0-9_@!@#$%&*])(?:(?:@|@) (?!\/))([a-zA-Z0-9/_]{1,15})(?:\b(?!@| @)|$)/ 而不是用户 ID
      猜你喜欢
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多