Skip to content

Commit

Permalink
Revert "Update DSharpPlus version, using new async listeners that don…
Browse files Browse the repository at this point in the history
…'t require offloading"

This reverts commit 653d013.
  • Loading branch information
Ryan de Jonge committed Apr 11, 2023
1 parent 653d013 commit f93352b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 75 deletions.
39 changes: 22 additions & 17 deletions ModCore/Commands/Interactive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,32 @@ public async Task PollAsync(InteractionContext ctx)

List<DiscordUser> members = new List<DiscordUser>();

async Task collectorTask(DiscordClient sender, DSharpPlus.EventArgs.ComponentInteractionCreateEventArgs e)
Task collectorTask(DiscordClient sender, DSharpPlus.EventArgs.ComponentInteractionCreateEventArgs e)
{
if(e.Message.Id == giveaway.Id && e.User.Id != ctx.User.Id)
_ = Task.Run(async () =>
{
if(e.Interaction.Data.CustomId == "join" && !members.Any(x => x.Id == e.User.Id))
if(e.Message.Id == giveaway.Id && e.User.Id != ctx.User.Id)
{
members.Add(e.User);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
.WithContent("✅ You joined the raffle!").AsEphemeral());
if(e.Interaction.Data.CustomId == "join" && !members.Any(x => x.Id == e.User.Id))
{
members.Add(e.User);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
.WithContent("✅ You joined the raffle!").AsEphemeral());
}
else if(e.Interaction.Data.CustomId == "leave")
{
members.RemoveAll(x => x.Id == e.User.Id);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
.WithContent("✅ You left the raffle.").AsEphemeral());
}
else
{
await e.Interaction.CreateResponseAsync(InteractionResponseType.Pong);
}
}
else if(e.Interaction.Data.CustomId == "leave")
{
members.RemoveAll(x => x.Id == e.User.Id);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
.WithContent("✅ You left the raffle.").AsEphemeral());
}
else
{
await e.Interaction.CreateResponseAsync(InteractionResponseType.Pong);
}
}
});

return Task.CompletedTask;
}

ctx.Client.ComponentInteractionCreated += collectorTask;
Expand Down
43 changes: 23 additions & 20 deletions ModCore/Extensions/Attributes/AsyncListenerAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using DSharpPlus;
using DSharpPlus.AsyncEvents;
using Emzi0767.Utilities;
using Microsoft.Extensions.Logging;
using ModCore.Extensions.Enums;
using System;
Expand All @@ -21,29 +21,32 @@ public AsyncListenerAttribute(EventType target)

public void Register(DiscordClient client, MethodInfo listener, IServiceProvider services)
{
async Task onEvent(DiscordClient client, object e)
Task onEvent(DiscordClient client, object e)
{

try
_ = Task.Run(async () =>
{
List<object> parameters = new List<object>();
foreach (var param in listener.GetParameters())
try
{
if (param.ParameterType == typeof(DiscordClient))
parameters.Add(client);
else if (param.ParameterType.IsAssignableTo(typeof(AsyncEventArgs)))
parameters.Add(e);
else
parameters.Add(services.GetService(param.ParameterType));
}
List<object> parameters = new List<object>();
foreach (var param in listener.GetParameters())
{
if (param.ParameterType == typeof(DiscordClient))
parameters.Add(client);
else if (param.ParameterType.IsAssignableTo(typeof(AsyncEventArgs)))
parameters.Add(e);
else
parameters.Add(services.GetService(param.ParameterType));
}
await (Task)listener.Invoke(null, parameters.ToArray());
}
catch (Exception ex)
{
client.Logger.LogError($"Uncaught error in event handler thread: {ex}");
client.Logger.LogError(ex.StackTrace);
}
await (Task)listener.Invoke(null, parameters.ToArray());
}
catch (Exception ex)
{
client.Logger.LogError($"Uncaught error in event handler thread: {ex}");
client.Logger.LogError(ex.StackTrace);
}
});
return Task.CompletedTask;
};

