微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

检查用户是否具有Discord.net角色

如何解决检查用户是否具有Discord.net角色

我正在使用discord.net,但我无法使此代码正常工作...

我的代码

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using discord;
using discord.WebSocket;

namespace NetflixManager
{
    class Program
    {
        private readonly discordSocketClient _client;

        static void Main(string[] args)
        {
            new Program().MainAsync().GetAwaiter().GetResult();
        }

        public Program()
        {
            _client = new discordSocketClient();

            _client.Log += LogAsync;
            _client.Ready += ReadyAsync;
            _client.MessageReceived += MessageReceivedAsync;
        }

        public async Task MainAsync()
        {
            await _client.LoginAsync(TokenType.Bot,File.ReadAllText("Token.txt"));
            await _client.StartAsync();

            await Task.Delay(-1);
        }

        private Task LogAsync(LogMessage log)
        {
            Console.WriteLine(log.ToString());
            return Task.CompletedTask;
        }

        private Task ReadyAsync()
        {
            Console.WriteLine($"{_client.CurrentUser} is connected!");

            return Task.CompletedTask;
        }

        private async Task MessageReceivedAsync(SocketMessage message)
        {
            // The bot should never respond to itself.
            if (message.Author.Id == _client.CurrentUser.Id)
                return;
            // The bot should not reply to private messages
            if (message.Channel.Name.StartsWith("@"))
                return;
            // The bot should not reply to bots
            if (message.Author.IsBot)
                return;
            // The bot should not reply to a webhook
            if (message.Author.IsWebhook)
                return;
            // Commands
            if (message.Content.StartsWith("!create"))
            {
                if (message.Author is SocketGuildUser socketUser)
                {
                    SocketGuild socketGuild = socketUser.Guild;
                    SocketRole socketRole = socketGuild.GetRole(772788208500211724);
                    if (socketUser.Roles.Any(r => r.Id == socketRole.Id))
                    {
                        await message.Channel.SendMessageAsync("The user '" + socketUser.Username + "' already has the role '" + socketRole.Name + "'!");
                    }
                    else
                    {
                        await socketUser.AddRoleAsync(socketRole);
                        await message.Channel.SendMessageAsync("Added Role '" + socketRole.Name + "' to '" + socketUser.Username + "'!");
                    }
                }
            }

            if (message.Content == "!ping")
                await message.Channel.SendMessageAsync("pong!");
        }
    }
}

我的目标

我想监视某人是否在行会的任何聊天中都写“!create”,然后检查发送该消息的人是否具有称为“游戏通知”的角色(Id:772788208500211724)。 如果此人确实有该角色,则应将此角色输出到原始消息发送到的通道:

"The user '<Username>' already has the role '<RoleName>'!"

如果此人没有该角色,则应给该人该角色,并将其输出到发送原始消息的通道:

"Added Role '<RoleName>' to '<Username>'!"

我的问题

如果我没有角色时我启动机器人,并且我写了!创建一个聊天记录,它会赋予我角色 。当我第二次执行命令时,它再次赋予了我角色。并不是说我已经担任过这个角色。

它也可以以其他方式工作: 如果我在启动机器人时担任角色并执行命令,则它正确地表示我具有该角色。现在,当我手动删除角色并再次执行命令时,它仍然说出我具有该角色

有什么办法解决这个问题吗?

使用discord.net Nu-Get数据包v2.2.0

解决方法

首先,应该使用built-in commands service而不是当前的方法。但是,这样做无法解决您的问题。

如果您注意到与用户有关的奇怪行为,那么很可能是由于最近的“特权意图”更新造成的。 Discord.NET缓存(下载)用户并使用诸如GuildUserUpdated之类的事件来使此缓存在后台保持最新状态。没有公会成员的意图,Discord.NET无法使用户缓存保持最新状态,从而导致诸如此类的问题。

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

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

这是我的DiscordSocketConfig,它指定网关意图(仅每晚提供):

new DiscordSocketConfig 
{
    TotalShards = _totalShards,MessageCacheSize = 0,ExclusiveBulkDelete = true,AlwaysDownloadUsers = _config.FillUserCache,LogLevel = Discord.LogSeverity.Info,GatewayIntents = 
        GatewayIntents.Guilds |
        GatewayIntents.GuildMembers |
        GatewayIntents.GuildMessageReactions | 
        GatewayIntents.GuildMessages | 
        GatewayIntents.GuildVoiceStates
});

如果您需要更多帮助,建议您加入Unofficial Discord API server并在#dotnet-discord-net频道中提问。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。