Skip to content

Commit

Permalink
aaaaaaaaaaa
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby authored and TheXorog committed Jul 28, 2023
1 parent 3c9b3b5 commit ef36a80
Show file tree
Hide file tree
Showing 14 changed files with 459 additions and 409 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ApplicationCommandsConfiguration
/// <para>This allows passing data around without resorting to static members.</para>
/// <para>Defaults to <see langword="null"/>.</para>
/// </summary>
public IServiceProvider ServiceProvider { internal get; set; }
public IServiceProvider? ServiceProvider { internal get; set; }

/// <summary>
/// <para>This option enables the default help command.</para>
Expand Down
716 changes: 377 additions & 339 deletions DisCatSharp.ApplicationCommands/ApplicationCommandsExtension.cs

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions DisCatSharp.ApplicationCommands/ApplicationCommandsUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,40 @@ internal static bool IsModuleCandidateType(this Type type)
/// <summary>
/// Whether this module is a candidate type.
/// </summary>
/// <param name="ti">The type info.</param>
internal static bool IsModuleCandidateType(this TypeInfo ti)
/// <param name="targetTypeInfo">The type info.</param>
internal static bool IsModuleCandidateType(this TypeInfo targetTypeInfo)
{
if (ApplicationCommandsExtension.DebugEnabled)
ApplicationCommandsExtension.Logger.LogDebug("Checking type {name}", ti.FullName);
ApplicationCommandsExtension.Logger.LogDebug("Checking type {name}", targetTypeInfo.FullName);
// check if compiler-generated
if (ti.GetCustomAttribute<CompilerGeneratedAttribute>(false) != null)
if (targetTypeInfo.GetCustomAttribute<CompilerGeneratedAttribute>(false) != null)
return false;

// check if derives from the required base class
var tmodule = typeof(ApplicationCommandsModule);
var timodule = tmodule.GetTypeInfo();
if (!timodule.IsAssignableFrom(ti))
var type = typeof(ApplicationCommandsModule);
var typeInfo = type.GetTypeInfo();
if (!typeInfo.IsAssignableFrom(targetTypeInfo))
{
if (ApplicationCommandsExtension.DebugEnabled)
ApplicationCommandsExtension.Logger.LogDebug("Not assignable from type");
return false;
}

// check if anonymous
if (ti.IsGenericType && ti.Name.Contains("AnonymousType") && (ti.Name.StartsWith("<>") || ti.Name.StartsWith("VB$")) && (ti.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic)
if (targetTypeInfo.IsGenericType && targetTypeInfo.Name.Contains("AnonymousType") && (targetTypeInfo.Name.StartsWith("<>") || targetTypeInfo.Name.StartsWith("VB$")) && (targetTypeInfo.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic)
{
if (ApplicationCommandsExtension.DebugEnabled)
ApplicationCommandsExtension.Logger.LogDebug("Anonymous");
return false;
}

// check if abstract, static, or not a class
if (!ti.IsClass || ti.IsAbstract)
if (!targetTypeInfo.IsClass || targetTypeInfo.IsAbstract)
return false;

// check if delegate type
var tdelegate = typeof(Delegate).GetTypeInfo();
if (tdelegate.IsAssignableFrom(ti))
var typeDelegate = typeof(Delegate).GetTypeInfo();
if (typeDelegate.IsAssignableFrom(targetTypeInfo))
{
if (ApplicationCommandsExtension.DebugEnabled)
ApplicationCommandsExtension.Logger.LogDebug("Delegated");
Expand All @@ -87,15 +87,15 @@ internal static bool IsModuleCandidateType(this TypeInfo ti)
if (ApplicationCommandsExtension.DebugEnabled)
ApplicationCommandsExtension.Logger.LogDebug("Checking qualifying methods");
// qualifies if any method or type qualifies
return ti.DeclaredMethods.Any(xmi => xmi.IsCommandCandidate(out _)) || ti.DeclaredNestedTypes.Any(xti => xti.IsModuleCandidateType());
return targetTypeInfo.DeclaredMethods.Any(xmi => xmi.IsCommandCandidate(out _)) || targetTypeInfo.DeclaredNestedTypes.Any(xti => xti.IsModuleCandidateType());
}

/// <summary>
/// Whether this is a command candidate.
/// </summary>
/// <param name="method">The method.</param>
/// <param name="parameters">The parameters.</param>
internal static bool IsCommandCandidate(this MethodInfo method, out ParameterInfo[] parameters)
internal static bool IsCommandCandidate(this MethodInfo? method, out ParameterInfo[]? parameters)
{
parameters = null;
// check if exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class AutocompleteContext
/// <summary>
/// Gets the guild this interaction was executed in.
/// </summary>
public DiscordGuild Guild { get; internal set; }
public DiscordGuild? Guild { get; internal set; }

/// <summary>
/// Gets the channel this interaction was executed in.
Expand All @@ -63,7 +63,7 @@ public class AutocompleteContext
/// <summary>
/// Gets the member which executed this interaction, or null if the command is in a DM.
/// </summary>
public DiscordMember Member
public DiscordMember? Member
=> this.User is DiscordMember member ? member : null;

/// <summary>
Expand All @@ -84,7 +84,7 @@ public DiscordMember Member
/// <summary>
/// Gets the entitlement sku ids.
/// </summary>
public List<ulong> EntitlementSkuIds { get; internal set; }
public List<ulong>? EntitlementSkuIds { get; internal set; }

/// <summary>
/// Gets the slash command module this interaction was created in.
Expand All @@ -101,7 +101,7 @@ public DiscordMember Member
/// <summary>
/// The options already provided.
/// </summary>
public IReadOnlyList<DiscordInteractionDataOption> Options { get; internal set; }
public IReadOnlyList<DiscordInteractionDataOption>? Options { get; internal set; }

/// <summary>
/// The option to autocomplete.
Expand Down
2 changes: 1 addition & 1 deletion DisCatSharp.ApplicationCommands/Context/BaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public string FullCommandName
/// <see cref="DiscordClient.TryGetPublishedListings(ulong, out IReadOnlyList{DiscordStoreSku})"/> for more information.
/// </summary>
[DiscordInExperiment("Currently in closed beta."), Experimental("We provide this type but can't provide support.")]
public List<ulong> Entitlements { get; internal set; }
public List<ulong>? Entitlements { get; internal set; }

/// <summary>
/// Gets the type of this interaction.
Expand Down
8 changes: 4 additions & 4 deletions DisCatSharp.ApplicationCommands/Context/InteractionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ public sealed class InteractionContext : BaseContext
/// <summary>
/// Gets the users mentioned in the command parameters.
/// </summary>
public IReadOnlyList<DiscordUser> ResolvedUserMentions { get; internal set; }
public IReadOnlyList<DiscordUser>? ResolvedUserMentions { get; internal set; }

/// <summary>
/// Gets the roles mentioned in the command parameters.
/// </summary>
public IReadOnlyList<DiscordRole> ResolvedRoleMentions { get; internal set; }
public IReadOnlyList<DiscordRole>? ResolvedRoleMentions { get; internal set; }

/// <summary>
/// Gets the channels mentioned in the command parameters.
/// </summary>
public IReadOnlyList<DiscordChannel> ResolvedChannelMentions { get; internal set; }
public IReadOnlyList<DiscordChannel>? ResolvedChannelMentions { get; internal set; }

/// <summary>
/// Gets the attachments in the command parameters, if applicable.
/// </summary>
public IReadOnlyList<DiscordAttachment> ResolvedAttachments { get; internal set; }
public IReadOnlyList<DiscordAttachment>? ResolvedAttachments { get; internal set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public class ApplicationCommandsModuleStartupFinishedEventArgs : DiscordEventArg
/// Initializes a new instance of the <see cref="ApplicationCommandsModuleStartupFinishedEventArgs"/> class.
/// </summary>
/// <param name="provider">The provider.</param>
internal ApplicationCommandsModuleStartupFinishedEventArgs(IServiceProvider provider) : base(provider)
internal ApplicationCommandsModuleStartupFinishedEventArgs(IServiceProvider? provider) : base(provider)
{ }
}
58 changes: 25 additions & 33 deletions DisCatSharp.ApplicationCommands/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ public static class ExtensionMethods
/// <param name="config">Configuration to use.</param>
/// <returns>Created <see cref="ApplicationCommandsExtension"/>.</returns>
public static ApplicationCommandsExtension UseApplicationCommands(this DiscordClient client,
ApplicationCommandsConfiguration config = null)
ApplicationCommandsConfiguration? config = null)
{
if (client.GetExtension<ApplicationCommandsExtension>() != null)
throw new InvalidOperationException("Application commands are already enabled for that client.");

var scomm = new ApplicationCommandsExtension(config);
client.AddExtension(scomm);
return scomm;
var applicationCommandsExtension = new ApplicationCommandsExtension(config);
client.AddExtension(applicationCommandsExtension);
return applicationCommandsExtension;
}

/// <summary>
/// Gets the application commands module for this client.
/// </summary>
/// <param name="client">Client to get application commands for.</param>
/// <returns>The module, or null if not activated.</returns>
public static ApplicationCommandsExtension GetApplicationCommands(this DiscordClient client)
public static ApplicationCommandsExtension? GetApplicationCommands(this DiscordClient client)
=> client.GetExtension<ApplicationCommandsExtension>();

/// <summary>
Expand All @@ -69,10 +69,7 @@ public static ApplicationCommandsExtension GetApplicationCommands(this DiscordCl
public static async Task<IReadOnlyDictionary<int, ApplicationCommandsExtension>> GetApplicationCommandsAsync(this DiscordShardedClient client)
{
await client.InitializeShardsAsync().ConfigureAwait(false);
var modules = new Dictionary<int, ApplicationCommandsExtension>();
foreach (var shard in client.ShardClients.Values)
modules.Add(shard.ShardId, shard.GetExtension<ApplicationCommandsExtension>());
return modules;
return client.ShardClients.Values.ToDictionary(shard => shard.ShardId, shard => shard.GetExtension<ApplicationCommandsExtension>());
}

/// <summary>
Expand All @@ -81,7 +78,7 @@ public static async Task<IReadOnlyDictionary<int, ApplicationCommandsExtension>>
/// <param name="extensions">Sharding extensions.</param>
/// <typeparam name="T">The command class to register.</typeparam>
/// <param name="translationSetup">A callback to setup translations with.</param>
public static void RegisterGlobalCommands<T>(this IReadOnlyDictionary<int, ApplicationCommandsExtension> extensions, Action<ApplicationCommandsTranslationContext> translationSetup = null) where T : ApplicationCommandsModule
public static void RegisterGlobalCommands<T>(this IReadOnlyDictionary<int, ApplicationCommandsExtension> extensions, Action<ApplicationCommandsTranslationContext>? translationSetup = null) where T : ApplicationCommandsModule
{
foreach (var extension in extensions.Values)
extension.RegisterGlobalCommands<T>(translationSetup);
Expand All @@ -93,7 +90,7 @@ public static void RegisterGlobalCommands<T>(this IReadOnlyDictionary<int, Appli
/// <param name="extensions">Sharding extensions.</param>
/// <param name="type">The <see cref="System.Type"/> of the command class to register.</param>
/// <param name="translationSetup">A callback to setup translations with.</param>
public static void RegisterGlobalCommands(this IReadOnlyDictionary<int, ApplicationCommandsExtension> extensions, Type type, Action<ApplicationCommandsTranslationContext> translationSetup = null)
public static void RegisterGlobalCommands(this IReadOnlyDictionary<int, ApplicationCommandsExtension> extensions, Type type, Action<ApplicationCommandsTranslationContext>? translationSetup = null)
{
if (!typeof(ApplicationCommandsModule).IsAssignableFrom(type))
throw new ArgumentException("Command classes have to inherit from ApplicationCommandsModule", nameof(type));
Expand All @@ -108,7 +105,7 @@ public static void RegisterGlobalCommands(this IReadOnlyDictionary<int, Applicat
/// <param name="extensions">Sharding extensions.</param>
/// <param name="guildId">The guild id to register it on.</param>
/// <param name="translationSetup">A callback to setup translations with.</param>
public static void RegisterGuildCommands<T>(this IReadOnlyDictionary<int, ApplicationCommandsExtension> extensions, ulong guildId, Action<ApplicationCommandsTranslationContext> translationSetup = null) where T : ApplicationCommandsModule
public static void RegisterGuildCommands<T>(this IReadOnlyDictionary<int, ApplicationCommandsExtension> extensions, ulong guildId, Action<ApplicationCommandsTranslationContext>? translationSetup = null) where T : ApplicationCommandsModule
{
foreach (var extension in extensions.Values)
extension.RegisterGuildCommands<T>(guildId, translationSetup);
Expand All @@ -122,7 +119,7 @@ public static void RegisterGuildCommands<T>(this IReadOnlyDictionary<int, Applic
/// <param name="type">The <see cref="System.Type"/> of the command class to register.</param>
/// <param name="guildId">The guild id to register it on.</param>
/// <param name="translationSetup">A callback to setup translations with.</param>
public static void RegisterGuildCommands(this IReadOnlyDictionary<int, ApplicationCommandsExtension> extensions, Type type, ulong guildId, Action<ApplicationCommandsTranslationContext> translationSetup = null)
public static void RegisterGuildCommands(this IReadOnlyDictionary<int, ApplicationCommandsExtension> extensions, Type type, ulong guildId, Action<ApplicationCommandsTranslationContext>? translationSetup = null)
{
if (!typeof(ApplicationCommandsModule).IsAssignableFrom(type))
throw new ArgumentException("Command classes have to inherit from ApplicationCommandsModule", nameof(type));
Expand All @@ -136,7 +133,7 @@ public static void RegisterGuildCommands(this IReadOnlyDictionary<int, Applicati
/// <param name="client">Client to enable application commands on.</param>
/// <param name="config">Configuration to use.</param>
/// <returns>A dictionary of created <see cref="ApplicationCommandsExtension"/> with the key being the shard id.</returns>
public static async Task<IReadOnlyDictionary<int, ApplicationCommandsExtension>> UseApplicationCommandsAsync(this DiscordShardedClient client, ApplicationCommandsConfiguration config = null)
public static async Task<IReadOnlyDictionary<int, ApplicationCommandsExtension>> UseApplicationCommandsAsync(this DiscordShardedClient client, ApplicationCommandsConfiguration? config = null)
{
var modules = new Dictionary<int, ApplicationCommandsExtension>();
await client.InitializeShardsAsync().ConfigureAwait(false);
Expand All @@ -155,25 +152,20 @@ public static async Task<IReadOnlyDictionary<int, ApplicationCommandsExtension>>
/// Gets the name from the <see cref="ChoiceNameAttribute"/> for this enum value.
/// </summary>
/// <returns>The name.</returns>
public static string GetName<T>(this T e) where T : IConvertible
public static string? GetName<T>(this T e) where T : IConvertible
{
if (e is Enum)
{
var type = e.GetType();
var values = Enum.GetValues(type);

foreach (int val in values)
{
if (val == e.ToInt32(CultureInfo.InvariantCulture))
{
var memInfo = type.GetMember(type.GetEnumName(val));

return memInfo[0]
.GetCustomAttributes(typeof(ChoiceNameAttribute), false)
.FirstOrDefault() is ChoiceNameAttribute nameAttribute ? nameAttribute.Name : type.GetEnumName(val);
}
}
}
return null;
if (e is not Enum)
return null;
var type = e.GetType();
var values = Enum.GetValues(type);

return (from int val in values
where val == e.ToInt32(CultureInfo.InvariantCulture)
let memInfo = type.GetMember(type.GetEnumName(val))
select memInfo[0]
.GetCustomAttributes(typeof(ChoiceNameAttribute), false)
.FirstOrDefault() is ChoiceNameAttribute nameAttribute
? nameAttribute.Name
: type.GetEnumName(val)).FirstOrDefault();
}
}
2 changes: 1 addition & 1 deletion DisCatSharp/Entities/Interaction/DiscordInteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public DiscordChannel Channel
/// <see cref="DiscordClient.TryGetPublishedListings(ulong, out IReadOnlyList{DiscordStoreSku})"/> for more information.
/// </summary>
[JsonProperty("entitlements", NullValueHandling = NullValueHandling.Ignore), DiscordInExperiment("Currently in closed beta."), Experimental("We provide this type but can't provide support.")]
public List<ulong> Entitlements { get; internal set; } = new();
public List<ulong>? Entitlements { get; internal set; }

/// <summary>
/// Creates a response to this interaction.
Expand Down
10 changes: 5 additions & 5 deletions DisCatSharp/Entities/Interaction/DiscordInteractionData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ public sealed class DiscordInteractionData : SnowflakeObject
/// Gets the parameters and values of the invoked interaction.
/// </summary>
[JsonProperty("options", NullValueHandling = NullValueHandling.Ignore)]
public IReadOnlyList<DiscordInteractionDataOption> Options { get; internal set; }
public IReadOnlyList<DiscordInteractionDataOption>? Options { get; internal set; }

/// <summary>
/// Gets the component rows (Applicable to modal submits).
/// </summary>
[JsonProperty("components", NullValueHandling = NullValueHandling.Ignore)]
internal List<DiscordActionRowComponentResult> ComponentsInternal { get; set; }
internal List<DiscordActionRowComponentResult>? ComponentsInternal { get; set; }

[JsonIgnore]
public IReadOnlyList<DiscordComponentResult> Components
=> this.ComponentsInternal.Select(x => x.Components[0]).ToList();
public IReadOnlyList<DiscordComponentResult>? Components
=> this.ComponentsInternal?.Select(x => x.Components[0]).ToList();

/// <summary>
/// Gets the Discord snowflake objects resolved from this interaction's arguments.
/// </summary>
[JsonProperty("resolved", NullValueHandling = NullValueHandling.Ignore)]
public DiscordInteractionResolvedCollection Resolved { get; internal set; }
public DiscordInteractionResolvedCollection? Resolved { get; internal set; }

/// <summary>
/// The Id of the component that invoked this interaction, if applicable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public sealed class DiscordInteractionDataOption : ObservableApiObject
/// <para>This can be cast to a <see langword="long"/>, <see langword="bool"></see>, <see langword="string"></see>, <see langword="double"></see> or <see langword="ulong"/> depending on the <see cref="System.Type"/></para>
/// </summary>
[JsonIgnore]
public object Value =>
public object? Value =>
this.Type == ApplicationCommandOptionType.Integer && int.TryParse(this.RawValue, out var raw)
? raw
: this.Type == ApplicationCommandOptionType.Integer
Expand All @@ -85,5 +85,5 @@ public sealed class DiscordInteractionDataOption : ObservableApiObject
/// Gets the additional parameters if this parameter is a subcommand.
/// </summary>
[JsonProperty("options", NullValueHandling = NullValueHandling.Ignore)]
public IReadOnlyList<DiscordInteractionDataOption> Options { get; internal set; }
public IReadOnlyList<DiscordInteractionDataOption>? Options { get; internal set; }
}
2 changes: 1 addition & 1 deletion DisCatSharp/EventArgs/DiscordEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract class DiscordEventArgs : AsyncEventArgs
/// <summary>
/// Initializes a new instance of the <see cref="DiscordEventArgs"/> class.
/// </summary>
protected DiscordEventArgs(IServiceProvider provider)
protected DiscordEventArgs(IServiceProvider? provider)
{
if (provider != null)
this.ServiceProvider = provider.CreateScope().ServiceProvider;
Expand Down
Loading

0 comments on commit ef36a80

Please sign in to comment.