javascript 在不共享同一服务器的 discord.js 中获取用户的头像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48483338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:04:13  来源:igfitidea点击:

Get avatar of an user in discord.js that doesn't share the same server

javascriptnode.jsdiscorddiscord.js

提问by Xanar

I have a small problem with my bot. For developing my bot, I made a new dev-bot which shares the same code as my normal bot, but has it's own token. However, I ran into a small issue while developing.

我的机器人有一个小问题。为了开发我的机器人,我制作了一个新的 dev-bot,它与我的普通机器人共享相同的代码,但有自己的令牌。但是,我在开发过程中遇到了一个小问题。

I use this code, to get someones' avatar:

我使用此代码来获取某人的头像:

client.users.get(event.user.uid).avatarURL

This works fine on my normal but, however on my Dev-Bot I get this error message:

这在我的正常情况下工作正常,但是在我的 Dev-Bot 上我收到此错误消息:

Error getting documents TypeError: Cannot read property 'AvatarURL' of undefined

I think it's due to the fact, that my Bot can't access the avatar of the user, because it doesn't share the same server/guild as this user.

我认为这是因为我的 Bot 无法访问用户的头像,因为它与该用户不共享相同的服务器/公会。

Is there any workaround I could use?

我可以使用任何解决方法吗?

回答by FireController1847

Due to Discord.js and their way of caching, not all users are going to be cached in the bot. While there is a small possibility of it actually not knowing anything about the user, there is a large chance that the Discord API will still allow you to get information form it.

由于 Discord.js 及其缓存方式,并非所有用户都会被缓存在机器人中。虽然它实际上对用户一无所知的可能性很小,但 Discord API 很有可能仍然允许您从它那里获取信息。

To fix this issue with caching in the latest master, we have to use Client.users, which returns a UserStore. Within the UserStore, we can use a method called fetchto get information about a user.

为了通过缓存最新的 master解决这个问题,我们必须使用 Client.users,它返回一个UserStore。在 UserStore 中,我们可以使用一个名为fetch的方法来获取有关用户的信息。

To fix this issue in the latest stable, we have to use a method called Client.fetchUser, which does the same thing but returns a User instead of a UserStore.

在最新的 stable 中解决此问题,我们必须使用一种称为 Client 的方法。fetchUser,它做同样的事情,但返回一个 User 而不是 UserStore。

Please note this is only available using a bot account. Here's an example of its usage:

请注意,这仅适用于机器人帐户。下面是它的用法示例:

// Using Discord.js Stable
bot.fetchUser(theUsersID).then(myUser => {
    console.log(myUser.avatarURL); // My user's avatar is here!
});

// Using Discord.js Master
bot.users.fetch(theUsersID).then(myUser => {
    console.log(myUser.avatarURL()); // My user's avatar is here!
});

If there is an error fetching the user (say, a DiscordAPI Permissions Error), this means that there is no way for your bot to get the user's avatar without knowing who the user is first (or sharing a Guild with the user). Happy coding!

如果在获取用户时出现错误(例如 DiscordAPI 权限错误),这意味着您的机器人无法在不知道用户是谁(或与用户共享公会)的情况下获取用户的头像。快乐编码!