Skip to content

Commit

Permalink
Update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Jun 4, 2024
1 parent 93ed45c commit c2682e5
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 70 deletions.
2 changes: 1 addition & 1 deletion libs/DSharpPlus
Submodule DSharpPlus updated 307 files
1 change: 0 additions & 1 deletion src/Commands/Common/CalculateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Data;
using System.Threading.Tasks;
using DSharpPlus.Commands;
Expand Down
13 changes: 10 additions & 3 deletions src/Commands/Common/InfoCommand/InfoCommand.Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,16 @@ public async ValueTask BotInfoAsync(CommandContext context)

StringBuilder stringBuilder = new();
stringBuilder.Append(context.Client.CurrentUser.Mention);
stringBuilder.Append(", `");
stringBuilder.Append(((DefaultPrefixResolver)context.Extension.GetProcessor<TextCommandProcessor>().Configuration.PrefixResolver.Target!).Prefix);
stringBuilder.Append("`, `/`");
stringBuilder.Append(", ");
foreach (string prefix in ((DefaultPrefixResolver)context.Extension.GetProcessor<TextCommandProcessor>().Configuration.PrefixResolver.Target!).Prefixes)
{
stringBuilder.Append('`');
stringBuilder.Append(prefix);
stringBuilder.Append('`');
stringBuilder.Append(", ");
}

