英文:
How can I get a user via UserID in a slash command?
问题
我一直在编写一个Discord机器人,根据需要参考教程,因为我是新手,但我遇到了一个问题,听起来很简单,但似乎无法解决。
我正在尝试绘制一个排行榜,在排行榜上的每个位置上,我想获取用户的头像来绘制在排行榜上。为此,我希望能够仅通过用户ID获取用户。但是,我是在一个单独的斜杠命令中执行所有这些操作。以下是我尝试做的事情的非常简化版本,以提供一些上下文和/或更好的解释:
const { SlashCommandBuilder, AttachmentBuilder } = require('discord.js');
// ... 这里放一些函数等等
module.exports = {
//... 设置命令的行代码在这里
async execute(interaction){
const data = fetchData(); //获取整个数据列表的函数;没有将其放在代码块中以节省空间,因为这不是必要的
// ... 一堆操作,对数据进行排序等等
data.forEach(async (userData, index) => {
// 这里开始了我的问题。下面的这一行只是我尝试过的内容
const target = interaction.guild.members.cache.get(userData.UserId);
});
};
}
我已经进行了相当多的研究,我找到的唯一可行的解决方案(如上面的示例代码中所示)是执行以下操作:
const target = interaction.guild.members.cache.get("User_ID");
但是,即使我可以在控制台中记录返回值,如果我尝试执行像"target.user"之类的操作,它会说target未定义。而且,如果有帮助的话,是的,我在我的意图中有GuildMembers。
英文:
I have been programming a discord bot, following tutorials as needed since I am quite new, but I have come across a problem that, as simple as it sounds, I can't seem to solve.
I am trying to draw a leaderboard, and for each placement on the leaderboard I want to get the user's avatar to draw onto the leaderboard. To do this, I want to be able to get the user through just the UserId. However, I am doing all of this in a separate slash command. Here is a very simplified version of what I'm trying to do to give some context and/or maybe a better explanation:
const { SlashCommandBuilder, AttachmentBuilder } = require('discord.js');
// ... some functions and what not go here
module.exports = {
//... lines for setting up the command go here
async execute(interaction){
const data = fetchData(); //A function that gets the whole list of data; did not put it in the codeblock to save on space since it's not necessary
// ... a bunch of stuff, ordering my data, etc.
data.forEach(async (userData, index) => {
// And here begins my issue. The line below is just what I have attempted to do
const target = interaction.guild.members.cache.get(userData.UserId);
});
};
}
I have done a fair bit of research, and the only feasible solution I could find (which you can see is in the above example code) is to do
const target = interaction.guild.members.cache.get("User_ID");
However, even though I can log the return in the console, if I tried to do something like "target.user" it would say target is undefined. And, in case it helps, yes, I do have GuildMembers in my intents.
答案1
得分: 0
你需要使用 members.fetch
,因为它是异步的,你需要将你的 forEach
改为 forof
,因为 forEach
是同步的。以下是代码示例:
const { SlashCommandBuilder, AttachmentBuilder } = require('discord.js');
// ... 一些函数和其他内容放在这里
module.exports = {
// ... 设置命令的行代码放在这里
async execute(interaction) {
const data = fetchData(); //获取整个数据列表的函数;为了节省空间,我没有将它放在代码块中,因为这不是必需的
// ... 大量的操作,对数据进行排序等等
for (const userData of data) {
const target = await interaction.guild.members.fetch(userData.UserId)
}
};
}
英文:
You need to use members.fetch
since it's async you need to change your forEach
to forof
because forEach
is synchronous
const { SlashCommandBuilder, AttachmentBuilder } = require('discord.js');
// ... some functions and what not go here
module.exports = {
//... lines for setting up the command go here
async execute(interaction) {
const data = fetchData(); //A function that gets the whole list of data; did not put it in the codeblock to save on space since it's not necessary
// ... a bunch of stuff, ordering my data, etc.
for (const userData of data) {
const target = await interaction.guild.members.fetch(userData.UserId)
}
};
}
答案2
得分: -1
如果您只想获取运行斜杠命令的用户的ID,您可以使用interaction.user.id
。
要通过ID获取用户,您可以运行:
//在斜杠命令的execute函数内部
interaction.guild.members.cache.get("user_id").then(function(user){
//与用户执行某些操作
}
英文:
If you just want to get the id of the user that ran the slash command, you can use interaction.user.id
.
To get a user by id, you can run:
//Inside the execute function of the slash command
interaction.guild.members.cache.get("user_id").then(function(user){
//Do something with user
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论