Skip to content

Commit

Permalink
Merge hacktober changes into main (#207)
Browse files Browse the repository at this point in the history
Signed-off-by: Lala Sabathil <aiko@aitsys.dev>
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Nyuw~ <nyuw@aitsys.dev>
Co-authored-by: maxximum <maxximum2006@gmail.com>
Co-authored-by: Mira <mira@aitsys.dev>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
5 people committed Nov 1, 2022
1 parent 4deaf96 commit 9d7a65b
Show file tree
Hide file tree
Showing 149 changed files with 2,438 additions and 977 deletions.
16 changes: 12 additions & 4 deletions .devcontainer/devcontainer.json
@@ -1,9 +1,17 @@
{
"image": "mcr.microsoft.com/devcontainers/universal:2",
"features": {
"ghcr.io/devcontainers/features/common-utils:1",
"ghcr.io/devcontainers/features/github-cli:1",
"ghcr.io/devcontainers/features/git-lfs:1",
"ghcr.io/devcontainers/features/sshd:1"
"ghcr.io/devcontainers/features/common-utils:1": {
"version": "latest"
},
"ghcr.io/devcontainers/features/github-cli:1": {
"version": "latest"
},
"ghcr.io/devcontainers/features/git-lfs:1": {
"version": "latest"
},
"ghcr.io/devcontainers/features/sshd:1": {
"version": "latest"
}
}
}
18 changes: 9 additions & 9 deletions .github/workflows/docs.yml
@@ -1,7 +1,7 @@
name: "Documentation"
on:
push:
branches: [ main ]
branches: [ main, hacktober/main ]
workflow_dispatch:

env:
Expand All @@ -19,7 +19,7 @@ jobs:
with:
path: DisCatSharp
- name: Setup .NET
uses: actions/setup-dotnet@v3.0.2
uses: actions/setup-dotnet@v3.0.3
with:
dotnet-version: 6.0.400
- name: Restore packages
Expand Down Expand Up @@ -60,14 +60,14 @@ jobs:
- name: Purge old docs
working-directory: DisCatSharp.Docs
shell: pwsh
run: Get-ChildItem -Exclude .git* | Remove-Item -Recurse -Force
run: Get-ChildItem -Exclude .git*,.htaccess | Remove-Item -Recurse -Force
- name: Extract new docs
shell: pwsh
run: Expand-Archive -Path dcs-docs.zip DisCatSharp.Docs/
- name: Delete packed docs
uses: geekyeggo/delete-artifact@v2
with:
name: dcs-docs.zip
#- name: Delete packed docs
# uses: geekyeggo/delete-artifact@v2
# with:
# name: dcs-docs.zip
- name: Commit and push changes
uses: EndBug/add-and-commit@main
with:
Expand All @@ -90,7 +90,7 @@ jobs:
ssh-private-key: ${{ secrets.AITSYS_SSH }}
- name: Publish on server
continue-on-error: true
run: ssh -o StrictHostKeyChecking=no -T root@80.153.182.68 -f 'cd /var/www/dcs.aitsys.dev/docs && git pull -f'
run: ssh -o StrictHostKeyChecking=no -T root@80.153.182.68 -f 'cd /var/www/dcs.aitsys.dev/docs && git pull -f && service apache2 restart'
publish-backup:
runs-on: ubuntu-latest
name: Publish documentation on backup server
Expand All @@ -102,4 +102,4 @@ jobs:
ssh-private-key: ${{ secrets.AITSYS_SSH }}
- name: Publish on server
continue-on-error: true
run: ssh -o StrictHostKeyChecking=no -T root@207.180.240.241 -f 'cd /var/www/dcsdocs && git pull -f'
run: ssh -o StrictHostKeyChecking=no -T root@207.180.240.241 -f 'cd /var/www/dcsdocs && git pull -f && service apache2 restart'
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3.0.2
uses: actions/setup-dotnet@v3.0.3
with:
dotnet-version: ${{ matrix.dotnet }}
- name: Restore dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_docs.yml
Expand Up @@ -19,7 +19,7 @@ jobs:
with:
path: DisCatSharp
- name: Setup .NET
uses: actions/setup-dotnet@v3.0.2
uses: actions/setup-dotnet@v3.0.3
with:
dotnet-version: 6.0.400
- name: Restore packages
Expand Down
64 changes: 30 additions & 34 deletions DisCatSharp.ApplicationCommands/ApplicationCommandsExtension.cs
Expand Up @@ -109,21 +109,21 @@ public sealed class ApplicationCommandsExtension : BaseExtension
/// </summary>
public IReadOnlyList<KeyValuePair<ulong?, IReadOnlyList<DiscordApplicationCommand>>> RegisteredCommands
=> s_registeredCommands;
private static readonly List<KeyValuePair<ulong?, IReadOnlyList<DiscordApplicationCommand>>> s_registeredCommands = new();
private static List<KeyValuePair<ulong?, IReadOnlyList<DiscordApplicationCommand>>> s_registeredCommands = new();

/// <summary>
/// Gets a list of registered global commands.
/// </summary>
public IReadOnlyList<DiscordApplicationCommand> GlobalCommands
=> GlobalCommandsInternal;
internal static readonly List<DiscordApplicationCommand> GlobalCommandsInternal = new();
internal static List<DiscordApplicationCommand> GlobalCommandsInternal = new();

/// <summary>
/// Gets a list of registered guild commands mapped by guild id.
/// </summary>
public IReadOnlyDictionary<ulong, IReadOnlyList<DiscordApplicationCommand>> GuildCommands
=> GuildCommandsInternal;
internal static readonly Dictionary<ulong, IReadOnlyList<DiscordApplicationCommand>> GuildCommandsInternal = new();
internal static Dictionary<ulong, IReadOnlyList<DiscordApplicationCommand>> GuildCommandsInternal = new();

/// <summary>
/// Gets the registration count.
Expand All @@ -138,7 +138,7 @@ public IReadOnlyList<DiscordApplicationCommand> GlobalCommands
/// <summary>
/// Gets the guild ids where the applications.commands scope is missing.
/// </summary>
private IReadOnlyList<ulong> _missingScopeGuildIds;
private List<ulong> _missingScopeGuildIds;

/// <summary>
/// Gets whether debug is enabled.
Expand Down Expand Up @@ -449,7 +449,7 @@ internal async Task UpdateAsync()
await this.RegisterCommands(updateList.Where(x => x.Key == key).Select(x => x.Value).ToList(), key);
}

