When I try to pass a ulong discordId through await user.AddRoleAsync(role), I get the error that Object reference not set to an instance of an object, meaning that user is null.
当我尝试通过aWait user.AddRoleAsync(角色)传递一个ulong discordID时,我收到错误,对象引用未设置为对象的实例,这意味着该用户为空。
I have tried
我试过了
private async Task GiveRoleAsync(DiscordSocketClient client, ulong discordId, SocketGuild guild)
{
try
{
if (!string.IsNullOrEmpty(discordId.ToString()) && guild != null)
{
Console.WriteLine(discordId);
var role = guild.Roles.FirstOrDefault(x => x.Name == "Acolyte");
var roleId = role?.Id;
if (role != null)
{
var user = guild.GetUser(discordId);
if (user != null)
{
await user.AddRoleAsync(role);
}
else
{
Console.WriteLine("User not found in the guild.");
}
}
else
{
Console.WriteLine("Role not found.");
}
}
else
{
Console.WriteLine("User or Guild ID is null.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
However, I always receive the error that "User not found in the guild.", even when manually inserting my own discord id via the client.
As a note, I am trying to gather discordIds from a database and update roles according to certain parameters, and Console.WriteLine(discordId) works, but Console.WriteLine(user.Id) prints null.
Additionally, I have the Server Members Intent enabled as I am able to do something very similar using a select menu.
然而,我总是收到“用户在公会中找不到”的错误,即使当我通过客户端手动插入自己的不一致ID时也是如此。请注意,我正在尝试从数据库中收集discordID并根据某些参数更新角色,并且Console.WriteLine(DiscordId)可以工作,但Console.WriteLine(user.id)输出为空。此外,我启用了服务器成员意图,因为我可以使用选择菜单执行非常类似的操作。
private async Task GrabRolesHandler(SocketMessageComponent arg)
{
try
{
if (arg.User != null && arg.GuildId != null)
{
var text = (arg.Data.Values);
var userId = arg.User.Id;
Console.WriteLine(userId);
SocketGuild guild = _client.GetGuild(arg.GuildId.Value);
var role = guild.Roles.FirstOrDefault(x => x.Name == text);
var roleId = role?.Id;
if (role != null)
{
var user = guild.GetUser(userId);
if (user != null)
{
if (user.Roles.Contains(role))
{
await user.RemoveRoleAsync(role);
await arg.RespondAsync($"You have **disabled** pings for the <@&{roleId}> role", ephemeral: true);
}
else
{
await user.AddRoleAsync(role);
await arg.RespondAsync($"You have **enabled** pings for the <@&{roleId}> role", ephemeral: true);
}
}
else
{
Console.WriteLine("User not found in the guild.");
}
}
else
{
Console.WriteLine("Role not found.");
await arg.DeferAsync();
}
}
else
{
Console.WriteLine("User or Guild ID is null.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
更多回答
Discord developer portal->Bot->Privileged Gateway Intents. From here can u check "Server Members Intent"
不一致开发人员门户->僵尸->特权网关意图。从这里可以检查“服务器成员意图”吗?
!string.IsNullOrEmpty(discordId.ToString())
this is nonsense. It will always pass. ulong
is a value type, so it cannot be null and ToString()
on it will resolve to its default value represented as string. Thus: it cannot and will not ever be null or empty.
!string.IsNullOrEmpty(discordId.ToString())这是胡说八道。一切都会过去的。Ulong是一个值类型,因此它不能为空,并且它上的ToString()将解析为表示为字符串的默认值。因此:它不能也永远不会为空或空。
我是一名优秀的程序员,十分优秀!