stringBuilder.Append(", `/`");
embedBuilder.AddField("Prefixes", stringBuilder.ToString(), true);
embedBuilder.AddField("Bot Version", _botVersion, true);
embedBuilder.AddField("DSharpPlus Library Version", _dSharpPlusVersion, true);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Common/TimeOfCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static async ValueTask ExecuteAsync(CommandContext context, params string
foreach (string message in messages)
{
converterContext.NextArgument();
MessageCreateEventArgs messageCreateEventArgs = TextCommandUtilities.CreateFakeMessageEventArgs(context, message);
MessageCreatedEventArgs messageCreateEventArgs = TextCommandUtilities.CreateFakeMessageEventArgs(context, message);
Optional<ulong> parsedMessageId = await _uint64Converter.ConvertAsync(converterContext, messageCreateEventArgs);
if (parsedMessageId.HasValue)
{
Expand Down
27 changes: 26 additions & 1 deletion src/Events/DiscordEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
using System.Linq;
using System.Reflection;
using DSharpPlus;
using DSharpPlus.Commands.Processors.SlashCommands;
using DSharpPlus.Commands.Processors.TextCommands;
using Microsoft.Extensions.DependencyInjection;

namespace OoLunar.Tomoe.Events
{
public sealed class DiscordEventManager
{
public DiscordIntents Intents { get; private set; }
public DiscordIntents Intents { get; private set; } = TextCommandProcessor.RequiredIntents | SlashCommandProcessor.RequiredIntents | DiscordIntents.MessageContents;
private readonly IServiceProvider _serviceProvider;
private readonly List<MethodInfo> _eventHandlers = [];

Expand All @@ -31,6 +33,29 @@ public void GatherEventHandlers(Assembly assembly)
}
}

public void RegisterEventHandlers(DiscordClientBuilder clientBuilder)
{
ArgumentNullException.ThrowIfNull(clientBuilder, nameof(clientBuilder));
clientBuilder.ConfigureEventHandlers(eventHandlingBuilder =>
{
foreach (MethodInfo methodInfo in typeof(EventHandlingBuilder).GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
if (!methodInfo.Name.StartsWith("Handle", StringComparison.Ordinal))
{
continue;
}

foreach (MethodInfo eventHandler in _eventHandlers)
{
if (methodInfo.GetParameters().Select(parameter => parameter.ParameterType).SequenceEqual(eventHandler.GetParameters().Select(parameter => parameter.ParameterType)))
{
eventHandlingBuilder = (EventHandlingBuilder)methodInfo.Invoke(eventHandlingBuilder, [eventHandler])!;
}
}
}
});
}

public void RegisterEventHandlers(object obj)
{
ArgumentNullException.ThrowIfNull(obj, nameof(obj));
Expand Down
14 changes: 7 additions & 7 deletions src/Events/Handlers/GuildMemberEventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class GuildMemberEventHandlers
public GuildMemberEventHandlers(ILogger<GuildMemberEventHandlers> logger) => this.logger = logger ?? NullLogger<GuildMemberEventHandlers>.Instance;

[DiscordEvent(DiscordIntents.Guilds | DiscordIntents.GuildPresences)]
public async Task OnGuildCreateAsync(DiscordClient _, GuildCreateEventArgs eventArgs)
public async Task OnGuildCreateAsync(DiscordClient _, GuildCreatedEventArgs eventArgs)
{
List<GuildMemberModel> guildMemberModels = [];
foreach (DiscordMember member in eventArgs.Guild.Members.Values)
Expand All @@ -37,7 +37,7 @@ public async Task OnGuildCreateAsync(DiscordClient _, GuildCreateEventArgs event
}

[DiscordEvent(DiscordIntents.GuildMembers)]
public async Task OnGuildMemberAddAsync(DiscordClient _, GuildMemberAddEventArgs eventArgs)
public async Task OnGuildMemberAddAsync(DiscordClient _, GuildMemberAddedEventArgs eventArgs)
{
GuildMemberModel? guildMemberModel = await GuildMemberModel.FindMemberAsync(eventArgs.Member.Id, eventArgs.Guild.Id);
if (guildMemberModel is null)
Expand Down Expand Up @@ -76,7 +76,7 @@ public async Task OnGuildMemberAddAsync(DiscordClient _, GuildMemberAddEventArgs
}

[DiscordEvent(DiscordIntents.GuildMembers)]
public static async Task OnGuildMemberRemoveAsync(DiscordClient _, GuildMemberRemoveEventArgs eventArgs)
public static async Task OnGuildMemberRemoveAsync(DiscordClient _, GuildMemberRemovedEventArgs eventArgs)
{
GuildMemberModel? guildMemberModel = await GuildMemberModel.FindMemberAsync(eventArgs.Member.Id, eventArgs.Guild.Id);
if (guildMemberModel is null)
Expand All @@ -93,7 +93,7 @@ public static async Task OnGuildMemberRemoveAsync(DiscordClient _, GuildMemberRe
}

[DiscordEvent(DiscordIntents.GuildMembers)]
public static async Task OnGuildMemberUpdateAsync(DiscordClient _, GuildMemberUpdateEventArgs eventArgs)
public static async Task OnGuildMemberUpdateAsync(DiscordClient _, GuildMemberUpdatedEventArgs eventArgs)
{
GuildMemberModel? guildMemberModel = await GuildMemberModel.FindMemberAsync(eventArgs.Member.Id, eventArgs.Guild.Id);
if (guildMemberModel is null)
Expand All @@ -110,7 +110,7 @@ public static async Task OnGuildMemberUpdateAsync(DiscordClient _, GuildMemberUp
}

[DiscordEvent(DiscordIntents.GuildMembers)]
public static async Task OnGuildMemberChunkAsync(DiscordClient _, GuildMembersChunkEventArgs eventArgs)
public static async Task OnGuildMemberChunkAsync(DiscordClient _, GuildMembersChunkedEventArgs eventArgs)
{
foreach (DiscordMember member in eventArgs.Members)
{
Expand All @@ -130,7 +130,7 @@ public static async Task OnGuildMemberChunkAsync(DiscordClient _, GuildMembersCh
}

[DiscordEvent(DiscordIntents.GuildModeration)]
public static async Task OnGuildMemberAddBanAsync(DiscordClient _, GuildBanAddEventArgs eventArgs)
public static async Task OnGuildMemberAddBanAsync(DiscordClient _, GuildBanAddedEventArgs eventArgs)
{
GuildMemberModel? guildMemberModel = await GuildMemberModel.FindMemberAsync(eventArgs.Member.Id, eventArgs.Guild.Id);
if (guildMemberModel is null)
Expand All @@ -147,7 +147,7 @@ public static async Task OnGuildMemberAddBanAsync(DiscordClient _, GuildBanAddEv
}

[DiscordEvent(DiscordIntents.GuildModeration)]
public static async Task OnGuildMemberRemoveBanAsync(DiscordClient _, GuildBanRemoveEventArgs eventArgs)
public static async Task OnGuildMemberRemoveBanAsync(DiscordClient _, GuildBanRemovedEventArgs eventArgs)
{
GuildMemberModel? guildMemberModel = await GuildMemberModel.FindMemberAsync(eventArgs.Member.Id, eventArgs.Guild.Id);
if (guildMemberModel is null)
Expand Down
2 changes: 1 addition & 1 deletion src/Events/Handlers/PollSubmittedEventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace OoLunar.Tomoe.Events.Handlers
public sealed class PollSubmittedEventHandlers
{
[DiscordEvent]
public static async Task OnPollSubmittedAsync(DiscordClient client, InteractionCreateEventArgs eventArgs)
public static async Task OnPollSubmittedAsync(DiscordClient client, InteractionCreatedEventArgs eventArgs)
{
if (eventArgs.Interaction.Type != DiscordInteractionType.Component)
{
Expand Down
85 changes: 39 additions & 46 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using DSharpPlus.Commands.Processors.TextCommands;
using DSharpPlus.Commands.Processors.TextCommands.Parsing;
using DSharpPlus.Commands.Processors.UserCommands;
using DSharpPlus.Interactivity.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -22,7 +23,6 @@
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
using DSharpPlusDiscordConfiguration = DSharpPlus.DiscordConfiguration;
using SerilogLoggerConfiguration = Serilog.LoggerConfiguration;

namespace OoLunar.Tomoe
Expand Down Expand Up @@ -119,24 +119,22 @@ public static async Task Main(string[] args)
}

DiscordEventManager eventManager = serviceProvider.GetRequiredService<DiscordEventManager>();
DiscordShardedClient discordClient = new(new DSharpPlusDiscordConfiguration
{
Token = tomoeConfiguration.Discord.Token,
Intents = eventManager.Intents | TextCommandProcessor.RequiredIntents | SlashCommandProcessor.RequiredIntents | DiscordIntents.MessageContents,
LoggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>(),
});

return discordClient;
DiscordClientBuilder clientBuilder = DiscordClientBuilder.CreateDefault(tomoeConfiguration.Discord.Token, eventManager.Intents, serviceCollection);
//clientBuilder.ConfigureLogging(logger => logger.AddSerilo)
eventManager.RegisterEventHandlers(clientBuilder);
return clientBuilder.Build();
});

// Almost start the program
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
DatabaseHandler databaseHandler = serviceProvider.GetRequiredService<DatabaseHandler>();
TomoeConfiguration tomoeConfiguration = serviceProvider.GetRequiredService<TomoeConfiguration>();
DiscordShardedClient discordClient = serviceProvider.GetRequiredService<DiscordShardedClient>();
DiscordClient discordClient = serviceProvider.GetRequiredService<DiscordClient>();
DiscordEventManager eventManager = serviceProvider.GetRequiredService<DiscordEventManager>();
Assembly currentAssembly = typeof(Program).Assembly;

discordClient.UseInteractivity();

// Connect to the database
try
{
Expand All @@ -148,59 +146,54 @@ public static async Task Main(string[] args)
}

// Register extensions here since these involve asynchronous operations
IReadOnlyDictionary<int, CommandsExtension> commandsExtensions = await discordClient.UseCommandsAsync(new CommandsConfiguration()
CommandsExtension commandsExtension = discordClient.UseCommands(new CommandsConfiguration()
{
ServiceProvider = serviceProvider,
DebugGuildId = tomoeConfiguration.Discord.GuildId,
UseDefaultCommandErrorHandler = false,
RegisterDefaultCommandProcessors = false
});

// Iterate through each Discord shard
foreach (CommandsExtension commandsExtension in commandsExtensions.Values)
{
// Add all commands by scanning the current assembly
commandsExtension.AddCommands(currentAssembly);
// Add all commands by scanning the current assembly
commandsExtension.AddCommands(currentAssembly);

// Enable each command type specified by the user
List<ICommandProcessor> processors = [];
foreach (string processor in tomoeConfiguration.Discord.Processors)
// Enable each command type specified by the user
List<ICommandProcessor> processors = [];
foreach (string processor in tomoeConfiguration.Discord.Processors)
{
if (processor.Equals("text", StringComparison.OrdinalIgnoreCase))
{
if (processor.Equals("text", StringComparison.OrdinalIgnoreCase))
TextCommandProcessor textCommandProcessor = new(new()
{
TextCommandProcessor textCommandProcessor = new(new()
{
PrefixResolver = new DefaultPrefixResolver(tomoeConfiguration.Discord.Prefix ?? throw new InvalidOperationException("Missing Discord prefix.")).ResolvePrefixAsync
});
PrefixResolver = new DefaultPrefixResolver(true, tomoeConfiguration.Discord.Prefix ?? throw new InvalidOperationException("Missing Discord prefix.")).ResolvePrefixAsync
});

textCommandProcessor.AddConverters(currentAssembly);
processors.Add(textCommandProcessor);
}
else if (processor.Equals("slash", StringComparison.OrdinalIgnoreCase))
{
SlashCommandProcessor slashCommandProcessor = new();
slashCommandProcessor.AddConverters(currentAssembly);
processors.Add(slashCommandProcessor);
}
else if (processor.Equals("user", StringComparison.OrdinalIgnoreCase))
{
processors.Add(new UserCommandProcessor());
}
else if (processor.Equals("message", StringComparison.OrdinalIgnoreCase))
{
processors.Add(new MessageCommandProcessor());
}
textCommandProcessor.AddConverters(currentAssembly);
processors.Add(textCommandProcessor);
}
else if (processor.Equals("slash", StringComparison.OrdinalIgnoreCase))
{
SlashCommandProcessor slashCommandProcessor = new();
slashCommandProcessor.AddConverters(currentAssembly);
processors.Add(slashCommandProcessor);
}
else if (processor.Equals("user", StringComparison.OrdinalIgnoreCase))
{
processors.Add(new UserCommandProcessor());
}
else if (processor.Equals("message", StringComparison.OrdinalIgnoreCase))
{
processors.Add(new MessageCommandProcessor());
}

await commandsExtension.AddProcessorsAsync(processors);
eventManager.RegisterEventHandlers(commandsExtension);
}

await commandsExtension.AddProcessorsAsync(processors);
eventManager.RegisterEventHandlers(commandsExtension);

// Register event handlers for the Discord Client itself
eventManager.RegisterEventHandlers(discordClient);

// Connect the bot to the Discord gateway.
await discordClient.StartAsync();
await discordClient.ConnectAsync();

// Wait indefinitely
await Task.Delay(-1);
Expand Down
Loading

0 comments on commit c2682e5

Please sign in to comment.