Skip to content

Commit

Permalink
Some minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Plerx2493 committed Sep 19, 2023
1 parent 7f13925 commit 94e4242
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 53 deletions.
2 changes: 1 addition & 1 deletion ModularAssistentForDiscordServer/Commands/Slash/About.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace MADS.Commands.Slash;

public class About : MadsBaseApplicationCommand
public sealed class About : MadsBaseApplicationCommand
{
[SlashCommand("about", "Infos about the bot")]
public async Task AboutCommand(InteractionContext ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace MADS.Commands.Slash;

public class BotStats : MadsBaseApplicationCommand
public sealed class BotStats : MadsBaseApplicationCommand
{
private IDbContextFactory<MadsContext> _contextFactory;
private DiscordRestClient _discordRestClient;
Expand Down
2 changes: 1 addition & 1 deletion ModularAssistentForDiscordServer/Commands/Slash/Jumpad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace MADS.Commands.Slash;

[GuildOnly]
public class Jumppad : MadsBaseApplicationCommand
public sealed class Jumppad : MadsBaseApplicationCommand
{
[SlashCommand("jumppad", "Create a jumppad button"), SlashCommandPermissions(Permissions.MoveMembers)]
public async Task Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace MADS.Commands.Slash;

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

Expand Down Expand Up @@ -60,8 +60,7 @@ private async Task DoSnipeAsync(InteractionContext ctx, bool edit)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace MADS.Commands.Slash;

public class NoBtches : MadsBaseApplicationCommand
public sealed class NoBtches : MadsBaseApplicationCommand
{
[SlashCommand("nobtches", "nobtches api")]
public async Task PingCommand
Expand Down
2 changes: 1 addition & 1 deletion ModularAssistentForDiscordServer/Commands/Slash/Ping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace MADS.Commands.Slash;

public class Ping : MadsBaseApplicationCommand
public sealed class Ping : MadsBaseApplicationCommand
{
[SlashCommand("ping", "Get the bot's ping")]
public async Task PingCommand(InteractionContext ctx)
Expand Down
4 changes: 2 additions & 2 deletions ModularAssistentForDiscordServer/Commands/Slash/Purge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace MADS.Commands.Slash;

public class Purge : MadsBaseApplicationCommand
public sealed class Purge : MadsBaseApplicationCommand
{
[SlashCommand("purge", "Purges messages"),
SlashRequirePermissions(Permissions.ManageMessages),
Expand All @@ -32,7 +32,7 @@ public async Task PurgeMessages
{
if (amount > 100)
{
await ctx.CreateResponseAsync("You cannot purge more than 100 messages at once", true);
await CreateResponse_Error("You cannot purge more than 100 messages at once", true);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion ModularAssistentForDiscordServer/Commands/Slash/Quotes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace MADS.Commands.Slash;

[SlashCommandGroup("Quotes", "Commands related to adding and retrieving quotes"), SlashRequireGuild]
public class Quotes : MadsBaseApplicationCommand
public sealed class Quotes : MadsBaseApplicationCommand
{
private QuotesService _quotesService;

Expand Down
13 changes: 5 additions & 8 deletions ModularAssistentForDiscordServer/Commands/Slash/Reminder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
namespace MADS.Commands.Slash;

[SlashCommandGroup("reminder", "mangage reminders")]
public class Reminder : MadsBaseApplicationCommand
public sealed class Reminder : MadsBaseApplicationCommand
{
private ReminderService _reminderService;

Expand All @@ -50,7 +50,7 @@ public async Task AddReminder

if (timeSpan is null)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("Invalid timespan (5s, 3m, 7h, 2d)"));
await EditResponse_Error("Invalid timespan (5s, 3m, 7h, 2d)");
return;
}

Expand All @@ -66,9 +66,7 @@ public async Task AddReminder

await _reminderService.AddReminder(newReminder);

await ctx.EditResponseAsync(
new DiscordWebhookBuilder().WithContent(
$"Reminder created. I will remind you in {Formatter.Timestamp(timeSpan.Value)}"));
await EditResponse_Success($"Reminder created. I will remind you in {Formatter.Timestamp(timeSpan.Value)}");
}

[SlashCommand("list", "list your Reminder")]
Expand Down Expand Up @@ -107,16 +105,15 @@ long id

if (reminder is null)
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("Reminder does not exsists"));
await EditResponse_Error("Reminder does not exists");
return;
}

var success = await _reminderService.TryDeleteById((ulong) id);

if (!success)
{
await ctx.EditResponseAsync(
new DiscordWebhookBuilder().WithContent("Something went wrong. Please try again"));
await EditResponse_Error("Something went wrong. Please try again");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using MADS.Extensions;
using Serilog;

namespace MADS.Commands.Slash;

public class RoleSelection : MadsBaseApplicationCommand
public sealed class RoleSelection : MadsBaseApplicationCommand
{
[SlashCommand("RoleSelection", "Use this command in the channel the message should be posted"),
SlashRequirePermissions(Permissions.ManageRoles),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace MADS.Commands.Slash;

public class StarboardConfig : MadsBaseApplicationCommand
public sealed class StarboardConfig : MadsBaseApplicationCommand
{
private static readonly Regex EmoteRegex = new(@"^<(?<animated>a)?:(?<name>[a-zA-Z0-9_]+?):(?<id>\d+?)>$",
RegexOptions.ECMAScript | RegexOptions.Compiled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace MADS.Commands.Slash;

[SlashCommandGroup("voicealerts", "mangage voicealerts")]
public class VoiceAlerts : MadsBaseApplicationCommand
public sealed class VoiceAlerts : MadsBaseApplicationCommand
{
private VoiceAlertService _voiceAlertService;

Expand Down
14 changes: 8 additions & 6 deletions ModularAssistentForDiscordServer/Commands/Slash/moveEmoji.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace MADS.Commands.Slash;

public class MoveEmoji : MadsBaseApplicationCommand
public sealed class MoveEmoji : MadsBaseApplicationCommand
{
private const string EmojiRegex = @"<a?:(.+?):(\d+)>";

Expand All @@ -38,7 +38,7 @@ await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWith

if (!matches.Any())
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("There are no emojis in your input"));
await EditResponse_Error("There are no emojis in your input");
return;
}

Expand All @@ -47,7 +47,10 @@ await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWith
var animated = matches[0].Value.StartsWith("<a");

if (!ulong.TryParse(split, out var emojiId))
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("⚠️ Failed to fetch your new emoji."));
{
await EditResponse_Error("⚠️ Failed to fetch your new emoji.");
return;
}

List<DiscordGuild> guilds = new();

Expand All @@ -63,8 +66,7 @@ await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWith

if (!guilds.Any())
{
await ctx.EditResponseAsync(
new DiscordWebhookBuilder().WithContent("There are no guilds where you are able to add emojis"));
await EditResponse_Error("There are no guilds where you are able to add emojis");
return;
}

Expand Down Expand Up @@ -104,7 +106,7 @@ await ctx.EditResponseAsync(new DiscordWebhookBuilder
await CopyEmoji(ctx.Client, emojiName, emojiId, animated, guildId);
}

await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("End"));
await EditResponse_Success("Emoji moved");
}

private static async Task CopyEmoji(DiscordClient client, string name, ulong id, bool animated, ulong targetGuild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task EvalCommand(CommandContext ctx, [RemainingText] string code)
var scriptOptions = ScriptOptions.Default;
scriptOptions = scriptOptions.WithImports("System", "System.Collections.Generic", "System.Linq",
"System.Text", "System.Threading.Tasks", "DSharpPlus", "DSharpPlus.Entities", "DSharpPlus.CommandsNext",
"DSharpPlus.Interactivity", "DSharpPlus.Rest", "DSharpPlus.SlashCommands", "DSharpPlus.VoiceNext",
"DSharpPlus.Interactivity", "DSharpPlus.SlashCommands",
"MADS", "Humanizer");
scriptOptions = scriptOptions.WithReferences(AppDomain.CurrentDomain.GetAssemblies()
.Where(assembly =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ public async Task LeaveGuildOwner(CommandContext ctx)
await ctx.Message.DeleteAsync();
await ctx.Guild.LeaveAsync();
}

[Command("test"), Description("Leave given server"), RequireGuild, Hidden, RequireOwner]
public async Task Test(CommandContext ctx)
{
var usr1 = await ctx.Client.GetUserAsync(262722553380864011);
await ctx.RespondAsync(usr1.Username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.1-dev-00910" />
<PackageReference Include="DSharpPlus" Version="4.4.2" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.4.2" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.2" />
<PackageReference Include="DSharpPlus.Rest" Version="4.4.2" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.2" />
<PackageReference Include="DSharpPlus.VoiceNext" Version="4.4.2" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01717" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-01717" />
<PackageReference Include="DSharpPlus.Interactivity" Version="5.0.0-nightly-01717" />
<PackageReference Include="DSharpPlus.Rest" Version="5.0.0-nightly-01717" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="5.0.0-nightly-01717" />
<PackageReference Include="DSharpPlus.VoiceNext" Version="5.0.0-nightly-01717" />
</ItemGroup>
</Project>
22 changes: 6 additions & 16 deletions ModularAssistentForDiscordServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace MADS;

internal static class MainProgram
{
public static void Main()
public async static Task Main()
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
Expand All @@ -55,33 +55,23 @@ public static void Main()
//Create a discordWebhookClient and add the debug webhook from the config.json
var webhookClient = new DiscordWebhookClient();
var webhookUrl = new Uri(config.DiscordWebhook);
webhookClient.AddWebhookAsync(webhookUrl).GetAwaiter().GetResult();

//loop while the bot shouldn't be canceled
//while (!cancellationSource.IsCancellationRequested)
//{
await webhookClient.AddWebhookAsync(webhookUrl);

//Create a new instance of the bot
ModularDiscordBot modularDiscordBot = new();
//execute the bot and catch uncaught exceptions
try
{
modularDiscordBot.RunAsync(cancellationSource.Token).GetAwaiter().GetResult();
await modularDiscordBot.RunAsync(cancellationSource.Token);
}
catch (Exception e)
{
if (e is TaskCanceledException) return;

var _ = LogToWebhookAsync(e);
}

try
{
Task.Delay(10_000, cancellationSource.Token).GetAwaiter().GetResult();
}
catch (TaskCanceledException)
{
}
//}

cancellationSource.Token.WaitHandle.WaitOne();
}

private static bool ValidateConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
var context = await _dbContextFactory.CreateDbContextAsync();
if ((await context.Database.GetPendingMigrationsAsync()).Any())
await context.Database.MigrateAsync();

DiscordActivity act = new("over some Servers", ActivityType.Watching);


Expand Down
4 changes: 3 additions & 1 deletion ModularAssistentForDiscordServer/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"token": "<Token here>",
"token": "<Your Token here>",
"defaultPrefix": "!",
"minmumloglvl": 1,
"databaseConnectionString": "Server=192.168.178.61,Port=3306;Database=MadsDB;User=USR;Password=PWD;",
"databaseConnectionStringQuartz": "Server=192.168.178.61,Port=3306;Database=MadsDB;User=USR;Password=PWD;",
"discordWebhook": null,
"DmProxyChannelId": 0
}

0 comments on commit 94e4242

Please sign in to comment.