Discord.Net
Discord.Net copied to clipboard
[Bug]: NullReference on SendMessageAsync
Check The Docs
- [X] I double checked the docs and couldn't find any useful information.
Verify Issue Source
- [X] I verified the issue was caused by Discord.Net.
Check your intents
- [X] I double checked that I have the required intents.
Description
Get an nullreference error when execute SendMessageAsync
Version
3.7.2
Working Version
No response
Logs
at Discord.Rest.RestDMChannel..ctor(BaseDiscordClient discord, UInt64 id, UInt64 recipientId)
at Discord.Rest.RestDMChannel.Create(BaseDiscordClient discord, Channel model)
at Discord.Rest.UserHelper.<CreateDMChannelAsync>d__3.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at Discord.Rest.RestUser.<Discord-IUser-CreateDMChannelAsync>d__57.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable
1.ConfiguredTaskAwaiter.GetResult()
at Discord.UserExtensions.<SendMessageAsync>d__0.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at FlyffUniverseTools.Core.Services.DiscordBotService.<SendPrivateMessage>d__7.MoveNext()
in C:\Users\Kevin\source\repos\FlyffUniverseTools\FlyffUniverseTools.Core\Services\DiscordBotService.cs:line 123
Sample
DiscordSocketClient _client = null;
private async Task<DiscordSocketClient> GetSocketClient()
{
if (_client == null)
{
_client = new DiscordSocketClient();
}
if (_client.LoginState == LoginState.LoggedOut)
{
await _client.LoginAsync(TokenType.Bot, "XXX");
await _client.StartAsync();
}
return _client;
}
public async Task SendPrivateMessage(ulong recieverUser)
{
var client = await GetSocketClient();
var user= await _client.GetUserAsync(recieverUser);
await user.SendMessageAsync("Hello :)");
}
Someone can help me? I want to send a private message to an user by userid.
Packages
A couple of things are wrong or unclear with this example.
1: You cannot send messages to users the bot does not share a guild with. Are you certain that they are sharing? 2: The user appears to be null, is the ID correct? 3: Have you verified the client has been started successfully? 4: The rest client is not ready to be used before the _client.Ready event. A viable reason for not fetching the user may be that it is not ready to make the call yet.
Please follow up on my suggestion. If any of these issues were the cause and are now resolved, please close the issue.
Closed by lack of followup.