Getting ID from tagged user

好久不见. 提交于 2019-12-11 07:49:13

问题


Hey I was wondering if I can check if someone is being tagged and if they are get the user ID of the person tagged and use it as reporting

let [cmd, user, proof, reason] = msg.content.split(' ');
        let reporting = user //user being reported (usually tagged)
        let reported = msg.author.tag
        let reportedID = msg.author.id

let embedReply = new Discord.RichEmbed()
        .setColor("PURPLE")
        .setTitle("Ready to send?")
        .setDescription("Please check if this is correct:")
        .addField("Your name:", `${reported} (${reportedID})`)
        .addField("You are reporting:", `${reporting} (${reporting.id})`)
        .addField("With the proof:", proof)
        .addField("With the reason:", reason)
        .setFooter("Please check this report so you know what you're sending.")

回答1:


I am the author of the npm package shown in this answer. The sole reason for including it is for its ease of use and application.

You could use my discord-mentions package to extract the desired mention out of the string. See the package page for more details and specific usage.

Example:

// Require the package.
const getMention = require('discord-mentions'); // Don't forget to install.

// Extract any mentions from 'reporting.'
let mention = getMention(reporting, message.guild);

// If there is a mention, assign the member to 'reporting.' Otherwise, search for them by tag.
if (mention) reporting = mention.member;
else reporting = message.guild.members.find(m => m.user.tag === reporting);

// If the mention was not of a (valid) member, or none could be found, return an error.
if (!reporting) {
  return message.channel.send(':x: Unknown user. Use a mention or their tag.')
    .catch(console.error);
}

// 'reporting' is now a GuildMember. Use 'reporting.user.id' for their ID.


来源:https://stackoverflow.com/questions/56760250/getting-id-from-tagged-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!