Skip to content

Commit

Permalink
Merge branch 'feat/discatsharp.applicationcommands-cooldowns' of http…
Browse files Browse the repository at this point in the history
…s://github.com/Aiko-IT-Systems/DisCatSharp into feat/discatsharp.applicationcommands-cooldowns

# Conflicts:
#	DisCatSharp.ApplicationCommands/ApplicationCommandsExtension.cs
#	DisCatSharp.ApplicationCommands/Entities/FakeApplicationCommandObjects.cs
  • Loading branch information
TheXorog committed Jan 17, 2024
2 parents f6af29e + bd85f56 commit a88661b
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 165 deletions.
28 changes: 18 additions & 10 deletions DisCatSharp.ApplicationCommands/ApplicationCommandsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -675,20 +675,20 @@ private async Task RegisterCommands(List<ApplicationCommandsModuleConfiguration>
if (scg.Options is not null)
foreach (var sc in scg.Options)
if (sc.Options is null || sc.Options.Count is 0)
cs.Add(new(sc.Name, sc.Description, null, null));
cs.Add(new(sc.Name, sc.Description, null, null, sc.RawNameLocalizations, sc.RawDescriptionLocalizations));
else
cs.Add(new(sc.Name, sc.Description, [.. sc.Options], null));
cgs.Add(new(scg.Name, scg.Description, cs, null));
cs.Add(new(sc.Name, sc.Description, [.. sc.Options], null, sc.RawNameLocalizations, sc.RawDescriptionLocalizations));
cgs.Add(new(scg.Name, scg.Description, cs, null, scg.RawNameLocalizations, scg.RawDescriptionLocalizations));
}

foreach (var sc2 in cmd.Options.Where(x => x.Type is ApplicationCommandOptionType.SubCommand))
if (sc2.Options == null || sc2.Options.Count == 0)
cs2.Add(new(sc2.Name, sc2.Description, null, null));
cs2.Add(new(sc2.Name, sc2.Description, null, null, sc2.RawNameLocalizations, sc2.RawDescriptionLocalizations));
else
cs2.Add(new(sc2.Name, sc2.Description, [.. sc2.Options], null));
cs2.Add(new(sc2.Name, sc2.Description, [.. sc2.Options], null, sc2.RawNameLocalizations, sc2.RawDescriptionLocalizations));
}

cgwsgs.Add(new(cmd.Name, cmd.Description, cgs, cs2, cmd.Type));
cgwsgs.Add(new(cmd.Name, cmd.Description, cgs, cs2, cmd.Type, cmd.RawNameLocalizations, cmd.RawDescriptionLocalizations));
}

if (cgwsgs.Count is not 0)
Expand Down Expand Up @@ -732,12 +732,20 @@ private async Task RegisterCommands(List<ApplicationCommandsModuleConfiguration>
var cs = new List<Command>();
foreach (var cmd in slashCommands.applicationCommands.Where(cmd => cmd.Type is ApplicationCommandType.ChatInput && (cmd.Options is null || !cmd.Options.Any(x => x.Type is ApplicationCommandOptionType.SubCommand or ApplicationCommandOptionType.SubCommandGroup))))
if (cmd.Options == null || cmd.Options.Count == 0)
cs.Add(new(cmd.Name, cmd.Description, null, ApplicationCommandType.ChatInput));
cs.Add(new(cmd.Name, cmd.Description, null, ApplicationCommandType.ChatInput, cmd.RawNameLocalizations, cmd.RawDescriptionLocalizations));
else
cs.Add(new(cmd.Name, cmd.Description, [.. cmd.Options], ApplicationCommandType.ChatInput));
cs.Add(new(cmd.Name, cmd.Description, [.. cmd.Options], ApplicationCommandType.ChatInput, cmd.RawNameLocalizations, cmd.RawDescriptionLocalizations));

if (cs.Count is not 0)
translation.AddRange(cs.Select(c => JsonConvert.DeserializeObject<CommandTranslator>(JsonConvert.SerializeObject(c))!));
//translation.AddRange(cs.Select(c => JsonConvert.DeserializeObject<CommandTranslator>(JsonConvert.SerializeObject(c))!));
{
foreach (var c in cs)
{
var json = JsonConvert.SerializeObject(c);
var obj = JsonConvert.DeserializeObject<CommandTranslator>(json);
translation.Add(obj!);
}
}
}
}

