有没有一种方法可以在用户不在公会的情况下,从一条信息中获取用户id?

o2gm4chl  于 2021-09-13  发布在  Java
关注(0)|答案(2)|浏览(220)

我试图通过使用提及来发出unban命令。如果有一种方法可以通过提及获得用户的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);
    },
};

先谢谢你。

3wabscal

3wabscal1#

您可以使用正则表达式从用户提及模式中匹配并捕获用户id,如下所示: /<@!?(\d{17,19})>/ ```
<@ - 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];

1wnzp6jl

1wnzp6jl2#

我认为如果用户不在公会中,就不可能从提及中获得用户id。因为他们不是 GuildMember 是的,你不能提及他们(是的,有一种方法可以通过使用用户id来提及不在公会中的用户,但是 discord.js 我想我不认为这是一个有效的说法。)
很好的解决方法是使用 BanInfo 因为它包含一个 User 对象得到 id 从那里开始。如果要按用户名取消对用户的绑定,可以比较 username 财产 User 在里面 BanInfo 使用发送unban命令的人员指定的用户名。
但是请注意,用户名不是唯一的,因此也可以使用 discriminator 性质 User .

相关问题