【问题标题】:Post mentioned user's avatar embedded using Discord.js帖子提到使用 Discord.js 嵌入的用户头像
【发布时间】:2020-05-20 16:54:39
【问题描述】:

我正在使用 Discord.js 作为一个不和谐的机器人,我正在努力做到这一点,以便当你执行命令 !avatar @user

它会回复所提到用户头像的嵌入图像。我不断得到:

(node:3168) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body embed.image.url: Not a well formed URL.

这是我目前的代码,但是我不知道还有什么方法可以获取用户的头像?

    const Discord = require('discord.js');
const config = require('./config.json');
const client = new Discord.Client();

function getUserFromMention(mention) {
    if (!mention) return;
    if (mention.startsWith('<@') && mention.endsWith('>')) {
        mention = mention.slice(2, -1);
        if (mention.startsWith('!')) {
            mention = mention.slice(1);
        }
        return client.users.get(mention);
    }
}

function getUserFromMentionRegEx(mention) {
    const matches = mention.match(/^<@!?(\d+)>$/);  
    const id = matches[1];
    return client.users.get(id);
}

client.once('ready', () => {
    console.log('Ready!');
});

const prefix = "!";
client.on('message', message => {
    if (!message.content.startsWith(prefix)) return;  
    const withoutPrefix = message.content.slice(prefix.length);
    const split = withoutPrefix.split(/ +/);
    const command = split[0];
    const args = split.slice(1);

    if (command === 'avatar') {
        if (args[0]) {          
            const user = getUserFromMention(args[0]);
            const userAvatar = user.displayAvatarURL;
            if (!user) {
                return message.reply('Please use a proper mention if you want to see someone else\'s avatar.');
            }
            const avatarEmbed = new Discord.RichEmbed()
            .setColor('#275BF0')
            .setImage('userAvatar');
            message.channel.send(avatarEmbed);
        }

        return message.channel.send(`${message.author.username}, your avatar: ${message.author.displayAvatarURL}`);
    }
});

client.login(config.token);

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    你不需要用另一个函数来解析提及,discord 有这个方法。你可以使用message.mentions.members.first()

    您尝试将图像设置为一个字符串,其中包含文本userAvatar,所以确定您遇到了错误。

    您可以获得提及会员头像并使用此代码发送。

    const Discord = require('discord.js');
    const config = require('./config.json');
    const client = new Discord.Client();
    
    
    client.once('ready', () => {
        console.log('Ready!');
    });
    
    const prefix = "!";
    
    client.on('message', message => {
        if (!message.content.startsWith(prefix)) return;  
        const withoutPrefix = message.content.slice(prefix.length);
        const split = withoutPrefix.split(/ +/);
        const command = split[0];
        const args = split.slice(1);    
    
    
    
        if (command === 'avatar') {
            let targetMember;
            if(!message.mentions.members.first()) {
                targetMember = message.guild.members.get(message.author.id);
            } else {
                targetMember = message.mentions.members.first()
            }
    
            let avatarEmbed = new Discord.RichEmbed()
                .setImage(targetMember.user.displayAvatarURL)
                .setColor(targetMember.displayHexColor);
                message.channel.send(avatarEmbed);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-08-01
      • 2020-12-19
      • 2019-08-03
      • 2021-06-25
      • 2020-12-21
      • 2020-07-26
      • 2020-11-21
      • 2021-02-08
      • 2017-09-13
      相关资源
      最近更新 更多