【问题标题】:Is there a way to get a user's ID from a mention without them being in the guild?有没有办法在不加入公会的情况下从提及中获取用户的 ID?
【发布时间】:2021-09-21 04:54:36
【问题描述】:

我正在尝试通过提及来发出取消禁令命令。如果有一种方法可以通过提及获取用户 ID 而无需他们在我正在执行命令的实际行会中,那就太好了。

尝试执行时,我收到一条错误消息,告诉我它无法读取未定义的属性“id”。但是当我在用户在公会时这样做时,它可以很好地阅读它。

我的代码:

const Discord = require("discord.js");

module.exports = {
    name: "unban",
    aliases: [],
    usage: "{prefix}unban <user>",
    category: "moderation",
    desc: "Unban a banned user.",
    run: async (client, message, args) => {

        let unbanned1 = message.mentions.users.first().id || args[0];
        let unbanned = await client.users.fetch(unbanned1);

        let ban = await message.guild.fetchBans();

        // MESSAGES

        if (!args[0]) {
            return message.channel.send('❌ - Please specify a user to unban.')
        }

        if (!unbanned) {
            return message.channel.send(`❌ - User not found.`)
        }

        if (!ban.get(unbanned.id)) {
            return message.channel.send("❌ - This user hasn't been banned.")
        }

        // No author permissions
        if (!message.member.hasPermission("BAN_MEMBERS")) {
            return channel.send("❌ You do not have permissions to ban members.")
        }
        // No bot permissions
        if (!message.guild.me.hasPermission("BAN_MEMBERS")) {
            return channel.send("❌ I do not have permissions to ban members. Please contact a staff member")
        }

        var user = ban.get(unbanned1);
        message.guild.members.unban(unbanned1);

        const embed = new Discord.MessageEmbed()
            .setColor("GREEN")
            .setAuthor(user.user.username, user.user.displayAvatarURL({ dynamic: true }))
            .setDescription(`${user.user.tag} got unbanned:`)
            .setTitle("User Unbanned Successfully")
            .addField(`By:`, `${message.author.tag}`, true)
            .setThumbnail(user.user.displayAvatarURL({ dynamic: false }))
            .setFooter(message.member.displayName, message.author.avatarURL({ dynamic: true }))
            .setTimestamp()


        message.channel.send(embed);
    },
};

提前谢谢你。

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    您可以使用正则表达式从用户提及模式中匹配和捕获用户的 ID,如下所示:/&lt;@!?(\d{17,19})&gt;/

    <@ - matches these characters literally
    !? - optional "!"
    (...) - captures everything inside for later use
    \d - any digit
    {17-19} - 17 to 19 of the preceding character (\d)
    > - matches this character literally
    

    您可以使用以下代码执行匹配:

    const match = args[0].match(/<@!?(\d{17,19})>/);
    

    如果没有找到,这将返回null。否则,它将返回具有此结构的数组:

    [
      '<@!id_here>',
      'id_here',
      index: 0,
      input: '<@!id_here>',
      groups: undefined
    ]
    

    因此,要获取 ID,只需获取第二个元素(在索引 1 处)

    // I'm using the optional chaining operator (?.) in the below
    // example, which requires node 14.0.0
    // if you do not have this version, just separate the `match()` result
    // into a separate variable
    // and validate it exists before accessing match[1]
    let unbanned1 = args[0].match(/<@!?(\d{17,19})>/)?.[1] || args[0];
    

    【讨论】:

      【解决方案2】:

      如果用户不在公会中,我认为不可能从提及中获取用户的 ID。因为它们不是GuildMembers,所以你不能提及它们。 (是的,有一种方法可以通过使用他们的 id 来提及不在公会中的用户,但我认为 discord.js 不认为这是一个有效的提及。)

      很好的解决方法是使用BanInfo,因为它包含User 对象。从那里获取id。如果要通过用户名解禁用户,可以将BanInfoUserusername属性与发送解禁命令的人指定的用户名进行比较。

      但请注意,用户名不是唯一的,因此最好也使用 Userdiscriminator 属性。

      【讨论】:

        猜你喜欢
        • 2013-08-23
        • 1970-01-01
        • 2021-07-07
        • 2020-07-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-04
        • 1970-01-01
        • 2010-09-23
        相关资源
        最近更新 更多