SMSModLogOutputAnalyzer/SMSModLogOutputAnalyzer/Modules/Notes/NotesModule.Commands.cs
Jonathan Riedel 7f08d72ef8 Refactored a bunch of things
Added Notes Module + Data Structure
Added Config Module + Data Structure
Added Role Buttons Module + Data Structure
2024-04-12 02:25:32 +02:00

108 lines
5.1 KiB
C#

using Discord;
using Discord.Interactions;
using SMSModLogOutputAnalyzer.Data;
namespace SMSModLogOutputAnalyzer.Modules
{
public partial class NotesModule
{
[Group("notes", "Various commands related to notes.")]
public partial class Commands : InteractionModuleBase
{
[SlashCommand("view", "View notes that have been added to a user.")]
[RequireUserPermission(GuildPermission.ManageRoles)]
public async Task View(IUser user, [Summary(description: "How many notes to skip (used for pagination)")] int offset = 0)
{
EmbedBuilder embedBuilder = new EmbedBuilder();
var notes = Data[user].Notes.Skip(offset).Take(25).ToArray();
bool addNextPageButton = Data[user].Notes.Count > offset + 25;
if (notes.Length == 0)
{
await RespondAsync(embed: new EmbedBuilder().WithDefaults().WithDescription("No " + (offset > 0 ? "more " : "") + "notes have been added to this user.").Build(), ephemeral: true);
return;
}
embedBuilder.WithAuthor(user);
embedBuilder.WithDefaults();
foreach (var note in notes)
{
embedBuilder.AddField(note.ToField());
}
var embed = embedBuilder.Build();
var button = new ComponentBuilder().WithButton("Next Page", "view_notes:" + user.Id + ":" + (offset + 25)).Build();
if (addNextPageButton)
{
await RespondAsync(embed: embed, components: button, ephemeral: true);
}
else
{
await RespondAsync(embed: embed, ephemeral: true);
}
}
[SlashCommand("add", "Add a note to a user.")]
[RequireUserPermission(GuildPermission.ManageRoles)]
public async Task Add(IUser user, [Summary("note")] string text)
{
if (string.IsNullOrEmpty(text))
{
Console.WriteLine("Sending modal");
await Context.Interaction.RespondWithModalAsync<AddNoteModal>("add_note:" + user.Id);
return;
}
NotesDataObject.Note note = new();
note.Text = text;
note.CreatorID = Context.User.Id;
note.CreationTime = DateTime.Now;
note.Id = ++Data[user].Counter;
Data[user].Notes.Add(note);
Data.Save();
string error = "";
var field = note.ToField();
try
{
await Config.NotesLogChannel.SendMessageAsync(embed: new EmbedBuilder().WithDefaults().WithAuthor(user.Username + " - New Note", user.GetDisplayAvatarUrl()).WithTitle(field.Name).WithDescription((string)field.Value).Build());
} catch (Exception e)
{
error = "\n\n:x: Logging failed";
}
await RespondAsync(embed: new EmbedBuilder().WithDescription("Note **" + note.Id + "** added." + error).WithDefaults().Build(), ephemeral: true);
}
[SlashCommand("remove", "Removes a note from a user.")]
[RequireUserPermission(GuildPermission.ManageRoles)]
public async Task Remove(IUser user, int note_id)
{
int num = Data[user].Notes.RemoveAll(x => x.Id == note_id);
Data.Save();
if (num > 0) await RespondAsync(embed: new EmbedBuilder().WithDescription("Note **" + note_id + "** removed.").WithDefaults().Build(), ephemeral: true);
else await RespondAsync(embed: new EmbedBuilder().WithDescription("Note **" + note_id + "** was not found.").WithDefaults().Build(), ephemeral: true);
}
[SlashCommand("clear", "Removes all notes from a user.")]
[RequireUserPermission(GuildPermission.ManageRoles)]
public async Task Clear(IUser user)
{
int num = Data[user].Notes.RemoveAll(x => true);
Data.Save();
await RespondAsync(embed: new EmbedBuilder().WithDescription("**" + num + "** notes were removed.").WithDefaults().Build(), ephemeral: true);
}
[ModalInteraction("add_note:*", true)]
[RequireUserPermission(ChannelPermission.ManageRoles)]
public async Task AddModal(AddNoteModal modal, string user) => await Add(await Context.Client.GetUserAsync(ulong.Parse(user)), modal.Note);
[ComponentInteraction("view_notes:*:*", true)]
[RequireUserPermission(ChannelPermission.ManageRoles)]
public async Task ViewButton(string user, string offset) => await View(await Context.Client.GetUserAsync(ulong.Parse(user)), int.Parse(offset));
[MessageCommand("View Notes")]
[RequireUserPermission(ChannelPermission.ManageRoles)]
public async Task UserView(IMessage message) => await View(message.Author);
}
}
}