Skip to content

Commit

Permalink
Fix: RestDMChannel relies on recipient (#2910)
Browse files Browse the repository at this point in the history
* don't assume we have a DM user

* also check nullable for Update/GetUser

* Update src/Discord.Net.Rest/Entities/Channels/RestDMChannel.cs

Co-authored-by: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com>

---------

Co-authored-by: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com>
  • Loading branch information
quinchs and Misha-133 authored Apr 15, 2024
1 parent 397a887 commit 13ea566
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Discord.Net.Rest/Entities/Channels/RestDMChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,26 @@ public class RestDMChannel : RestChannel, IDMChannel, IRestPrivateChannel, IRest
/// <summary>
/// Gets a collection that is the current logged-in user and the recipient.
/// </summary>
public IReadOnlyCollection<RestUser> Users => ImmutableArray.Create(CurrentUser, Recipient);
public IReadOnlyCollection<RestUser> Users => Recipient is null
? ImmutableArray<RestUser>.Empty
: ImmutableArray.Create(CurrentUser, Recipient);

internal RestDMChannel(BaseDiscordClient discord, ulong id, ulong recipientId)
internal RestDMChannel(BaseDiscordClient discord, ulong id, ulong? recipientId)
: base(discord, id)
{
Recipient = new RestUser(Discord, recipientId);
Recipient = recipientId.HasValue ? new RestUser(Discord, recipientId.Value) : null;
CurrentUser = new RestUser(Discord, discord.CurrentUser.Id);
}
internal new static RestDMChannel Create(BaseDiscordClient discord, Model model)
{
var entity = new RestDMChannel(discord, model.Id, model.Recipients.Value[0].Id);
var entity = new RestDMChannel(discord, model.Id, model.Recipients.GetValueOrDefault(null)?[0].Id);
entity.Update(model);
return entity;
}
internal override void Update(Model model)
{
Recipient.Update(model.Recipients.Value[0]);
if(model.Recipients.IsSpecified)
Recipient?.Update(model.Recipients.Value[0]);
}

/// <inheritdoc />
Expand All @@ -68,7 +71,7 @@ public Task CloseAsync(RequestOptions options = null)
/// </returns>
public RestUser GetUser(ulong id)
{
if (id == Recipient.Id)
if (id == Recipient?.Id)
return Recipient;
else if (id == Discord.CurrentUser.Id)
return CurrentUser;
Expand Down

0 comments on commit 13ea566

Please sign in to comment.