-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
134 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using System; | ||
|
||
namespace NotifierRedirecter.Events; | ||
|
||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)] | ||
public sealed class DiscordEventAttribute : Attribute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace NotifierRedirecter.Events; | ||
|
||
public sealed class DiscordEventManager | ||
{ | ||
private readonly IServiceProvider ServiceProvider; | ||
private readonly List<MethodInfo> EventHandlers = []; | ||
|
||
public DiscordEventManager(IServiceProvider serviceProvider) => this.ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); | ||
|
||
public void GatherEventHandlers(Assembly assembly) | ||
{ | ||
ArgumentNullException.ThrowIfNull(assembly); | ||
foreach (Type type in assembly.GetExportedTypes()) | ||
{ | ||
foreach (MethodInfo methodInfo in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) | ||
{ | ||
if (methodInfo.GetCustomAttribute<DiscordEventAttribute>() is not null) | ||
{ | ||
this.EventHandlers.Add(methodInfo); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void RegisterEventHandlers(object obj) | ||
{ | ||
ArgumentNullException.ThrowIfNull(obj); | ||
foreach (EventInfo eventInfo in obj.GetType().GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) | ||
{ | ||
foreach (MethodInfo methodInfo in this.EventHandlers) | ||
{ | ||
if (eventInfo.EventHandlerType!.GetGenericArguments().SequenceEqual(methodInfo.GetParameters().Select(parameter => parameter.ParameterType))) | ||
{ | ||
Delegate handler = methodInfo.IsStatic | ||
? Delegate.CreateDelegate(eventInfo.EventHandlerType, methodInfo) | ||
: Delegate.CreateDelegate(eventInfo.EventHandlerType, ActivatorUtilities.CreateInstance(this.ServiceProvider, methodInfo.DeclaringType!), methodInfo); | ||
|
||
eventInfo.AddEventHandler(obj, handler); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using DSharpPlus; | ||
using DSharpPlus.EventArgs; | ||
|
||
namespace NotifierRedirecter.Events.Handlers; | ||
|
||
public sealed class TypingStartedEventHandler | ||
{ | ||
private readonly UserActivityTracker _userActivityTracker; | ||
|
||
public TypingStartedEventHandler(UserActivityTracker userActivityTracker) => this._userActivityTracker = userActivityTracker ?? throw new ArgumentNullException(nameof(userActivityTracker)); | ||
public Task ExecuteAsync(DiscordClient _, TypingStartEventArgs eventArgs) | ||
{ | ||
this._userActivityTracker.UpdateUser(eventArgs.User.Id, eventArgs.Channel.Id); | ||
return Task.CompletedTask; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters