检查用户是否具有角色 Discord.net
Check if user has role 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!");
}
}
}
我的目标
我想监控是否有人在公会的任何聊天中写了“!创建”,然后检查发送消息的人是否具有名为“游戏通知”的角色(ID:772788208500211724)。
如果此人确实具有角色,则应将其输出到发送原始消息的频道:
"The user '<Username>' already has the role '<RoleName>'!"
如果此人没有角色,则应为其赋予角色并将其输出到发送原始消息的频道:
"Added Role '<RoleName>' to '<Username>'!"
我的问题
如果我 启动机器人 当我 没有角色 并且我写 !create one chat 它成功 给我这个角色。当我 第二次执行命令时,它又给了我角色 。不是说我已经有这个角色了。
反之亦然:
如果我在启动 bot 并正确执行命令时 拥有角色,它会 说我拥有角色 。当我现在手动删除我的角色并再次执行命令时,它仍然说我有角色.
有什么解决办法吗?
Using Discord.net Nu-Get Packet v2.2.0
首先,您应该使用 built-in commands service 而不是您当前的方法。但是,这样做并不能解决您的问题。
如果您注意到与用户相关的奇怪行为(您确实如此),那么很可能是最近的特权意图更新所致。 Discord.NET 缓存(下载)用户并使用诸如 GuildUserUpdated 之类的事件来使此缓存在后台保持最新。没有公会成员的意图,Discord.NET 无法保持其用户缓存是最新的,从而导致诸如此类的问题。
要解决此问题,请在 Discord 开发人员门户上的机器人页面的机器人选项卡上启用公会成员特权意图。
如果这不起作用,则使用 Discord.NET 的夜间版本,并在 DiscordSocketConfig. To use the nightly versions, add https://www.myget.org/F/discord-net/api/v3/index.json 中指定您需要的所有意图作为 NuGet 包管理器上的包源。
这是我的 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 频道中询问。
我正在尝试 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!");
}
}
}
我的目标
我想监控是否有人在公会的任何聊天中写了“!创建”,然后检查发送消息的人是否具有名为“游戏通知”的角色(ID:772788208500211724)。 如果此人确实具有角色,则应将其输出到发送原始消息的频道:
"The user '<Username>' already has the role '<RoleName>'!"
如果此人没有角色,则应为其赋予角色并将其输出到发送原始消息的频道:
"Added Role '<RoleName>' to '<Username>'!"
我的问题
如果我 启动机器人 当我 没有角色 并且我写 !create one chat 它成功 给我这个角色。当我 第二次执行命令时,它又给了我角色 。不是说我已经有这个角色了。
反之亦然: 如果我在启动 bot 并正确执行命令时 拥有角色,它会 说我拥有角色 。当我现在手动删除我的角色并再次执行命令时,它仍然说我有角色.
有什么解决办法吗?
Using Discord.net Nu-Get Packet v2.2.0
首先,您应该使用 built-in commands service 而不是您当前的方法。但是,这样做并不能解决您的问题。
如果您注意到与用户相关的奇怪行为(您确实如此),那么很可能是最近的特权意图更新所致。 Discord.NET 缓存(下载)用户并使用诸如 GuildUserUpdated 之类的事件来使此缓存在后台保持最新。没有公会成员的意图,Discord.NET 无法保持其用户缓存是最新的,从而导致诸如此类的问题。
要解决此问题,请在 Discord 开发人员门户上的机器人页面的机器人选项卡上启用公会成员特权意图。
如果这不起作用,则使用 Discord.NET 的夜间版本,并在 DiscordSocketConfig. To use the nightly versions, add https://www.myget.org/F/discord-net/api/v3/index.json 中指定您需要的所有意图作为 NuGet 包管理器上的包源。
这是我的 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 频道中询问。