检查用户是否作为自定义状态/关于我的链接



我想创建一个命令,返回服务器的所有用户在他们的自定义状态/关于我的链接。我试着在谷歌上搜索,但什么也没找到。非常感谢:)

我通常不会像这样填塞别人的代码,但我希望你把这作为一个机会,停下来,试着理解代码是如何工作的以及为什么。

我留下了一些有用的注释来指导您完成代码。

重要:要运行下面的代码,您需要使用这些intent:Intents.FLAGS.GUILD_MEMBERSIntents.FLAGS.GUILD_PRESENCES,并在应用程序仪表板 中启用它们
// Get all members in the guild and let discord.js know we want to have their presence in the result
const members = await msg.guild.members.fetch({ withPresences: true });
// Loop through the members and call the function for every member inside the members list
members.forEach(async (member) => {
if (
// Does the user have a presence? if not hes maybe offline?
!member.presence ||
// User has a presence, check if he has any activities on his profile
member.presence.activities.length <= 0 ||
// The first activity is always a custom status, so we can check if user has a custom status set
member.presence.activities[0].id != 'custom'
)
// If any of the above is true, we can skip this member
return;
// Get their custom status
const activity = member.presence.activities[0];
// Check if activity.state has a valid url
if (
// Check if the activity has a state, might be undefined
!activity.state ||
// Check if the activity state has a url inside it
!activity.state.match(/^https?:///)
)
return;
// Here we can do whatever we want! For now we only log this to the console
console.log(`${member.user.username} has custom status with url: ${activity.state}`);
});

最新更新