Browse Source

Added a Ready event to DiscordShardedClient

pull/1385/head
Casino Boyale 6 years ago
parent
commit
89d7def6e7
2 changed files with 26 additions and 8 deletions
  1. +12
    -5
      src/Discord.Net.WebSocket/DiscordShardedClient.Events.cs
  2. +14
    -3
      src/Discord.Net.WebSocket/DiscordShardedClient.cs

+ 12
- 5
src/Discord.Net.WebSocket/DiscordShardedClient.Events.cs View File

@@ -7,32 +7,39 @@ namespace Discord.WebSocket
{ {
//General //General
/// <summary> Fired when a shard is connected to the Discord gateway. </summary> /// <summary> Fired when a shard is connected to the Discord gateway. </summary>
public event Func<DiscordSocketClient, Task> ShardConnected
public event Func<DiscordSocketClient, Task> ShardConnected
{ {
add { _shardConnectedEvent.Add(value); } add { _shardConnectedEvent.Add(value); }
remove { _shardConnectedEvent.Remove(value); } remove { _shardConnectedEvent.Remove(value); }
} }
private readonly AsyncEvent<Func<DiscordSocketClient, Task>> _shardConnectedEvent = new AsyncEvent<Func<DiscordSocketClient, Task>>(); private readonly AsyncEvent<Func<DiscordSocketClient, Task>> _shardConnectedEvent = new AsyncEvent<Func<DiscordSocketClient, Task>>();
/// <summary> Fired when a shard is disconnected from the Discord gateway. </summary> /// <summary> Fired when a shard is disconnected from the Discord gateway. </summary>
public event Func<Exception, DiscordSocketClient, Task> ShardDisconnected
public event Func<Exception, DiscordSocketClient, Task> ShardDisconnected
{ {
add { _shardDisconnectedEvent.Add(value); } add { _shardDisconnectedEvent.Add(value); }
remove { _shardDisconnectedEvent.Remove(value); } remove { _shardDisconnectedEvent.Remove(value); }
} }
private readonly AsyncEvent<Func<Exception, DiscordSocketClient, Task>> _shardDisconnectedEvent = new AsyncEvent<Func<Exception, DiscordSocketClient, Task>>(); private readonly AsyncEvent<Func<Exception, DiscordSocketClient, Task>> _shardDisconnectedEvent = new AsyncEvent<Func<Exception, DiscordSocketClient, Task>>();
/// <summary> Fired when a guild data for a shard has finished downloading. </summary> /// <summary> Fired when a guild data for a shard has finished downloading. </summary>
public event Func<DiscordSocketClient, Task> ShardReady
public event Func<DiscordSocketClient, Task> ShardReady
{ {
add { _shardReadyEvent.Add(value); } add { _shardReadyEvent.Add(value); }
remove { _shardReadyEvent.Remove(value); } remove { _shardReadyEvent.Remove(value); }
} }
private readonly AsyncEvent<Func<DiscordSocketClient, Task>> _shardReadyEvent = new AsyncEvent<Func<DiscordSocketClient, Task>>(); private readonly AsyncEvent<Func<DiscordSocketClient, Task>> _shardReadyEvent = new AsyncEvent<Func<DiscordSocketClient, Task>>();
/// <summary> Fired when a shard receives a heartbeat from the Discord gateway. </summary> /// <summary> Fired when a shard receives a heartbeat from the Discord gateway. </summary>
public event Func<int, int, DiscordSocketClient, Task> ShardLatencyUpdated
public event Func<int, int, DiscordSocketClient, Task> ShardLatencyUpdated
{ {
add { _shardLatencyUpdatedEvent.Add(value); } add { _shardLatencyUpdatedEvent.Add(value); }
remove { _shardLatencyUpdatedEvent.Remove(value); } remove { _shardLatencyUpdatedEvent.Remove(value); }
} }
private readonly AsyncEvent<Func<int, int, DiscordSocketClient, Task>> _shardLatencyUpdatedEvent = new AsyncEvent<Func<int, int, DiscordSocketClient, Task>>(); private readonly AsyncEvent<Func<int, int, DiscordSocketClient, Task>> _shardLatencyUpdatedEvent = new AsyncEvent<Func<int, int, DiscordSocketClient, Task>>();
/// <summary> Fired when all shards are ready. </summary>
public event Func<Task> Ready
{
add { _readyEvent.Add(value); }
remove { _readyEvent.Remove(value); }
}
private readonly AsyncEvent<Func<Task>> _readyEvent = new AsyncEvent<Func<Task>>();
} }
}
}

+ 14
- 3
src/Discord.Net.WebSocket/DiscordShardedClient.cs View File

@@ -18,6 +18,7 @@ namespace Discord.WebSocket
private int[] _shardIds; private int[] _shardIds;
private DiscordSocketClient[] _shards; private DiscordSocketClient[] _shards;
private int _totalShards; private int _totalShards;
private int _shardsReady;


private bool _isDisposed; private bool _isDisposed;


@@ -290,16 +291,26 @@ namespace Discord.WebSocket
}; };
if (isPrimary) if (isPrimary)
{ {
client.Ready += () =>
client.Ready += async () =>
{ {
CurrentUser = client.CurrentUser; CurrentUser = client.CurrentUser;
return Task.Delay(0);
if (_totalShards >= _shardsReady++) //to account for re-identifies, don't see a better way to handle this
{
await _readyEvent.InvokeAsync();
}
}; };
} }


client.Connected += () => _shardConnectedEvent.InvokeAsync(client); client.Connected += () => _shardConnectedEvent.InvokeAsync(client);
client.Disconnected += (exception) => _shardDisconnectedEvent.InvokeAsync(exception, client); client.Disconnected += (exception) => _shardDisconnectedEvent.InvokeAsync(exception, client);
client.Ready += () => _shardReadyEvent.InvokeAsync(client);
client.Ready += async () =>
{
await _shardReadyEvent.InvokeAsync(client);
if (_totalShards >= _shardsReady++) //to account for re-identifies, don't see a better way to handle this
{
await _readyEvent.InvokeAsync();
}
};
client.LatencyUpdated += (oldLatency, newLatency) => _shardLatencyUpdatedEvent.InvokeAsync(oldLatency, newLatency, client); client.LatencyUpdated += (oldLatency, newLatency) => _shardLatencyUpdatedEvent.InvokeAsync(oldLatency, newLatency, client);


client.ChannelCreated += (channel) => _channelCreatedEvent.InvokeAsync(channel); client.ChannelCreated += (channel) => _channelCreatedEvent.InvokeAsync(channel);


Loading…
Cancel
Save