this._missingScopeGuildIds = failedGuilds;
this._missingScopeGuildIds = new(failedGuilds);

await this._applicationCommandsModuleReady.InvokeAsync(this, new ApplicationCommandsModuleReadyEventArgs(Configuration?.ServiceProvider)
{
Expand Down Expand Up @@ -720,13 +720,9 @@ private async Task RegisterCommands(List<ApplicationCommandsModuleConfiguration>
var actualCommands = regCommands.Distinct().ToList();
commands.AddRange(actualCommands);
GuildCommandsInternal.Add(guildId.Value, actualCommands);
/*
if (client.Guilds.TryGetValue(guildId.Value, out var guild))
{
guild.InternalRegisteredApplicationCommands = new();

if (this.Client.Guilds.TryGetValue(guildId.Value, out var guild))
guild.InternalRegisteredApplicationCommands.AddRange(actualCommands);
}
*/
}
else
{
Expand Down Expand Up @@ -968,10 +964,10 @@ private Task InteractionHandler(DiscordClient client, InteractionCreateEventArgs
var command = e.Interaction.Data.Options[0];
var group = subgroups.First().SubCommands.First(x => x.Name == command.Name);
var method = group.Methods.First(x => x.Key == command.Options.First().Name).Value;
var method = group.Methods.First(x => x.Key == command.Options[0].Name).Value;
this.Client.Logger.LogDebug("Executing {cmd}", method.Name);
var args = await this.ResolveInteractionCommandParameters(e, context, method, e.Interaction.Data.Options[0].Options.First().Options);
var args = await this.ResolveInteractionCommandParameters(e, context, method, e.Interaction.Data.Options[0].Options[0].Options);
await this.RunCommandAsync(context, method, args);
}
Expand Down Expand Up @@ -1064,9 +1060,9 @@ private Task InteractionHandler(DiscordClient client, InteractionCreateEventArgs
else if (subgroups.Any())
{
var command = e.Interaction.Data.Options[0];
var group = subgroups.First().SubCommands.First(x => x.Name == command.Name).Methods.First(x => x.Key == command.Options.First().Name).Value;
var group = subgroups.First().SubCommands.First(x => x.Name == command.Name).Methods.First(x => x.Key == command.Options[0].Name).Value;
var focusedOption = command.Options.First().Options.First(o => o.Focused);
var focusedOption = command.Options[0].Options.First(o => o.Focused);
var option = group.GetParameters().Skip(1).First(p => p.GetCustomAttribute<OptionAttribute>().Name == focusedOption.Name);
var provider = option.GetCustomAttribute<AutocompleteAttribute>().ProviderType;
Expand All @@ -1082,7 +1078,7 @@ private Task InteractionHandler(DiscordClient client, InteractionCreateEventArgs
Guild = e.Interaction.Guild,
Channel = e.Interaction.Channel,
User = e.Interaction.User,
Options = command.Options.First().Options.ToList(),
Options = command.Options[0].Options.ToList(),
FocusedOption = focusedOption,
Locale = e.Interaction.Locale,
GuildLocale = e.Interaction.GuildLocale,
Expand Down Expand Up @@ -1414,19 +1410,19 @@ private async Task RunPreexecutionChecksAsync(MethodInfo method, BaseContext con
if (context is InteractionContext ctx)
{
//Gets all attributes from parent classes as well and stuff
var attributes = new List<SlashCheckBaseAttribute>();
attributes.AddRange(method.GetCustomAttributes<SlashCheckBaseAttribute>(true));
attributes.AddRange(method.DeclaringType.GetCustomAttributes<SlashCheckBaseAttribute>());
var attributes = new List<ApplicationCommandCheckBaseAttribute>();
attributes.AddRange(method.GetCustomAttributes<ApplicationCommandCheckBaseAttribute>(true));
attributes.AddRange(method.DeclaringType.GetCustomAttributes<ApplicationCommandCheckBaseAttribute>());
if (method.DeclaringType.DeclaringType != null)
{
attributes.AddRange(method.DeclaringType.DeclaringType.GetCustomAttributes<SlashCheckBaseAttribute>());
attributes.AddRange(method.DeclaringType.DeclaringType.GetCustomAttributes<ApplicationCommandCheckBaseAttribute>());
if (method.DeclaringType.DeclaringType.DeclaringType != null)
{
attributes.AddRange(method.DeclaringType.DeclaringType.DeclaringType.GetCustomAttributes<SlashCheckBaseAttribute>());
attributes.AddRange(method.DeclaringType.DeclaringType.DeclaringType.GetCustomAttributes<ApplicationCommandCheckBaseAttribute>());
}
}

var dict = new Dictionary<SlashCheckBaseAttribute, bool>();
var dict = new Dictionary<ApplicationCommandCheckBaseAttribute, bool>();
foreach (var att in attributes)
{
//Runs the check and adds the result to a list
Expand All @@ -1440,19 +1436,19 @@ private async Task RunPreexecutionChecksAsync(MethodInfo method, BaseContext con
}
if (context is ContextMenuContext cMctx)
{
var attributes = new List<ContextMenuCheckBaseAttribute>();
attributes.AddRange(method.GetCustomAttributes<ContextMenuCheckBaseAttribute>(true));
attributes.AddRange(method.DeclaringType.GetCustomAttributes<ContextMenuCheckBaseAttribute>());
var attributes = new List<ApplicationCommandCheckBaseAttribute>();
attributes.AddRange(method.GetCustomAttributes<ApplicationCommandCheckBaseAttribute>(true));
attributes.AddRange(method.DeclaringType.GetCustomAttributes<ApplicationCommandCheckBaseAttribute>());
if (method.DeclaringType.DeclaringType != null)
{
attributes.AddRange(method.DeclaringType.DeclaringType.GetCustomAttributes<ContextMenuCheckBaseAttribute>());
attributes.AddRange(method.DeclaringType.DeclaringType.GetCustomAttributes<ApplicationCommandCheckBaseAttribute>());
if (method.DeclaringType.DeclaringType.DeclaringType != null)
{
attributes.AddRange(method.DeclaringType.DeclaringType.DeclaringType.GetCustomAttributes<ContextMenuCheckBaseAttribute>());
attributes.AddRange(method.DeclaringType.DeclaringType.DeclaringType.GetCustomAttributes<ApplicationCommandCheckBaseAttribute>());
}
}

var dict = new Dictionary<ContextMenuCheckBaseAttribute, bool>();
var dict = new Dictionary<ApplicationCommandCheckBaseAttribute, bool>();
foreach (var att in attributes)
{
//Runs the check and adds the result to a list
Expand Down Expand Up @@ -1565,15 +1561,15 @@ private static ApplicationCommandOptionType GetParameterType(Type type)
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <param name="guildId">The optional guild id.</param>
internal static async Task<List<DiscordApplicationCommandOption>> ParseParametersAsync(IEnumerable<ParameterInfo> parameters, ulong? guildId)
internal static async Task<List<DiscordApplicationCommandOption>> ParseParametersAsync(IEnumerable<ParameterInfo> parameters, string commandName, ulong? guildId)
{
var options = new List<DiscordApplicationCommandOption>();
foreach (var parameter in parameters)
{
//Gets the attribute
var optionAttribute = parameter.GetCustomAttribute<OptionAttribute>();
if (optionAttribute == null)
throw new ArgumentException("Arguments must have the Option attribute!");
throw new ArgumentException($"One or more arguments of the command '{commandName}' are missing the Option attribute!");

var minimumValue = parameter.GetCustomAttribute<MinimumValueAttribute>()?.Value ?? null;
var maximumValue = parameter.GetCustomAttribute<MaximumValueAttribute>()?.Value ?? null;
Expand All @@ -1583,9 +1579,9 @@ internal static async Task<List<DiscordApplicationCommandOption>> ParseParameter

var autocompleteAttribute = parameter.GetCustomAttribute<AutocompleteAttribute>();
if (optionAttribute.Autocomplete && autocompleteAttribute == null)
throw new ArgumentException("Autocomplete options must have the Autocomplete attribute!");
throw new ArgumentException($"The command '{commandName}' has autocomplete enabled but is missing an autocomplete attribute!");
if (!optionAttribute.Autocomplete && autocompleteAttribute != null)
throw new ArgumentException("Setting an autocomplete provider requires the option to have autocomplete set to true!");
throw new ArgumentException($"The command '{commandName}' has an autocomplete provider but the option to have autocomplete set to false!");

//Sets the type
var type = parameter.ParameterType;
Expand Down Expand Up @@ -2052,8 +2048,8 @@ await ctx.EditResponseAsync(new DiscordWebhookBuilder()
if (ApplicationCommandsExtension.Configuration.AutoDefer)
await ctx.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(discordEmbed));
else
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().AddEmbed(discordEmbed).AsEphemeral(true));
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().AddEmbed(discordEmbed).AsEphemeral(true));
}
else
{
Expand Down
Expand Up @@ -30,12 +30,12 @@ namespace DisCatSharp.ApplicationCommands.Attributes;
/// <summary>
/// The base class for a pre-execution check for a application command.
/// </summary>
public abstract class SlashCheckBaseAttribute : Attribute
public abstract class ApplicationCommandCheckBaseAttribute : Attribute
{
/// <summary>
/// Checks whether this command can be executed within the current context.
/// </summary>
/// <param name="ctx">The context.</param>
/// <returns>Whether the checks passed.</returns>
public abstract Task<bool> ExecuteChecksAsync(InteractionContext ctx);
public abstract Task<bool> ExecuteChecksAsync(BaseContext ctx);
}

This file was deleted.

Expand Up @@ -34,7 +34,7 @@ namespace DisCatSharp.ApplicationCommands.Attributes;
/// Defines a cooldown for this command. This allows you to define how many times can users execute a specific command
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public sealed class ContextMenuCooldownAttribute : ContextMenuCheckBaseAttribute, ICooldown<ContextMenuContext, ContextMenuCooldownBucket>
public sealed class ContextMenuCooldownAttribute : ApplicationCommandCheckBaseAttribute, ICooldown<BaseContext, ContextMenuCooldownBucket>
{
/// <summary>
/// Gets the maximum number of uses before this command triggers a cooldown for its bucket.
Expand Down Expand Up @@ -75,7 +75,7 @@ public ContextMenuCooldownAttribute(int maxUses, double resetAfter, CooldownBuck
/// </summary>
/// <param name="ctx">Command context to get cooldown bucket for.</param>
/// <returns>Requested cooldown bucket, or null if one wasn't present.</returns>
public ContextMenuCooldownBucket GetBucket(ContextMenuContext ctx)
public ContextMenuCooldownBucket GetBucket(BaseContext ctx)
{
var bid = this.GetBucketId(ctx, out _, out _, out _);
this._buckets.TryGetValue(bid, out var bucket);
Expand All @@ -87,7 +87,7 @@ public ContextMenuCooldownBucket GetBucket(ContextMenuContext ctx)
/// </summary>
/// <param name="ctx">Context for which to calculate the cooldown.</param>
/// <returns>Remaining cooldown, or zero if no cooldown is active.</returns>
public TimeSpan GetRemainingCooldown(ContextMenuContext ctx)
public TimeSpan GetRemainingCooldown(BaseContext ctx)
{
var bucket = this.GetBucket(ctx);
return bucket == null ? TimeSpan.Zero : bucket.RemainingUses > 0 ? TimeSpan.Zero : bucket.ResetsAt - DateTimeOffset.UtcNow;
Expand All @@ -101,7 +101,7 @@ public TimeSpan GetRemainingCooldown(ContextMenuContext ctx)
/// <param name="channelId">ID of the channel with which this bucket is associated.</param>
/// <param name="guildId">ID of the guild with which this bucket is associated.</param>
/// <returns>Calculated bucket ID.</returns>
private string GetBucketId(ContextMenuContext ctx, out ulong userId, out ulong channelId, out ulong guildId)
private string GetBucketId(BaseContext ctx, out ulong userId, out ulong channelId, out ulong guildId)
{
userId = 0ul;
if ((this.BucketType & CooldownBucketType.User) != 0)
Expand All @@ -125,7 +125,7 @@ private string GetBucketId(ContextMenuContext ctx, out ulong userId, out ulong c
/// Executes a check.
/// </summary>
/// <param name="ctx">The command context.</param>
public override async Task<bool> ExecuteChecksAsync(ContextMenuContext ctx)
public override async Task<bool> ExecuteChecksAsync(BaseContext ctx)
{
var bid = this.GetBucketId(ctx, out var usr, out var chn, out var gld);
if (!this._buckets.TryGetValue(bid, out var bucket))
Expand Down
Expand Up @@ -32,7 +32,7 @@ namespace DisCatSharp.ApplicationCommands.Attributes;
/// Defines that usage of this application command is only possible when the bot is granted a specific permission.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
public sealed class ApplicationCommandRequireBotPermissionsAttribute : SlashCheckBaseAttribute
public sealed class ApplicationCommandRequireBotPermissionsAttribute : ApplicationCommandCheckBaseAttribute
{
/// <summary>
/// Gets the permissions required by this attribute.
Expand All @@ -58,7 +58,7 @@ public ApplicationCommandRequireBotPermissionsAttribute(Permissions permissions,
/// <summary>
/// Runs checks.
/// </summary>
public override async Task<bool> ExecuteChecksAsync(InteractionContext ctx)
public override async Task<bool> ExecuteChecksAsync(BaseContext ctx)
{
if (ctx.Guild == null)
return this.IgnoreDms;
Expand Down

0 comments on commit 9d7a65b

Please sign in to comment.