Skip to content

Commit

Permalink
wörk 5
Browse files Browse the repository at this point in the history
  • Loading branch information
TheXorog committed Jul 4, 2023
1 parent aaf0983 commit afdb31b
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 31 deletions.
11 changes: 11 additions & 0 deletions DisCatSharp.HybridCommands/Attributes/HybridCommandAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,15 @@ public HybridCommandAttribute(string name, string description, bool dmPermission
this.DmPermission = dmPermission;
this.IsNsfw = isNsfw;
}

/// <summary>
/// Marks this method as hybrid command.
/// </summary>
/// <param name="name">The name of this hybrid command.</param>
/// <param name="description">The description of this hybrid command.</param>
public HybridCommandAttribute(string name, string description)
{
this.Name = name;
this.Description = description;
}
}
15 changes: 12 additions & 3 deletions DisCatSharp.HybridCommands/Context/HybridCommandContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
namespace DisCatSharp.HybridCommands.Context;
public class HybridCommandContext
{
internal HybridCommandContext(CommandContext ctx)
/// <summary>
/// Do not use, required by RunTime-compiled classes.
/// </summary>
public HybridCommandContext(CommandContext ctx)
{
this.ExcutionType = HybridExecutionType.PrefixCommand;

Expand All @@ -60,7 +63,10 @@ internal HybridCommandContext(CommandContext ctx)
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
}

internal HybridCommandContext(InteractionContext ctx)
/// <summary>
/// Do not use, required by RunTime-compiled classes.
/// </summary>
public HybridCommandContext(InteractionContext ctx)
{
this.ExcutionType = HybridExecutionType.SlashCommand;

Expand All @@ -80,7 +86,10 @@ internal HybridCommandContext(InteractionContext ctx)
this.ParentCommandName = ctx.CommandName;
}

internal HybridCommandContext(ContextMenuContext ctx)
/// <summary>
/// Do not use, required by RunTime-compiled classes.
/// </summary>
public HybridCommandContext(ContextMenuContext ctx)
{
this.ExcutionType = HybridExecutionType.ContextMenuCommand;

Expand Down
3 changes: 3 additions & 0 deletions DisCatSharp.HybridCommands/HybridCommandsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,8 @@ public HybridCommandsConfiguration(HybridCommandsConfiguration other)
this.PrefixResolver = other.PrefixResolver;
this.ServiceProvider = other.ServiceProvider;
this.StringPrefixes = other.StringPrefixes;
this.DisableCompilationCache = other.DisableCompilationCache;
this.OutputGeneratedClasses = other.OutputGeneratedClasses;
this.DebugEnabled = other.DebugEnabled;
}
}
102 changes: 92 additions & 10 deletions DisCatSharp.HybridCommands/HybridCommandsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@
using System.Linq;
using DisCatSharp.ApplicationCommands.Context;
using System.Threading.Tasks;
using System.IO;
using DisCatSharp.HybridCommands.Context;
using DisCatSharp.HybridCommands.Attributes;
using System.Collections.Generic;
using DisCatSharp.Enums;
using DisCatSharp.Entities;
using System.Diagnostics;
using DisCatSharp.EventArgs;

namespace DisCatSharp.HybridCommands;
public sealed class HybridCommandsExtension : BaseExtension
Expand All @@ -41,10 +46,12 @@ public sealed class HybridCommandsExtension : BaseExtension

internal static string? ExecutionDirectory { get; set; }

internal List<HybridCommandAttribute> registeredCommands { get; set; } = new();

public static bool DebugEnabled
=> Configuration?.DebugEnabled ?? false;

internal protected override void Setup(DiscordClient client)
internal protected override async void Setup(DiscordClient client)
{
if (this.Client != null)
throw new InvalidOperationException("What did I tell you?");
Expand All @@ -60,6 +67,8 @@ internal protected override void Setup(DiscordClient client)
{
EnableLocalization = Configuration.EnableLocalization,
ServiceProvider = Configuration.ServiceProvider ?? null,
EnableDefaultHelp = false,
AutoDefer = true
});


Expand All @@ -69,8 +78,12 @@ internal protected override void Setup(DiscordClient client)
PrefixResolver = Configuration.PrefixResolver ?? null,
ServiceProvider = Configuration.ServiceProvider ?? null,
StringPrefixes = Configuration.StringPrefixes ?? null,
EnableDefaultHelp = false
});
#pragma warning restore CS8601 // Possible null reference assignment.

if (HybridCommandsExtension.Configuration.EnableDefaultHelp)
await this.RegisterGlobalCommands<DefaultHybridHelp>();
}

