【问题标题】:Discord.Net Refresh user roleDiscord.Net 刷新用户角色
【发布时间】:2021-03-16 02:11:46
【问题描述】:

所以,我在 Discord.Net 上工作,想了解用户是否具有特定角色
我写了这段代码:

public bool IsAlreadyHasAffiliation(SocketGuildUser user)
{
    IReadOnlyCollection<SocketRole> roles = user.Roles;
    foreach(SocketRole role in roles)
    {
        if (SpecifiedRoles.ContainsValue(role.Id)) return true;
    }
    return false;
}

它工作正常,但有一些问题:在机器人启动后,没有得到机器人添加的角色。

例如,在我通过机器人添加“Role1”并执行上述功能后,它就像用户没有 Role1 一样工作。 如何获取机器人角色的实时更新?

【问题讨论】:

标签: discord.net


【解决方案1】:

如果您注意到与用户相关的奇怪行为(您就是这样),那么这很可能是由于最近的 Privileged Intents 更新所致。 Discord.NET 缓存(下载)用户并使用 GuildUserUpdated 等事件在后台保持此缓存最新。如果没有 Guild Members 意图,Discord.NET 无法使其用户缓存保持最新,从而导致诸如此类的问题。

要解决此问题,请在 Discord 开发者门户上您的机器人页面的机器人选项卡上启用公会成员特权意图。

如果这不起作用,请使用 Discord.NET 的夜间版本并在 DiscordSocketConfig 中指定您需要的所有意图。要使用每晚版本,请在 NuGet 包管理器上添加 https://www.myget.org/F/discord-net/api/v3/index.json 作为包源。

这是一个 DiscordSocketConfig,它指定网关意图(仅在夜间可用):

new DiscordSocketConfig 
{
    GatewayIntents = 
        GatewayIntents.Guilds |
        GatewayIntents.GuildMembers |
        GatewayIntents.GuildMessageReactions | 
        GatewayIntents.GuildMessages | 
        GatewayIntents.GuildVoiceStates,
    ...
});

【讨论】:

    猜你喜欢
    • 2017-05-31
    • 2021-02-24
    • 2021-05-05
    • 2019-02-08
    • 2019-09-22
    • 1970-01-01
    • 2021-08-04
    • 2021-04-22
    • 1970-01-01
    相关资源
    最近更新 更多