Skip to content

Commit

Permalink
fix: Create DM channel with id and author alone (#1850)
Browse files Browse the repository at this point in the history
* Create DM channel with id and author alone

* Unneeded cast
  • Loading branch information
SubZero0 committed May 24, 2021
1 parent 75b74e1 commit 95bae78
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 38 deletions.
83 changes: 45 additions & 38 deletions src/Discord.Net.WebSocket/DiscordSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,56 +1236,63 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_CREATE)").ConfigureAwait(false);

var data = (payload as JToken).ToObject<API.Message>(_serializer);
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel;

var guild = (channel as SocketGuildChannel)?.Guild;
if (guild != null && !guild.IsSynced)
{
var guild = (channel as SocketGuildChannel)?.Guild;
if (guild != null && !guild.IsSynced)
{
await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
return;
}
await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
return;
}

SocketUser author;
if (guild != null)
{
if (data.WebhookId.IsSpecified)
author = SocketWebhookUser.Create(guild, State, data.Author.Value, data.WebhookId.Value);
else
author = guild.GetUser(data.Author.Value.Id);
}
SocketUser author;
if (guild != null)
{
if (data.WebhookId.IsSpecified)
author = SocketWebhookUser.Create(guild, State, data.Author.Value, data.WebhookId.Value);
else
author = (channel as SocketChannel).GetUser(data.Author.Value.Id);
author = guild.GetUser(data.Author.Value.Id);
}
else
author = (channel as SocketChannel)?.GetUser(data.Author.Value.Id);

if (author == null)
if (author == null)
{
if (guild != null)
{
if (guild != null)
if (data.Member.IsSpecified) // member isn't always included, but use it when we can
{
if (data.Member.IsSpecified) // member isn't always included, but use it when we can
{
data.Member.Value.User = data.Author.Value;
author = guild.AddOrUpdateUser(data.Member.Value);
}
else
author = guild.AddOrUpdateUser(data.Author.Value); // user has no guild-specific data
data.Member.Value.User = data.Author.Value;
author = guild.AddOrUpdateUser(data.Member.Value);
}
else if (channel is SocketGroupChannel)
author = (channel as SocketGroupChannel).GetOrAddUser(data.Author.Value);
else
{
await UnknownChannelUserAsync(type, data.Author.Value.Id, channel.Id).ConfigureAwait(false);
return;
}
author = guild.AddOrUpdateUser(data.Author.Value); // user has no guild-specific data
}

var msg = SocketMessage.Create(this, State, author, channel, data);
SocketChannelHelper.AddMessage(channel, this, msg);
await TimedInvokeAsync(_messageReceivedEvent, nameof(MessageReceived), msg).ConfigureAwait(false);
else if (channel is SocketGroupChannel groupChannel)
author = groupChannel.GetOrAddUser(data.Author.Value);
else
author = State.GetOrAddUser(data.Author.Value.Id, x => SocketGlobalUser.Create(this, State, data.Author.Value));
}
else

if (channel == null)
{
await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
return;
if (!data.GuildId.IsSpecified) // assume it is a DM
{
var dm = SocketDMChannel.Create(this, State, data.ChannelId, data.Author.Value);
channel = dm;
State.AddChannel(dm);
dm.Recipient.GlobalUser.DMChannel = dm;
}
else
{
await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
return;
}
}

var msg = SocketMessage.Create(this, State, author, channel, data);
SocketChannelHelper.AddMessage(channel, this, msg);
await TimedInvokeAsync(_messageReceivedEvent, nameof(MessageReceived), msg).ConfigureAwait(false);
}
break;
case "MESSAGE_UPDATE":
Expand Down
10 changes: 10 additions & 0 deletions src/Discord.Net.WebSocket/Entities/Channels/SocketDMChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ internal override void Update(ClientState state, Model model)
{
Recipient.Update(state, model.Recipients.Value[0]);
}
internal static SocketDMChannel Create(DiscordSocketClient discord, ClientState state, ulong channelId, API.User recipient)
{
var entity = new SocketDMChannel(discord, channelId, discord.GetOrCreateUser(state, recipient));
entity.Update(state, recipient);
return entity;
}
internal void Update(ClientState state, API.User recipient)
{
Recipient.Update(state, recipient);
}

/// <inheritdoc />
public Task CloseAsync(RequestOptions options = null)
Expand Down

0 comments on commit 95bae78

Please sign in to comment.