Skip to content

Commit

Permalink
Start migration to DSP.C:
Browse files Browse the repository at this point in the history
Removed CNext and SL
Migrated most commands
  • Loading branch information
Plerx2493 committed May 7, 2024
1 parent b2d9dfa commit 74f7a64
Show file tree
Hide file tree
Showing 29 changed files with 358 additions and 468 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.Commands.Processors.SlashCommands;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using MADS.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace MADS.Commands.AutoCompletion;

public class ReminderAutoCompletion : IAutocompleteProvider
public class ReminderAutoCompletion : IAutoCompleteProvider
{
private readonly IDbContextFactory<MadsContext> _factory;

Expand All @@ -29,15 +29,15 @@ public ReminderAutoCompletion(IServiceProvider services)
_factory = services.GetRequiredService<IDbContextFactory<MadsContext>>();
}

public async Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteContext ctx)
public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(AutoCompleteContext context)
{
await using MadsContext db = await _factory.CreateDbContextAsync();
IEnumerable<DiscordAutoCompleteChoice> choices = db.Reminders
.Where(x => x.UserId == ctx.User.Id)
.Select(x => x.Id.ToString())
Dictionary<string, object> choices = db.Reminders
.Where(x => x.UserId == context.User.Id)
.Select(x => x.Id)
.Where(x => x.ToString().StartsWith(context.UserInput))
.Take(25)
.Select(x => new DiscordAutoCompleteChoice(x, x))
.ToList();
.ToDictionary(x => x.ToString(), x => (object) x);

return choices;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

using DeepL;
using DeepL.Model;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.Commands.Processors.SlashCommands;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;

namespace MADS.Commands.AutoCompletion;

public class TargetLanguageAutoCompletion : IAutocompleteProvider
public class TargetLanguageAutoCompletion : IAutoCompleteProvider
{
private readonly Translator _translator;

Expand All @@ -28,12 +28,15 @@ public TargetLanguageAutoCompletion(Translator translator)
_translator = translator;
}

public async Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteContext ctx)
public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(AutoCompleteContext context)
{
TargetLanguage[] sourceLangs = await _translator.GetTargetLanguagesAsync();
IEnumerable<DiscordAutoCompleteChoice> choices = sourceLangs
.Select(x => new DiscordAutoCompleteChoice(x.Name, x.Code))
.Take(25);
Dictionary<string, object> choices = sourceLangs
.Where(x => x.ToString().StartsWith(context.UserInput))
.Take(25)
.Select(x => x.ToString())
.ToDictionary(x => x, x => (object) x);

return choices;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using DSharpPlus.Commands.Processors.SlashCommands;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using MADS.Entities;
using MADS.Services;
using Microsoft.Extensions.DependencyInjection;

namespace MADS.Commands.AutoCompletion;

public class VoiceAlertAutoCompletion : IAutocompleteProvider
public class VoiceAlertAutoCompletion : IAutoCompleteProvider
{
private readonly VoiceAlertService _voiceAlertService;

Expand All @@ -29,18 +30,22 @@ public VoiceAlertAutoCompletion(IServiceProvider services)
_voiceAlertService = services.GetRequiredService<VoiceAlertService>();
}

public async Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteContext ctx)
public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(AutoCompleteContext context)
{
IEnumerable<VoiceAlert> choices = await _voiceAlertService.GetVoiceAlerts(ctx.User.Id);

List<DiscordAutoCompleteChoice> result = new();
IEnumerable<VoiceAlert> choices = await _voiceAlertService.GetVoiceAlerts(context.User.Id);

List<DiscordChannel> result = new();
foreach (VoiceAlert choice in choices)
{
DiscordChannel chn = await ctx.Guild.GetChannelAsync(choice.ChannelId);
result.Add(new DiscordAutoCompleteChoice(chn.Name, choice.ChannelId.ToString()));
DiscordChannel channel = await context.Client.GetChannelAsync(choice.ChannelId);
result.Add(channel);
}

return result;
Dictionary<string, object> dict = result
.Where(x => x.Name.StartsWith(context.UserInput))
.Take(25)
.ToDictionary(x => x.Name, x => (object) x.Id);

return dict;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

using System.Text.RegularExpressions;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using MADS.Extensions;

namespace MADS.Commands.ContextMenu;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
using DeepL;
using DeepL.Model;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using MADS.CustomComponents;
using MADS.Extensions;
using MADS.Services;
using Quartz.Util;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using DSharpPlus.SlashCommands;
using Humanizer;
using MADS.Extensions;

namespace MADS.Commands.ContextMenu;

Expand Down
22 changes: 15 additions & 7 deletions ModularAssistentForDiscordServer/Commands/Slash/About.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,33 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.ComponentModel;
using DSharpPlus.Commands;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using Humanizer;
using MADS.Extensions;
using MADS.Services;

namespace MADS.Commands.Slash;

public sealed class About : MadsBaseApplicationCommand
public sealed class About
{
[SlashCommand("about", "Infos about the bot")]
public async Task AboutCommand(InteractionContext ctx)
private readonly DiscordClientService DiscordService;

public About(DiscordClientService service)
{
DiscordService = service;
}

[Command("about"), Description("Infos about the bot")]
public async Task AboutCommand(CommandContext ctx)
{
DiscordEmbedBuilder discordEmbedBuilder = new();
DiscordInteractionResponseBuilder discordMessageBuilder = new();
string inviteUri = ctx.Client.CurrentApplication.GenerateOAuthUri(null, DiscordPermissions.Administrator, DiscordOAuthScope.Bot,
DiscordOAuthScope.ApplicationsCommands);
string addMe = $"[Click here!]({inviteUri.Replace(" ", "%20")})";

TimeSpan diff = DateTime.Now - CommandService.StartTime;
TimeSpan diff = DateTime.Now - DiscordService.StartTime;
string date = $"{diff.Days} days {diff.Hours} hours {diff.Minutes} minutes";

discordEmbedBuilder
Expand All @@ -52,6 +60,6 @@ public async Task AboutCommand(InteractionContext ctx)
"Feedback"));
discordMessageBuilder.AsEphemeral();

await ctx.CreateResponseAsync(DiscordInteractionResponseType.ChannelMessageWithSource, discordMessageBuilder);
await ctx.RespondAsync(discordMessageBuilder);
}
}
15 changes: 9 additions & 6 deletions ModularAssistentForDiscordServer/Commands/Slash/BotStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.ComponentModel;
using System.Diagnostics;
using DSharpPlus;
using DSharpPlus.Commands;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using Humanizer;
using MADS.Entities;
using MADS.Extensions;
using Microsoft.EntityFrameworkCore;

