[英]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 它成功地為我提供了角色。 當我第二次執行命令時,它又給了我這個角色。 這並不是說我已經有了這個角色。
它也可以反過來工作:如果我在啟動機器人並執行命令時有角色,它會正確地表示我有角色。 當我現在手動從我身上刪除角色並再次執行命令時,它仍然說我擁有該角色。
任何想法如何解決這一問題?
使用 Discord.net Nu-Get Packet v2.2.0
首先,您應該使用內置命令 service而不是您當前的方法。 但是,這樣做並不能解決您的問題。
如果您注意到與用戶有關的奇怪行為,那么很可能是由於最近的特權意圖更新。 Discord.NET 緩存(下載)用戶並使用 GuildUserUpdated 等事件在后台保持此緩存為最新。 如果沒有 Guild Members 的意圖,Discord.NET 就無法保持其用戶緩存是最新的,從而導致諸如此類的問題。
要解決此問題,請在 Discord 開發人員門戶上的機器人頁面的 Bot 選項卡上啟用 Guild Members 特權意圖。
如果不工作,然后用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
});
如果您需要更多幫助,我建議您加入非官方 Discord API 服務器並在 #dotnet-discord-net 頻道中詢問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.