fix: Create DM channel with id and author alone (#1850)

* Create DM channel with id and author alone

* Unneeded cast
This commit is contained in:
Paulo
2021-05-24 17:19:11 -03:00
committed by GitHub
parent 75b74e1a3f
commit 95bae786b8
2 changed files with 64 additions and 47 deletions

View File

@@ -1236,8 +1236,8 @@ namespace Discord.WebSocket
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_CREATE)").ConfigureAwait(false); await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_CREATE)").ConfigureAwait(false);
var data = (payload as JToken).ToObject<API.Message>(_serializer); 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; var guild = (channel as SocketGuildChannel)?.Guild;
if (guild != null && !guild.IsSynced) if (guild != null && !guild.IsSynced)
{ {
@@ -1254,7 +1254,7 @@ namespace Discord.WebSocket
author = guild.GetUser(data.Author.Value.Id); author = guild.GetUser(data.Author.Value.Id);
} }
else else
author = (channel as SocketChannel).GetUser(data.Author.Value.Id); author = (channel as SocketChannel)?.GetUser(data.Author.Value.Id);
if (author == null) if (author == null)
{ {
@@ -1268,11 +1268,24 @@ namespace Discord.WebSocket
else else
author = guild.AddOrUpdateUser(data.Author.Value); // user has no guild-specific data author = guild.AddOrUpdateUser(data.Author.Value); // user has no guild-specific data
} }
else if (channel is SocketGroupChannel) else if (channel is SocketGroupChannel groupChannel)
author = (channel as SocketGroupChannel).GetOrAddUser(data.Author.Value); author = groupChannel.GetOrAddUser(data.Author.Value);
else
author = State.GetOrAddUser(data.Author.Value.Id, x => SocketGlobalUser.Create(this, State, data.Author.Value));
}
if (channel == null)
{
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 else
{ {
await UnknownChannelUserAsync(type, data.Author.Value.Id, channel.Id).ConfigureAwait(false); await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
return; return;
} }
} }
@@ -1281,12 +1294,6 @@ namespace Discord.WebSocket
SocketChannelHelper.AddMessage(channel, this, msg); SocketChannelHelper.AddMessage(channel, this, msg);
await TimedInvokeAsync(_messageReceivedEvent, nameof(MessageReceived), msg).ConfigureAwait(false); await TimedInvokeAsync(_messageReceivedEvent, nameof(MessageReceived), msg).ConfigureAwait(false);
} }
else
{
await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
return;
}
}
break; break;
case "MESSAGE_UPDATE": case "MESSAGE_UPDATE":
{ {

View File

@@ -49,6 +49,16 @@ namespace Discord.WebSocket
{ {
Recipient.Update(state, model.Recipients.Value[0]); 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 /> /// <inheritdoc />
public Task CloseAsync(RequestOptions options = null) public Task CloseAsync(RequestOptions options = null)