Discord-Net-Labs/Discord.Net-Labs

SocketGuildUser.AddRoleAsync NullReferenceException

Closed this issue · 2 comments

source code :
[Command("test")]
public async Task test()
{
var role = Context.Guild.GetRole(roleid);
var user = Context.Guild.GetUser(Context.User.Id);
await user.AddRoleAsync(role);
await ReplyAsync("test");

    }

Exception Message:

---> System.NullReferenceException: Object reference not set to an instance of an object.
at Discord.Net.Queue.RequestBucket.SendAsync(RestRequest request)
at Discord.Net.Queue.RequestQueue.SendAsync(RestRequest request)
at Discord.API.DiscordRestApiClient.SendInternalAsync(String method, String endpoint, RestRequest request)
at Discord.API.DiscordRestApiClient.SendAsync(String method, String endpoint, BucketId bucketId, ClientBucketType clientBucket, RequestOptions options)
at Discord.API.DiscordRestApiClient.AddRoleAsync(UInt64 guildId, UInt64 userId, UInt64 roleId, RequestOptions options)
at Discord.Rest.UserHelper.AddRolesAsync(IGuildUser user, BaseDiscordClient client, IEnumerable`1 roleIds, RequestOptions options)
at DobakBot.GambleModule.test() in C:\Users\son\source\repos\DobakBot\DobakBot\Controller\GambleModule.cs:line 54
at Discord.Commands.ModuleClassBuilder.<>c__DisplayClass6_0.<g__ExecuteCallback|0>d.MoveNext()
--- End of stack trace from previous location ---
at Discord.Commands.CommandInfo.ExecuteInternalAsync(ICommandContext context, Object[] args, IServiceProvider services)

my bot permisson was admin
role and user was not null

csmir commented

Is your bot's highest (priority) role below the role you're trying to assign? If so, move it down to below the highest role your bot has. Even as administrator, users (so bot users as well) are not permitted to modify nor assign roles above their own highest role in the hierarchy.

it works!! thank you