Skip to content

Commit

Permalink
Refactor code for readability and clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
FabiChan99 committed Dec 23, 2023
1 parent 86adb59 commit 173b1b0
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/Commands/Moderation/BanRequestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using AGC_Management.Attributes;
using AGC_Management.Utils;
using AGC_Management.Managers;
using DisCatSharp.CommandsNext;
using DisCatSharp.CommandsNext.Attributes;
using DisCatSharp.Entities;
Expand Down Expand Up @@ -201,7 +200,8 @@ public async Task BanRequest(CommandContext ctx, DiscordUser user, [RemainingTex
var semoji = sent ? "<:yes:861266772665040917>" : "<:no:861266772724023296>";
try
{
await ctx.Guild.BanMemberAsync(user.Id, await Helpers.GenerateBannDeleteMessageDays(user.Id), ReasonString);
await ctx.Guild.BanMemberAsync(user.Id, await Helpers.GenerateBannDeleteMessageDays(user.Id),
ReasonString);
var dm = sent ? "" : "";
b_users += $"{user.UsernameWithDiscriminator} | DM: {dm}\n";
}
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Moderation/BanUserCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public async Task BanMember(CommandContext ctx, DiscordUser user, [RemainingText
var semoji = sent ? "<:yes:861266772665040917>" : "<:no:861266772724023296>";
try
{
await ctx.Guild.BanMemberAsync(user.Id, await Helpers.GenerateBannDeleteMessageDays(user.Id), ReasonString);
await ctx.Guild.BanMemberAsync(user.Id, await Helpers.GenerateBannDeleteMessageDays(user.Id),
ReasonString);
var dm = sent ? "" : "";
b_users += $"{user.UsernameWithDiscriminator} | DM: {dm}\n";
}
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Moderation/MultiBanCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ public async Task MultiBan(CommandContext ctx, [RemainingText] string ids_and_re
var semoji = sent ? "<:yes:861266772665040917>" : "<:no:861266772724023296>";
try
{
await ctx.Guild.BanMemberAsync(user.Id, await Helpers.GenerateBannDeleteMessageDays(user.Id), reasonString);
await ctx.Guild.BanMemberAsync(user.Id, await Helpers.GenerateBannDeleteMessageDays(user.Id),
reasonString);
var dm = sent ? "" : "";
b_users += $"{user.UsernameWithDiscriminator} | DM: {dm}\n";
}
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Moderation/MultiBanRequestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ public async Task MultiBanRequest(CommandContext ctx, [RemainingText] string ids
var semoji = sent ? "<:yes:861266772665040917>" : "<:no:861266772724023296>";
try
{
await ctx.Guild.BanMemberAsync(user.Id, await Helpers.GenerateBannDeleteMessageDays(user.Id), ReasonString);
await ctx.Guild.BanMemberAsync(user.Id, await Helpers.GenerateBannDeleteMessageDays(user.Id),
ReasonString);
var dm = sent ? "" : "";
b_users += $"{user.UsernameWithDiscriminator} | DM: {dm}\n";
}
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Moderation/MultiWarnCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ public async Task MultiWarnUser(CommandContext ctx, [RemainingText] string ids_a
{
if (BanEnabled)
{
await ctx.Guild.BanMemberAsync(user, await Helpers.GenerateBannDeleteMessageDays(user.Id), reasonString);
await ctx.Guild.BanMemberAsync(user, await Helpers.GenerateBannDeleteMessageDays(user.Id),
reasonString);
uAction = "Gebannt";
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Moderation/PermaWarnCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public async Task PermaWarnUser(CommandContext ctx, DiscordUser user, [Remaining
{
if (BanEnabled)
{
await ctx.Guild.BanMemberAsync(user, await Helpers.GenerateBannDeleteMessageDays(user.Id), reasonString);
await ctx.Guild.BanMemberAsync(user, await Helpers.GenerateBannDeleteMessageDays(user.Id),
reasonString);
uAction = "Gebannt";
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Moderation/WarnUserCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public async Task WarnUser(CommandContext ctx, DiscordUser user, [RemainingText]
{
if (BanEnabled)
{
await ctx.Guild.BanMemberAsync(user, await Helpers.GenerateBannDeleteMessageDays(user.Id), reasonString);
await ctx.Guild.BanMemberAsync(user, await Helpers.GenerateBannDeleteMessageDays(user.Id),
reasonString);
uAction = "Gebannt";
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/Commands/Utilities/ReasonTemplateManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using AGC_Management.Attributes;
using DisCatSharp.CommandsNext;
#region

using AGC_Management.Attributes;
using AGC_Management.Utils;
using DisCatSharp.CommandsNext;
using DisCatSharp.CommandsNext.Attributes;
using DisCatSharp.Entities;

#endregion

namespace AGC_Management.Commands;

[Group("template")]
Expand All @@ -25,13 +29,15 @@ public async Task ReasonTemplateManager_Help(CommandContext ctx)
eb.AddField(new DiscordEmbedField("-" + key, value, true));
i++;
}

if (i == 0)
{
eb.WithDescription("__Keine Templates vorhanden.__");
}

await ctx.RespondAsync(eb);
}

[Command("add")]
[RequireStaffRole]
public async Task AddTemplate(CommandContext ctx, string key, [RemainingText] string text)
Expand All @@ -46,7 +52,7 @@ public async Task AddTemplate(CommandContext ctx, string key, [RemainingText] st
await ctx.RespondAsync($"Template `{key}` existiert bereits.");
}
}

[Command("remove")]
[RequireStaffRole]
public async Task RemoveTemplate(CommandContext ctx, string key)
Expand All @@ -61,6 +67,4 @@ public async Task RemoveTemplate(CommandContext ctx, string key)
await ctx.RespondAsync($"Template `{key}` existiert nicht.");
}
}

}

}
54 changes: 31 additions & 23 deletions src/Commands/Utilities/UserReportMessage.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using DisCatSharp.ApplicationCommands;
#region

using DisCatSharp.ApplicationCommands;
using DisCatSharp.ApplicationCommands.Attributes;
using DisCatSharp.ApplicationCommands.Context;
using DisCatSharp.Entities;
using DisCatSharp.Enums;
using DisCatSharp.Interactivity.Extensions;

#endregion

namespace AGC_Management.Commands;

public class UserReportMessage : ApplicationCommandsModule
Expand All @@ -14,9 +18,9 @@ public static async Task UserReportMessageCommand(ContextMenuContext ctx)
{
var randomid = new Random();
var cid = randomid.Next(100000, 999999).ToString();

DiscordInteractionModalBuilder modal = new();

/* Disable this for now because Discord doenst support it yet
List<DiscordStringSelectComponentOption> options = new();
options.Add(new DiscordStringSelectComponentOption("Spam", "spam", "Spam"));
Expand All @@ -25,15 +29,15 @@ public static async Task UserReportMessageCommand(ContextMenuContext ctx)
options.Add(new DiscordStringSelectComponentOption("Sonstiges", "sonstiges", "Sonstiges"));
modal.AddSelectComponent(new DiscordStringSelectComponent("report_reason", options, "Kategorie für die Meldung"));
*/

var guild = ctx.Guild;
var role = guild.GetRole(ulong.Parse(BotConfig.GetConfig()["ServerConfig"]["StaffRoleId"]));

var member = await guild.GetMemberAsync(ctx.User.Id);
ulong serverIdToSkip = 818699057878663168; // My test server

var isEventler = false;

foreach (var r in member.Roles)
{
if (r.Name.ToLower().Contains("event manager"))
Expand All @@ -45,11 +49,12 @@ public static async Task UserReportMessageCommand(ContextMenuContext ctx)
if (member.Roles.Contains(role) && (guild.Id != serverIdToSkip && isEventler))
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().WithContent("Moderative Teammitglieder können keine Nachrichten melden!")
new DiscordInteractionResponseBuilder()
.WithContent("Moderative Teammitglieder können keine Nachrichten melden!")
.AsEphemeral());
return;
}


if (ctx.User.Id == ctx.TargetMessage.Author.Id)
{
Expand All @@ -58,30 +63,30 @@ await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
.AsEphemeral());
return;
}


modal.WithTitle("Nachricht melden");
modal.CustomId = cid;
modal.AddTextComponent(new DiscordTextComponent(TextComponentStyle.Paragraph,
label: "Geb uns weitere Infos hierzu", minLength: 4, maxLength: 400));

await ctx.CreateModalResponseAsync(modal);

var interactivity = ctx.Client.GetInteractivity();
var result = await interactivity.WaitForModalAsync(cid, TimeSpan.FromMinutes(5));

if (result.TimedOut)
{
return;
}


var message = ctx.TargetMessage;
var channel = ctx.Channel;

var reportChannelId = ulong.Parse(BotConfig.GetConfig()["ModerationConfig"]["ReportLogId"]);
var reportChannel = guild.GetChannel(reportChannelId);

var teamChannelId = ulong.Parse(BotConfig.GetConfig()["ModerationConfig"]["TeamChatId"]);
var teamChannel = guild.GetChannel(teamChannelId);

Expand All @@ -91,27 +96,30 @@ await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
{
messagecontent = "Kein Textinhalt. Möglicherweise ein Bild oder Sticker oder ähnliches.";
}

var targetMessageLink = message.JumpLink.ToString();
var targetmessagebutton = new DiscordLinkButtonComponent(targetMessageLink, "Zur gemeldeten Nachricht", false);
var targetmessagebutton = new DiscordLinkButtonComponent(targetMessageLink, "Zur gemeldeten Nachricht");
var embed = new DiscordEmbedBuilder()
.WithTitle("Nachricht gemeldet")
.WithDescription($"**Gemeldete Nachricht:**```{messagecontent}```\n**Gemeldet von:**\n{ctx.User.Mention} / {ctx.User.Id}\n\n**Zusätzliche Infos:**\n```{result.Result.Interaction.Data.Components[0].Value}```")
.WithDescription(
$"**Gemeldete Nachricht:**```{messagecontent}```\n**Gemeldet von:**\n{ctx.User.Mention} / {ctx.User.Id}\n\n**Zusätzliche Infos:**\n```{result.Result.Interaction.Data.Components[0].Value}```")
.WithColor(DiscordColor.Red)
.WithFooter($"Gemeldet in #{channel.Name}")
.Build();
var mb = new DiscordMessageBuilder();
mb.AddComponents(targetmessagebutton);
mb.WithEmbed(embed);
var m = await reportChannel.SendMessageAsync(mb);

var targetuser = message.Author;

await result.Result.Interaction.CreateResponseAsync(
InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().WithContent("Danke fürs deinen Report!").AsEphemeral());

await Task.Delay(500);

await teamChannel.SendMessageAsync($"**Neuer Report** \n{ctx.User.Mention} / {ctx.User.Id} hat eine Nachricht von {message.Author.Mention} / {message.Author.Id} gemeldet. {m.JumpLink}");
await teamChannel.SendMessageAsync(
$"**Neuer Report** \n{ctx.User.Mention} / {ctx.User.Id} hat eine Nachricht von {message.Author.Mention} / {message.Author.Id} gemeldet. {m.JumpLink}");
}
}
15 changes: 8 additions & 7 deletions src/Utils/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,11 @@ public static async Task<bool> UserHasClosedPendingTicket(ulong UserId)
await connection.CloseAsync();
return true;
}

await connection.CloseAsync();
return false;
}

public static async Task<bool> UserHasOpenTicket(ulong UserId)
{
string con = TicketDatabaseService.GetConnectionString();
Expand All @@ -245,6 +246,7 @@ public static async Task<bool> UserHasOpenTicket(ulong UserId)
await connection.CloseAsync();
return true;
}

await connection.CloseAsync();
return false;
}
Expand All @@ -257,16 +259,15 @@ public static async Task<int> GenerateBannDeleteMessageDays(ulong UserId)
{
return 0;
}
else if (hasClosedPendingTicket)

if (hasClosedPendingTicket)
{
return 0;
}
else
{
return 7;
}

return 7;
}

public static async Task SendWarnAsChannel(CommandContext ctx, DiscordUser user, DiscordEmbed uembed, string caseid)
{
DiscordEmbed userembed = uembed;
Expand Down
1 change: 0 additions & 1 deletion src/Utils/ReasonTemplateResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,5 @@ public static async Task<bool> RemoveReplacement(string key)
return false;
}
}

}
}
2 changes: 1 addition & 1 deletion src/Utils/Ticket/SnippetManagerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#region

using System.Text.RegularExpressions;
using AGC_Management.Services;
using AGC_Management.Managers;
using AGC_Management.Services;
using DisCatSharp.Entities;
using DisCatSharp.Enums;
using Npgsql;
Expand Down
3 changes: 0 additions & 3 deletions src/Utils/Ticket/TicketManagerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

using System.Diagnostics;
using System.Text;
using AGC_Management;
using AGC_Management.Components;
using AGC_Management.Enums;
using AGC_Management.Helper;
using AGC_Management.Managers;
using AGC_Management.Services;
using AGC_Management.Utils;
using DisCatSharp;
Expand Down Expand Up @@ -1204,7 +1202,6 @@ public static async Task InsertTransscriptIntoDB(DiscordChannel ticket_channel,
await cmd2.ExecuteNonQueryAsync();
}
}



public static async Task SendTranscriptToLog(DiscordChannel channel, string ticket_url,
Expand Down

0 comments on commit 173b1b0

Please sign in to comment.