namespace MADS.Commands.Slash;

public sealed class BotStats : MadsBaseApplicationCommand
public sealed class BotStats
{
private readonly IDbContextFactory<MadsContext> _contextFactory;
private readonly DiscordRestClient _discordRestClient;
Expand All @@ -34,8 +34,8 @@ public BotStats(IDbContextFactory<MadsContext> contextFactory, DiscordRestClient
_discordRestClient = discordRestClient;
}

[SlashCommand("botstats", "Get statistics about the bot")]
public async Task GetBotStatsAsync(InteractionContext ctx)
[Command("botstats"), Description("Get statistics about the bot")]
public async Task GetBotStatsAsync(CommandContext ctx)
{
await using MadsContext db = await _contextFactory.CreateDbContextAsync();
Stopwatch swDb = new();
Expand Down Expand Up @@ -73,7 +73,10 @@ public async Task GetBotStatsAsync(InteractionContext ctx)
.AddField("Uptime:",
$"{DateTimeOffset.UtcNow.Subtract(process.StartTime).Humanize(3, minUnit: TimeUnit.Millisecond, maxUnit: TimeUnit.Day)}",
true);

DiscordInteractionResponseBuilder responseBuilder = new();
responseBuilder.AddEmbed(embed).AsEphemeral();

await ctx.CreateResponseAsync(embed, true);
await ctx.RespondAsync(responseBuilder);
}
}
21 changes: 11 additions & 10 deletions ModularAssistentForDiscordServer/Commands/Slash/Jumpad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.ComponentModel;
using DSharpPlus.Commands;
using DSharpPlus.Commands.ContextChecks;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using MADS.CustomComponents;
using MADS.Extensions;
using Quartz.Util;

namespace MADS.Commands.Slash;

