BirthdayBot/ApplicationCommands/BirthdayOverrideModule.cs

85 lines
3.8 KiB
C#
Raw Normal View History

2022-03-10 04:36:32 +00:00
using BirthdayBot.Data;
using Discord.Interactions;
using static BirthdayBot.Common;
2022-03-10 04:36:32 +00:00
namespace BirthdayBot.ApplicationCommands;
2022-03-10 23:37:34 +00:00
[Group("override", HelpCmdOverride)]
[DefaultMemberPermissions(GuildPermission.ManageGuild)]
[CommandContextType(InteractionContextType.Guild)]
2022-03-10 04:36:32 +00:00
public class BirthdayOverrideModule : BotModuleBase {
2022-03-10 23:37:34 +00:00
public const string HelpCmdOverride = "Commands to set options for other users.";
2022-03-10 04:36:32 +00:00
const string HelpOptOvTarget = "The user whose data to modify.";
// Note that these methods have largely been copied from BirthdayModule. Changes there should be reflected here as needed.
// TODO possible to use a common base class for shared functionality instead?
[SlashCommand("set-birthday", "Set a user's birthday on their behalf.")]
2024-04-28 08:51:58 +00:00
public async Task OvSetBirthday([Summary(description: HelpOptOvTarget)] SocketGuildUser target,
[Summary(description: HelpOptDate)] string date) {
2022-03-10 04:36:32 +00:00
int inmonth, inday;
try {
(inmonth, inday) = ParseDate(date);
} catch (FormatException e) {
// Our parse method's FormatException has its message to send out to Discord.
await RespondAsync(e.Message, ephemeral: true).ConfigureAwait(false);
return;
}
2022-03-20 08:07:17 +00:00
using var db = new BotDatabaseContext();
var user = target.GetUserEntryOrNew(db);
if (user.IsNew) db.UserEntries.Add(user);
user.BirthMonth = inmonth;
user.BirthDay = inday;
try {
await db.SaveChangesAsync();
} catch (Microsoft.EntityFrameworkCore.DbUpdateException e)
when (e.InnerException is Npgsql.PostgresException ex && ex.SqlState == Npgsql.PostgresErrorCodes.ForeignKeyViolation) {
await RespondAsync(BirthdayModule.ErrNotSetFk);
return;
}
2022-03-10 04:36:32 +00:00
await RespondAsync($":white_check_mark: {FormatName(target, false)}'s birthday has been set to " +
2022-03-10 04:36:32 +00:00
$"**{FormatDate(inmonth, inday)}**.").ConfigureAwait(false);
}
[SlashCommand("set-timezone", "Set a user's time zone on their behalf.")]
2024-04-28 08:51:58 +00:00
public async Task OvSetTimezone([Summary(description: HelpOptOvTarget)] SocketGuildUser target,
[Summary(description: HelpOptZone), Autocomplete<TzAutocompleteHandler>] string zone) {
2022-03-20 08:07:17 +00:00
using var db = new BotDatabaseContext();
var user = target.GetUserEntryOrNew(db);
if (user.IsNew) {
await RespondAsync($":x: {FormatName(target, false)} does not have a birthday set.")
2022-03-10 04:36:32 +00:00
.ConfigureAwait(false);
return;
}
2022-03-20 08:07:17 +00:00
string newzone;
2022-03-10 04:36:32 +00:00
try {
2022-03-20 08:07:17 +00:00
newzone = ParseTimeZone(zone);
2022-03-10 04:36:32 +00:00
} catch (FormatException e) {
await RespondAsync(e.Message, ephemeral: true).ConfigureAwait(false);
return;
}
2022-03-20 08:07:17 +00:00
user.TimeZone = newzone;
await db.SaveChangesAsync();
await RespondAsync($":white_check_mark: {FormatName(target, false)}'s time zone has been set to " +
2022-03-20 08:07:17 +00:00
$"**{newzone}**.").ConfigureAwait(false);
2022-03-10 04:36:32 +00:00
}
[SlashCommand("remove-birthday", "Remove a user's birthday information on their behalf.")]
2024-04-28 08:51:58 +00:00
public async Task OvRemove([Summary(description: HelpOptOvTarget)] SocketGuildUser target) {
2022-03-20 08:07:17 +00:00
using var db = new BotDatabaseContext();
var user = target.GetUserEntryOrNew(db);
2022-03-20 08:07:17 +00:00
if (!user.IsNew) {
db.UserEntries.Remove(user);
await db.SaveChangesAsync();
await RespondAsync($":white_check_mark: {FormatName(target, false)}'s birthday in this server has been removed.")
2022-03-10 04:36:32 +00:00
.ConfigureAwait(false);
} else {
await RespondAsync($":white_check_mark: {FormatName(target, false)}'s birthday is not registered.")
2022-03-10 04:36:32 +00:00
.ConfigureAwait(false);
}
}
}