#region Registering correct events
Expand Down
12 changes: 10 additions & 2 deletions ModCore/Extensions/InteractionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ private void setupComponents(DiscordClient client)
registerComponentHandler(handler);
}

client.ComponentInteractionCreated += handleComponentAsync;
client.ComponentInteractionCreated += (sender, e) =>
{
_ = Task.Run(async () => await handleComponentAsync(sender, e));
return Task.CompletedTask;
};
}

private void registerComponentHandler(Type type)
Expand Down Expand Up @@ -112,7 +116,11 @@ private void setupModals(DiscordClient client)
registerModalHandler(handler);
}

client.ModalSubmitted += handleModalSubmission;
client.ModalSubmitted += (sender, e) =>
{
_ = Task.Run(async () => await handleModalSubmission(sender, e));
return Task.CompletedTask;
};
}

private ModalHandler registerModalHandler(Type type)
Expand Down
20 changes: 10 additions & 10 deletions ModCore/ModCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Chronic-netstandard" Version="0.3.2.4" />
<PackageReference Include="DSharpPlus" Version="4.4.0-nightly-01429" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.4.0-nightly-01429" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.0-nightly-01429" />
<PackageReference Include="DSharpPlus.Rest" Version="4.4.0-nightly-01429" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.0-nightly-01429" />
<PackageReference Include="DSharpPlus" Version="4.4.0-nightly-01393" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.4.0-nightly-01393" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.0-nightly-01393" />
<PackageReference Include="DSharpPlus.Rest" Version="4.4.0-nightly-01393" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.0-nightly-01393" />
<PackageReference Include="F23.StringSimilarity" Version="5.1.0" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.3" />
<PackageReference Include="Microsoft.CodeAnalysis.Scripting" Version="4.5.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
Expand Down
57 changes: 31 additions & 26 deletions ModCore/Modals/PollModal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,42 @@ await interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWith
var msg = await interaction.Channel.SendMessageAsync(message);

// Temporary event handler for this specific poll.
async Task handlePoll(DiscordClient sender, ComponentInteractionCreateEventArgs e)
Task handlePoll(DiscordClient sender, ComponentInteractionCreateEventArgs e)
{
// check message ID is correct
if (e.Message.Id == msg.Id)
_ = Task.Run(async () =>
{
if (e.Interaction.Data.CustomId == "x")
// check message ID is correct
if (e.Message.Id == msg.Id)
{
responses.Remove(e.User.Id);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
.WithContent("✅ Cleared your poll response (if any).").AsEphemeral());
return;
if (e.Interaction.Data.CustomId == "x")
{
responses.Remove(e.User.Id);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
.WithContent("✅ Cleared your poll response (if any).").AsEphemeral());
return;
}
if (!int.TryParse(e.Interaction.Data.CustomId, out var index))
{
return;
}
if (responses.Any(x => x.Key == e.User.Id))
{
responses[e.User.Id] = index;
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
.WithContent($"✅ Updated your poll vote to `{splitOptions[index]}`.").AsEphemeral());
}
else
{
responses.Add(e.User.Id, index);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
.WithContent($"✅ Set your poll vote to `{splitOptions[index]}`.").AsEphemeral());
}
}
});

if (!int.TryParse(e.Interaction.Data.CustomId, out var index))
{
return;
}

if (responses.Any(x => x.Key == e.User.Id))
{
responses[e.User.Id] = index;
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
.WithContent($"✅ Updated your poll vote to `{splitOptions[index]}`.").AsEphemeral());
}
else
{
responses.Add(e.User.Id, index);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
.WithContent($"✅ Set your poll vote to `{splitOptions[index]}`.").AsEphemeral());
}
}
return Task.CompletedTask;
}

client.ComponentInteractionCreated += handlePoll;
Expand Down

0 comments on commit f93352b

Please sign in to comment.