/// <summary>
Expand Down Expand Up @@ -116,19 +129,19 @@ public async Task RegisterGlobalCommands(Type type, Action<ApplicationCommandsTr
if (!type.IsModuleCandidateType())
throw new ArgumentException("Command Class is not a valid module candidate.");

foreach (var assembly in await type.CompileCommands())
foreach (var assembly in await type.CompileAndLoadCommands())
{
var commandsNextModule = assembly.DefinedTypes.FirstOrDefault(x => typeof(BaseCommandModule).IsAssignableTo(x), null);
if (commandsNextModule is not null)
{
// this.Client.GetCommandsNext().RegisterCommands(commandsNextModule);
this.Client.GetCommandsNext().RegisterCommands(commandsNextModule);
}

var applicationCommandsModule = assembly.DefinedTypes.FirstOrDefault(x => typeof(ApplicationCommandsModule).IsAssignableTo(x), null);
if (applicationCommandsModule is not null)
{
#pragma warning disable CS8604 // Possible null reference argument.
// this.Client.GetApplicationCommands().RegisterGlobalCommands(applicationCommandsModule, translationSetup);
this.Client.GetApplicationCommands().RegisterGlobalCommands(applicationCommandsModule, translationSetup);
#pragma warning restore CS8604 // Possible null reference argument.
}
}
Expand All @@ -145,19 +158,19 @@ public async Task RegisterGuildCommands(Type type, ulong guildId, Action<Applica
if (!type.IsModuleCandidateType())
throw new ArgumentException("Command Class is not a valid module candidate.");

foreach (var assembly in await type.CompileCommands(guildId))
foreach (var assembly in await type.CompileAndLoadCommands(guildId))
{
var commandsNextModule = assembly.DefinedTypes.FirstOrDefault(x => typeof(BaseCommandModule).IsAssignableTo(x), null);
var commandsNextModule = assembly.DefinedTypes.FirstOrDefault(x => typeof(BaseCommandModule).IsAssignableFrom(x), null);
if (commandsNextModule is not null)
{
// this.Client.GetCommandsNext().RegisterCommands(commandsNextModule);
this.Client.GetCommandsNext().RegisterCommands(commandsNextModule);
}

var applicationCommandsModule = assembly.DefinedTypes.FirstOrDefault(x => typeof(ApplicationCommandsModule).IsAssignableTo(x), null);
var applicationCommandsModule = assembly.DefinedTypes.FirstOrDefault(x => typeof(ApplicationCommandsModule).IsAssignableFrom(x), null);
if (applicationCommandsModule is not null)
{
#pragma warning disable CS8604 // Possible null reference argument.
// this.Client.GetApplicationCommands().RegisterGuildCommands(applicationCommandsModule, guildId, translationSetup);
this.Client.GetApplicationCommands().RegisterGuildCommands(applicationCommandsModule, guildId, translationSetup);
#pragma warning restore CS8604 // Possible null reference argument.
}
}
Expand All @@ -184,8 +197,77 @@ private HybridCommandsExtension() { }

public class DefaultHybridHelp : HybridCommandsModule
{
[HybridCommand("help", "Displays all commands, their usage and description", true, false)]
public async Task HelpAsync(HybridCommandContext ctx)
{
var hybridModule = ctx.Client.GetHybridCommands();
var commandDescriptions = new List<string>();

foreach (var command in hybridModule.registeredCommands)
commandDescriptions.Add($"`{command.Name}` - _{command.Description}_{(command.IsNsfw ? " (**NSFW**)" : "")}");

var splitDescriptions = new List<string>();

var currentBuild = "";
foreach (var description in commandDescriptions)
{
var add = $"{description}\n";

if (add.Length + currentBuild.Length > 2048)
{
splitDescriptions.Add(currentBuild);
currentBuild = "";
}

currentBuild += add;
}

if (currentBuild.Length > 0)
{
splitDescriptions.Add(currentBuild);
currentBuild = "";
}

var embeds = splitDescriptions.Select(x => new DiscordEmbedBuilder
{
Description = x,
Title = "Command list",
Timestamp = DateTime.UtcNow
}).ToList();

var PrevPage = new DiscordButtonComponent(ButtonStyle.Primary, Guid.NewGuid().ToString(), "Previous page", false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("")));
var NextPage = new DiscordButtonComponent(ButtonStyle.Primary, Guid.NewGuid().ToString(), "Next page", false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("")));

Task EditMessage()
=> ctx.RespondOrEditAsync(new DiscordMessageBuilder()
.WithEmbed(embeds![0])
.AddComponents(PrevPage!.Disable(), NextPage!));

await EditMessage();

uint currentIndex = 0;
var sw = Stopwatch.StartNew();
ctx.Client.ComponentInteractionCreated += RunInteraction;

async Task RunInteraction(DiscordClient sender, ComponentInteractionCreateEventArgs e)
{
if (e.Id == PrevPage.CustomId)
currentIndex--;
else if (e.Id == NextPage.CustomId)
currentIndex++;

PrevPage!.SetState(currentIndex <= 0);
NextPage.SetState(currentIndex >= embeds.Count);

await EditMessage();
}

while (sw.Elapsed < TimeSpan.FromSeconds(60))
{
await Task.Delay(1000);
}

ctx.Client.ComponentInteractionCreated -= RunInteraction;
await ctx.RespondOrEditAsync(embeds[(int)currentIndex].WithFooter("Interaction timed out"));
}
}
Loading

0 comments on commit afdb31b

Please sign in to comment.