[GuildOnly]
public sealed class Jumppad : MadsBaseApplicationCommand
public sealed class Jumppad
{
[SlashCommand("jumppad", "Create a jumppad button"), SlashCommandPermissions(DiscordPermissions.MoveMembers)]
[Command("jumppad"), Description("Create a jumppad button"), RequireGuild, RequirePermissions(DiscordPermissions.MoveMembers)]
public async Task Test
(
InteractionContext ctx,
[Option("originChannel", "Channel where the users will be moved out"), ChannelTypes(DiscordChannelType.Voice)]
CommandContext ctx,
[Description("Channel where the users will be moved out"), SlashChannelTypes(DiscordChannelType.Voice, DiscordChannelType.Stage)]
DiscordChannel originChannel,
[Option("targetChannel", "Channel where the users will be put in"), ChannelTypes(DiscordChannelType.Voice)]
[Description("Channel where the users will be put in"), SlashChannelTypes(DiscordChannelType.Voice, DiscordChannelType.Stage)]
DiscordChannel targetChannel,
[Option("message", "Message to be sent")]
[Description("Message to be sent")]
string? content = null
)
{
Expand All @@ -43,6 +44,6 @@ public async Task Test

message.AddComponents(newButton);
message.WithContent(!content.IsNullOrWhiteSpace() ? content! : "Jumppad");
await ctx.CreateResponseAsync(DiscordInteractionResponseType.ChannelMessageWithSource, message);
await ctx.RespondAsync(message);
}
}
27 changes: 15 additions & 12 deletions ModularAssistentForDiscordServer/Commands/Slash/MessageSnipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.ComponentModel;
using DSharpPlus;
using DSharpPlus.Commands;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using MADS.CustomComponents;
using MADS.Extensions;
using MADS.Services;

namespace MADS.Commands.Slash;

public sealed class MessageSnipe : MadsBaseApplicationCommand
public sealed class MessageSnipe
{
private readonly MessageSnipeService _messageSnipeService;

Expand All @@ -30,29 +31,30 @@ public MessageSnipe(MessageSnipeService messageSnipeService)
_messageSnipeService = messageSnipeService;
}

[SlashCommand("snipe", "Snipes the last deleted message.")]
public async Task SnipeAsync(InteractionContext ctx)
[Command("snipe"), Description("Snipes the last deleted message.")]
public async Task SnipeAsync(CommandContext ctx)
{
await DoSnipeAsync(ctx, false);
}

[SlashCommand("snipeedit", "Snipes the last edited message.")]
public async Task SnipeEditAsync(InteractionContext ctx)
[Command("snipeedit"), Description("Snipes the last edited message.")]
public async Task SnipeEditAsync(CommandContext ctx)
{
await DoSnipeAsync(ctx, true);
}

private async Task DoSnipeAsync(InteractionContext ctx, bool edit)
private async Task DoSnipeAsync(CommandContext ctx, bool edit)
{
await ctx.DeferAsync(true);
await ctx.DeferResponseAsync();

bool result = !edit
? _messageSnipeService.TryGetMessage(ctx.Channel.Id, out DiscordMessage? message)
: _messageSnipeService.TryGetEditedMessage(ctx.Channel.Id, out message);

if (!result || message is null)
{
await EditResponse_Error("⚠️ No message to snipe! Either nothing was deleted, or the message has expired (12 hours)!");
await ctx.EditResponse_Error(
"⚠️ No message to snipe! Either nothing was deleted, or the message has expired (12 hours)!");
return;
}

Expand Down Expand Up @@ -115,12 +117,13 @@ private async Task DoSnipeAsync(InteractionContext ctx, bool edit)
await ctx.EditResponseAsync(response);
}

[SlashCommand("deletesnipe", "Deletes cached messages for this channel.")]
public async Task DeleteSnipeAsync(InteractionContext ctx)
[Command("deletesnipe"), Description("Deletes cached messages for this channel.")]
public async Task DeleteSnipeAsync(CommandContext ctx)
{
await ctx.DeferAsync(true);
_messageSnipeService.DeleteMessage(ctx.Channel.Id);
_messageSnipeService.DeleteEditedMessage(ctx.Channel.Id);
await EditResponse_Success("✅ Snipe cache cleared!");

await ctx.EditResponse_Success("✅ Snipe cache cleared!");
}
}
Loading

0 comments on commit 74f7a64

Please sign in to comment.