Fix 'unknown error' when setting birthday without guild config

Instead it now gives a known error
This commit is contained in:
Noi 2022-03-30 18:25:12 -07:00
parent c98b99cd62
commit bc2553ffad
2 changed files with 16 additions and 3 deletions

View file

@ -14,6 +14,7 @@ public class BirthdayModule : BotModuleBase {
public const string HelpCmdGet = "Gets a user's birthday."; public const string HelpCmdGet = "Gets a user's birthday.";
public const string HelpCmdNearest = "Get a list of users who recently had or will have a birthday."; public const string HelpCmdNearest = "Get a list of users who recently had or will have a birthday.";
public const string HelpCmdExport = "Generates a text file with all known and available birthdays."; public const string HelpCmdExport = "Generates a text file with all known and available birthdays.";
public const string ErrNotSetFk = $":x: The bot has not yet been set up. Please configure a birthday role."; // foreign key violation
// Note that these methods have largely been copied to BirthdayOverrideModule. Changes here should be reflected there as needed. // Note that these methods have largely been copied to BirthdayOverrideModule. Changes here should be reflected there as needed.
@ -47,10 +48,16 @@ public class BirthdayModule : BotModuleBase {
user.BirthMonth = inmonth; user.BirthMonth = inmonth;
user.BirthDay = inday; user.BirthDay = inday;
user.TimeZone = inzone; user.TimeZone = inzone;
try {
await db.SaveChangesAsync(); await db.SaveChangesAsync();
} catch (Microsoft.EntityFrameworkCore.DbUpdateException e)
when (e.InnerException is Npgsql.PostgresException ex && ex.SqlState == Npgsql.PostgresErrorCodes.ForeignKeyViolation) {
await RespondAsync(ErrNotSetFk);
return;
}
await RespondAsync($":white_check_mark: Your birthday has been set to **{FormatDate(inmonth, inday)}**" + await RespondAsync($":white_check_mark: Your birthday has been set to **{FormatDate(inmonth, inday)}**" +
(inzone == null ? "" : $", with time zone {inzone}") + ".").ConfigureAwait(false); (inzone == null ? "" : $" at time zone **{inzone}**") + ".").ConfigureAwait(false);
} }
[SlashCommand("timezone", HelpCmdSetZone)] [SlashCommand("timezone", HelpCmdSetZone)]

View file

@ -29,7 +29,13 @@ public class BirthdayOverrideModule : BotModuleBase {
if (user.IsNew) db.UserEntries.Add(user); if (user.IsNew) db.UserEntries.Add(user);
user.BirthMonth = inmonth; user.BirthMonth = inmonth;
user.BirthDay = inday; user.BirthDay = inday;
try {
await db.SaveChangesAsync(); 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;
}
await RespondAsync($":white_check_mark: {Common.FormatName(target, false)}'s birthday has been set to " + await RespondAsync($":white_check_mark: {Common.FormatName(target, false)}'s birthday has been set to " +
$"**{FormatDate(inmonth, inday)}**.").ConfigureAwait(false); $"**{FormatDate(inmonth, inday)}**.").ConfigureAwait(false);