Expand Down Expand Up @@ -910,7 +918,7 @@ private async Task RegisterCommands(List<ApplicationCommandsModuleConfiguration>
RegisteredCommands = GlobalCommandsInternal
}).ConfigureAwait(false);

await this.CheckRegistrationStartup(translation, groupTranslation);
await this.CheckRegistrationStartup(translation, groupTranslation, guildId);
}
catch (NullReferenceException ex)
{
Expand Down
17 changes: 14 additions & 3 deletions DisCatSharp.ApplicationCommands/Entities/ChoiceTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@ internal sealed class ChoiceTranslator
/// Gets the choice name translations.
/// </summary>
[JsonProperty("name_translations")]
internal Dictionary<string, string> NameTranslationsDictionary { get; set; }
internal Dictionary<string, string>? NameTranslationsDictionary { get; set; }

[JsonIgnore]
public DiscordApplicationCommandLocalization NameTranslations
=> new(this.NameTranslationsDictionary);
public DiscordApplicationCommandLocalization? NameTranslations
=> this.NameTranslationsDictionary is not null ? new(this.NameTranslationsDictionary) : null;

internal static ChoiceTranslator FromApplicationCommandChoice(DiscordApplicationCommandOptionChoice option)
{
var translator = new ChoiceTranslator
{
Name = option.Name,
NameTranslationsDictionary = option.RawNameLocalizations
};

return translator;
}
}
14 changes: 7 additions & 7 deletions DisCatSharp.ApplicationCommands/Entities/CommandTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ internal sealed class CommandTranslator
/// Gets the command name translations.
/// </summary>
[JsonProperty("name_translations")]
internal Dictionary<string, string> NameTranslationDictionary { get; set; }
internal Dictionary<string, string>? NameTranslationsDictionary { get; set; }

[JsonIgnore]
public DiscordApplicationCommandLocalization NameTranslations
=> new(this.NameTranslationDictionary);
public DiscordApplicationCommandLocalization? NameTranslations
=> this.NameTranslationsDictionary is not null ? new(this.NameTranslationsDictionary) : null;

/// <summary>
/// Gets the command description translations.
/// </summary>
[JsonProperty("description_translations")]
internal Dictionary<string, string> DescriptionTranslationDictionary { get; set; }
internal Dictionary<string, string>? DescriptionTranslationsDictionary { get; set; }

[JsonIgnore]
public DiscordApplicationCommandLocalization DescriptionTranslations
=> new(this.DescriptionTranslationDictionary);
public DiscordApplicationCommandLocalization? DescriptionTranslations
=> this.DescriptionTranslationsDictionary is not null ? new(this.DescriptionTranslationsDictionary) : null;

