forked from RPCS3/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderedEventHandlerWrapper.cs
37 lines (32 loc) · 1.2 KB
/
OrderedEventHandlerWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.EventArgs;
namespace CompatBot.EventHandlers;
public class OrderedEventHandlerWrapper<T> where T: DiscordEventArgs
{
private readonly ICollection<Func<DiscordClient,T,Task<bool>>> orderedHandlers;
private readonly ICollection<Func<DiscordClient,T,Task>> unorderedHandlers;
public OrderedEventHandlerWrapper(ICollection<Func<DiscordClient, T, Task<bool>>> orderedHandlers, ICollection<Func<DiscordClient, T, Task>> unorderedHandlers)
{
this.orderedHandlers = orderedHandlers;
this.unorderedHandlers = unorderedHandlers;
}
public async Task OnEvent(DiscordClient client, T eventArgs)
{
try
{
foreach (var h in orderedHandlers)
if (!await h(client, eventArgs).ConfigureAwait(false))
return;
var unorderedTasks = unorderedHandlers.Select(async h => await h(client, eventArgs).ConfigureAwait(false));
await Task.WhenAll(unorderedTasks).ConfigureAwait(false);
}
catch (Exception e)
{
Config.Log.Error(e);
}
}
}