Skip to content

Commit 90e8cca

Browse files
committed
refactor handlers to use event dispatcher pattern
1 parent 3334551 commit 90e8cca

3 files changed

Lines changed: 18 additions & 54 deletions

File tree

src/Disc.NET/Handlers/EventHandlers/PrefixCommandHandler.cs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,19 @@ public PrefixCommandHandler(AppConfiguration appConfiguration) : base(appConfigu
1414
{
1515
}
1616

17-
public override async Task HandleAsync(GatewayEvent @event, JsonDocument contextJson, AppConfiguration configuration)
18-
{
19-
if (@event != GatewayEvent.MessageCreate)
20-
{
21-
await base.HandleAsync(@event, contextJson, configuration).ConfigureAwait(false);
22-
return;
23-
}
17+
public GatewayEvent GetEventType()
18+
=> GatewayEvent.MessageCreate;
2419

20+
public async Task HandleAsync(JsonDocument contextJson, AppConfiguration configuration)
21+
{
2522
var content = contextJson.GetStringProperty("content");
26-
if (string.IsNullOrEmpty(content))
27-
{
28-
await base.HandleAsync(@event, contextJson, configuration).ConfigureAwait(false);
29-
return;
30-
}
23+
if (string.IsNullOrEmpty(content)) return;
3124
var commandModel = BuildCommandModelByEventContent(content);
32-
if (commandModel.Prefix != configuration.BotPrefix)
33-
{
34-
await base.HandleAsync(@event, contextJson, configuration).ConfigureAwait(false);
35-
return;
36-
}
37-
3825
var command = (IPrefixCommand)
3926
GetCommandByAttribute<PrefixCommandAttribute, CommandContext>(commandModel.Name);
40-
41-
if (command == null)
42-
{
43-
await base.HandleAsync(@event, contextJson, configuration).ConfigureAwait(false);
44-
return;
45-
}
46-
27+
if (command == null) return;
4728
var context = BuildCommandContext(contextJson, configuration);
4829
await command.RunAsync(context, commandModel.Params).ConfigureAwait(false);
4930
}
50-
5131
}
5232
}

src/Disc.NET/Handlers/EventHandlers/SlashCommandHandler.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,20 @@ public SlashCommandHandler(AppConfiguration appConfiguration) : base(appConfigur
1414
{
1515
}
1616

17-
public override async Task HandleAsync(GatewayEvent @event, JsonDocument contextJson,
18-
AppConfiguration configuration)
19-
{
20-
if (@event != GatewayEvent.InteractionCreate)
21-
{
22-
await base.HandleAsync(@event, contextJson, configuration).ConfigureAwait(false);
23-
return;
24-
}
2517

18+
public GatewayEvent GetEventType()
19+
=> GatewayEvent.InteractionCreate;
20+
21+
public async Task HandleAsync(JsonDocument contextJson,AppConfiguration configuration)
22+
{
2623
var data = contextJson.GetJsonStringProperty("data");
24+
if (string.IsNullOrEmpty(data)) return;
2725
var slashCommandResult = Serializer.Deserialize<SlashCommandParamsResult>(data);
28-
if (slashCommandResult == null)
29-
{
30-
await base.HandleAsync(@event, contextJson, configuration).ConfigureAwait(false);
31-
return;
32-
}
26+
if (slashCommandResult == null) return;
3327
var command = (ISlashCommand)
3428
GetCommandByAttribute<SlashCommandAttribute, InteractionContext>(slashCommandResult.Name);
35-
if (command == null)
36-
{
37-
await base.HandleAsync(@event, contextJson, configuration).ConfigureAwait(false);
38-
return;
39-
}
40-
29+
if (command == null) return;
4130
var context = BuildInteractionContext(contextJson, configuration);
42-
4331
await SendInteractionResponseAsync(contextJson);
4432
await command.RunAsync(context, slashCommandResult).ConfigureAwait(false);
4533

src/Disc.NET/Handlers/EventHandlers/SlashCommandRegisterHandler.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@ public SlashCommandRegisterHandler(AppConfiguration appConfiguration) : base(app
1313
{
1414
}
1515

16-
public override async Task HandleAsync(GatewayEvent @event, JsonDocument contextJson,
17-
AppConfiguration configuration)
18-
{
19-
if (@event != GatewayEvent.Ready)
20-
{
21-
await base.HandleAsync(@event, contextJson, configuration).ConfigureAwait(false);
22-
return;
23-
}
16+
public GatewayEvent GetEventType()
17+
=> GatewayEvent.Ready;
2418

19+
public async Task HandleAsync(JsonDocument contextJson,AppConfiguration configuration)
20+
{
2521
List<SlashCommandAttribute> attributes = GetCommandAttributes<SlashCommandAttribute>();
2622
foreach (var attribute in attributes)
2723
{

0 commit comments

Comments
 (0)