/// <summary>
/// Gets the option translators, if applicable.
/// </summary>
[JsonProperty("options", NullValueHandling = NullValueHandling.Ignore)]
public List<OptionTranslator> Options { get; set; }
public List<OptionTranslator>? Options { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;

using DisCatSharp.Entities;
using DisCatSharp.Enums;
Expand All @@ -15,8 +16,8 @@ internal sealed class CommandGroupWithSubGroups : BaseCommand
[JsonProperty("commands")]
internal List<Command> Commands { get; set; }

internal CommandGroupWithSubGroups(string name, string description, List<CommandGroup> subGroups, List<Command> commands, ApplicationCommandType type)
: base(name, description, type)
internal CommandGroupWithSubGroups(string name, string description, List<CommandGroup> subGroups, List<Command> commands, ApplicationCommandType type, Dictionary<string, string>? nameTranslations = null, Dictionary<string, string>? descriptionTranslations = null)
: base(name, description, type, nameTranslations, descriptionTranslations)
{
this.SubGroups = subGroups;
this.Commands = commands;
Expand All @@ -28,8 +29,8 @@ internal sealed class CommandGroup : BaseCommand
[JsonProperty("commands")]
internal List<Command> Commands { get; set; }

internal CommandGroup(string name, string description, List<Command> commands, ApplicationCommandType? type = null)
: base(name, description, type)
internal CommandGroup(string name, string description, List<Command> commands, ApplicationCommandType? type = null, Dictionary<string, string>? nameTranslations = null, Dictionary<string, string>? descriptionTranslations = null)
: base(name, description, type, nameTranslations, descriptionTranslations)
{
this.Commands = commands;
}
Expand All @@ -38,12 +39,13 @@ internal CommandGroup(string name, string description, List<Command> commands, A
internal sealed class Command : BaseCommand
{
[JsonProperty("options")]
internal List<DiscordApplicationCommandOption>? Options { get; set; }
internal List<OptionTranslator>? Options { get; set; }

internal Command(string name, string? description = null, List<DiscordApplicationCommandOption>? options = null, ApplicationCommandType? type = null)
: base(name, description, type)
internal Command(string name, string? description = null, List<DiscordApplicationCommandOption>? options = null, ApplicationCommandType? type = null, Dictionary<string, string>? nameTranslations = null, Dictionary<string, string>? descriptionTranslations = null)
: base(name, description, type, nameTranslations, descriptionTranslations)
{
this.Options = options;
if (options is not null)
this.Options = options.Select(OptionTranslator.FromApplicationCommandOption).ToList();
}
}

Expand All @@ -52,16 +54,24 @@ internal class BaseCommand
[JsonProperty("name")]
internal string Name { get; set; }

[JsonProperty("name_translations")]
internal Dictionary<string, string>? NameTranslations { get; set; }

[JsonProperty("description", NullValueHandling = NullValueHandling.Ignore)]
internal string? Description { get; set; }

[JsonProperty("description_translations", NullValueHandling = NullValueHandling.Ignore)]
internal Dictionary<string, string>? DescriptionTranslations { get; set; }

[JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)]
internal ApplicationCommandType? Type { get; set; }

internal BaseCommand(string name, string? description = null, ApplicationCommandType? type = null)
internal BaseCommand(string name, string? description = null, ApplicationCommandType? type = null, Dictionary<string, string>? nameTranslations = null, Dictionary<string, string>? descriptionTranslations = null)
{
this.Name = name;
this.Type = type;
this.Description = description;
this.NameTranslations = nameTranslations;
this.DescriptionTranslations = descriptionTranslations;
}
}
12 changes: 6 additions & 6 deletions DisCatSharp.ApplicationCommands/Entities/GroupTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ internal sealed class GroupTranslator
/// Gets the group name translations.
/// </summary>
[JsonProperty("name_translations")]
internal Dictionary<string, string> NameTranslationsDictionary { get; set; }
internal Dictionary<string, string>? NameTranslationsDictionary { get; set; }

[JsonIgnore]
public DiscordApplicationCommandLocalization NameTranslations
=> new(this.NameTranslationsDictionary);
public DiscordApplicationCommandLocalization? NameTranslations
=> this.NameTranslationsDictionary is not null ? new(this.NameTranslationsDictionary) : null;

/// <summary>
/// Gets the group description translations.
/// </summary>
[JsonProperty("description_translations")]
internal Dictionary<string, string> DescriptionTranslationsDictionary { get; set; }
internal Dictionary<string, string>? DescriptionTranslationsDictionary { get; set; }

[JsonIgnore]
public DiscordApplicationCommandLocalization DescriptionTranslations
=> new(this.DescriptionTranslationsDictionary);
public DiscordApplicationCommandLocalization? DescriptionTranslations
=> this.DescriptionTranslationsDictionary is not null ? new(this.DescriptionTranslationsDictionary) : null;

/// <summary>
/// Gets the sub group translators, if applicable.
Expand Down
34 changes: 26 additions & 8 deletions DisCatSharp.ApplicationCommands/Entities/OptionTranslator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;

using DisCatSharp.Entities;
using DisCatSharp.Enums;
Expand Down Expand Up @@ -28,31 +29,48 @@ internal sealed class OptionTranslator
/// Gets the option type
/// </summary>
[JsonProperty("type")]
public ApplicationCommandOptionType? Type { get; set; }
public ApplicationCommandOptionType Type { get; set; }

/// <summary>
/// Gets the option name translations.
/// </summary>
[JsonProperty("name_translations")]
internal Dictionary<string, string> NameTranslationsDictionary { get; set; }
internal Dictionary<string, string>? NameTranslationsDictionary { get; set; }

[JsonIgnore]
public DiscordApplicationCommandLocalization NameTranslations
=> new(this.NameTranslationsDictionary);
public DiscordApplicationCommandLocalization? NameTranslations
=> this.NameTranslationsDictionary is not null ? new(this.NameTranslationsDictionary) : null;

/// <summary>
/// Gets the option description translations.
/// </summary>
[JsonProperty("description_translations")]
internal Dictionary<string, string> DescriptionTranslationsDictionary { get; set; }
internal Dictionary<string, string>? DescriptionTranslationsDictionary { get; set; }

[JsonIgnore]
public DiscordApplicationCommandLocalization DescriptionTranslations
=> new(this.DescriptionTranslationsDictionary);
public DiscordApplicationCommandLocalization? DescriptionTranslations
=> this.DescriptionTranslationsDictionary is not null ? new(this.DescriptionTranslationsDictionary) : null;

/// <summary>
/// Gets the choice translators, if applicable.
/// </summary>
[JsonProperty("choices", NullValueHandling = NullValueHandling.Ignore)]
public List<ChoiceTranslator> Choices { get; set; }
public List<ChoiceTranslator>? Choices { get; set; }

internal static OptionTranslator FromApplicationCommandOption(DiscordApplicationCommandOption option)
{
var optionTranslator = new OptionTranslator
{
Name = option.Name,
Description = option.Description,
Type = option.Type,
NameTranslationsDictionary = option.RawNameLocalizations,
DescriptionTranslationsDictionary = option.RawDescriptionLocalizations
};

if (option.Choices is not null)
optionTranslator.Choices = option.Choices.Select(ChoiceTranslator.FromApplicationCommandChoice).ToList();

return optionTranslator;
}
}
12 changes: 6 additions & 6 deletions DisCatSharp.ApplicationCommands/Entities/SubGroupTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ internal sealed class SubGroupTranslator
/// Gets the sub group name translations.
/// </summary>
[JsonProperty("name_translations")]
internal Dictionary<string, string> NameTranslationsDictionary { get; set; }
internal Dictionary<string, string>? NameTranslationsDictionary { get; set; }

[JsonIgnore]
public DiscordApplicationCommandLocalization NameTranslations
=> new(this.NameTranslationsDictionary);
public DiscordApplicationCommandLocalization? NameTranslations
=> this.NameTranslationsDictionary is not null ? new(this.NameTranslationsDictionary) : null;

/// <summary>
/// Gets the sub group description translations.
/// </summary>
[JsonProperty("description_translations")]
internal Dictionary<string, string> DescriptionTranslationsDictionary { get; set; }
internal Dictionary<string, string>? DescriptionTranslationsDictionary { get; set; }

[JsonIgnore]
public DiscordApplicationCommandLocalization DescriptionTranslations
=> new(this.DescriptionTranslationsDictionary);
public DiscordApplicationCommandLocalization? DescriptionTranslations
=> this.DescriptionTranslationsDictionary is not null ? new(this.DescriptionTranslationsDictionary) : null;

/// <summary>
/// Gets the command translators.
Expand Down
Loading

0 comments on commit a88661b

Please sign in to comment.