Browse Source

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

* Create DM channel with id and author alone

* Unneeded cast
tags/3.0.0
Paulo GitHub 4 years ago
parent
commit
95bae786b8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 38 deletions
  1. +45
    -38
      src/Discord.Net.WebSocket/DiscordSocketClient.cs
  2. +10
    -0
      src/Discord.Net.WebSocket/Entities/Channels/SocketDMChannel.cs

+ 45
- 38
src/Discord.Net.WebSocket/DiscordSocketClient.cs View File

@@ -1236,56 +1236,63 @@ namespace Discord.WebSocket
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":


+ 10
- 0
src/Discord.Net.WebSocket/Entities/Channels/SocketDMChannel.cs View File

@@ -49,6 +49,16 @@ namespace Discord.WebSocket
{
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)


Loading…
Cancel
Save