mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-21 13:54:36 +00:00
Add birthday setting commands
This commit is contained in:
parent
b1af7922af
commit
edec3134be
2 changed files with 73 additions and 115 deletions
73
ApplicationCommands/BirthdayModule.cs
Normal file
73
ApplicationCommands/BirthdayModule.cs
Normal file
|
@ -0,0 +1,73 @@
|
|||
using Discord.Interactions;
|
||||
|
||||
namespace BirthdayBot.ApplicationCommands;
|
||||
|
||||
[Group("birthday", "Commands relating to birthdays.")]
|
||||
public class BirthdayModule : BotModuleBase {
|
||||
public const string HelpRemove = "Removes your birthday information from this bot.";
|
||||
|
||||
[Group("set", "Subcommands for setting birthday information.")]
|
||||
public class SubCmdsBirthdaySet : BotModuleBase {
|
||||
public const string HelpSetBday = "Sets or updates your birthday.";
|
||||
public const string HelpSetZone = "Sets or updates your time zone, when your birthday is already set.";
|
||||
|
||||
[SlashCommand("date", HelpSetBday)]
|
||||
public async Task CmdSetBday([Summary(description: HelpOptDate)] string date,
|
||||
[Summary(description: HelpOptPfxOptional + HelpOptZone)] string? zone = null) {
|
||||
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;
|
||||
}
|
||||
|
||||
string? inzone = null;
|
||||
if (zone != null) {
|
||||
try {
|
||||
inzone = ParseTimeZone(zone);
|
||||
} catch (FormatException e) {
|
||||
await ReplyAsync(e.Message).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var user = await Context.GetGuildUserConfAsync().ConfigureAwait(false);
|
||||
await user.UpdateAsync(inmonth, inday, inzone ?? user.TimeZone).ConfigureAwait(false);
|
||||
|
||||
await RespondAsync($":white_check_mark: Your birthday has been set to **{FormatDate(inmonth, inday)}**" +
|
||||
(inzone == null ? "" : $", with time zone {inzone}") + ".");
|
||||
}
|
||||
|
||||
[SlashCommand("zone", HelpSetZone)]
|
||||
public async Task CmdSetZone([Summary(description: HelpOptZone)] string zone) {
|
||||
var user = await Context.GetGuildUserConfAsync().ConfigureAwait(false);
|
||||
if (!user.IsKnown) {
|
||||
await RespondAsync(":x: You must first set your birthday to use this command.", ephemeral: true).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
string inzone;
|
||||
try {
|
||||
inzone = ParseTimeZone(zone);
|
||||
} catch (FormatException e) {
|
||||
await RespondAsync(e.Message, ephemeral: true).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
await user.UpdateAsync(user.BirthMonth, user.BirthDay, inzone).ConfigureAwait(false);
|
||||
await RespondAsync($":white_check_mark: Your time zone has been set to **{inzone}**.").ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
[SlashCommand("remove", HelpRemove)]
|
||||
public async Task CmdRemove() {
|
||||
var user = await Context.GetGuildUserConfAsync().ConfigureAwait(false);
|
||||
if (user.IsKnown) {
|
||||
await user.DeleteAsync().ConfigureAwait(false);
|
||||
await RespondAsync(":white_check_mark: Your information for this server has been removed.");
|
||||
} else {
|
||||
await RespondAsync(":white_check_mark: This bot already does not have your birthday for this server.");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,115 +0,0 @@
|
|||
using BirthdayBot.Data;
|
||||
|
||||
namespace BirthdayBot.ApplicationCommands;
|
||||
|
||||
internal class RegistrationCommands : BotApplicationCommand {
|
||||
#region Help strings
|
||||
public const string HelpSet = "Sets or updates your birthday.";
|
||||
public const string HelpZone = "Sets or updates your time zone. For use only if you have already set a birthday.";
|
||||
public const string HelpZoneDel = "Removes your time zone information from the bot.";
|
||||
public const string HelpDel = "Removes your birthday information from the bot.";
|
||||
|
||||
const string MsgNoData = "This bot does not have your birthday information for this server.";
|
||||
#endregion
|
||||
|
||||
public override IEnumerable<ApplicationCommandProperties> GetCommands() => new ApplicationCommandProperties[] {
|
||||
new SlashCommandBuilder()
|
||||
.WithName("set-birthday")
|
||||
.WithDescription(HelpSet)
|
||||
.AddOption("date", ApplicationCommandOptionType.String, HelpOptDate, isRequired: true)
|
||||
.Build(),
|
||||
new SlashCommandBuilder()
|
||||
.WithName("set-timezone")
|
||||
.WithDescription(HelpZone)
|
||||
.AddOption("zone", ApplicationCommandOptionType.String, HelpOptZone, isRequired: true)
|
||||
.Build(),
|
||||
new SlashCommandBuilder()
|
||||
.WithName("remove-timezone")
|
||||
.WithDescription(HelpZoneDel)
|
||||
.AddOption("zone", ApplicationCommandOptionType.String, HelpOptZone, isRequired: true)
|
||||
.Build(),
|
||||
new SlashCommandBuilder()
|
||||
.WithName("remove-birthday")
|
||||
.WithDescription(HelpDel)
|
||||
.Build()
|
||||
};
|
||||
public override CommandResponder? GetHandlerFor(string commandName) => commandName switch {
|
||||
"set-birthday" => CmdSetBirthday,
|
||||
"set-timezone" => CmdSetTimezone,
|
||||
"remove-timezone" => CmdDelTz,
|
||||
"remove-birthday" => CmdDelBd,
|
||||
_ => null
|
||||
};
|
||||
|
||||
// Note that the following subcommands have largely been copied to RegistrationOverrideCommands.
|
||||
// Any changes made here should be reflected there, if appropriate.
|
||||
|
||||
private static async Task CmdSetBirthday(ShardInstance instance, GuildConfiguration gconf, SocketSlashCommand arg) {
|
||||
int inmonth, inday;
|
||||
try {
|
||||
(inmonth, inday) = ParseDate((string)arg.Data.Options.First().Value);
|
||||
} catch (FormatException e) {
|
||||
// Our parse method's FormatException has its message to send out to Discord.
|
||||
arg.RespondAsync(e.Message).Wait();
|
||||
return;
|
||||
}
|
||||
|
||||
bool known;
|
||||
try {
|
||||
var user = await GuildUserConfiguration.LoadAsync(gconf.GuildId, arg.User.Id).ConfigureAwait(false);
|
||||
known = user.IsKnown;
|
||||
await user.UpdateAsync(inmonth, inday, user.TimeZone).ConfigureAwait(false);
|
||||
} catch (Exception ex) {
|
||||
Program.Log("Error", ex.ToString());
|
||||
arg.RespondAsync(ShardInstance.InternalError).Wait();
|
||||
return;
|
||||
}
|
||||
|
||||
await arg.RespondAsync(":white_check_mark: Your birthday has been " +
|
||||
$"{ (known ? "updated to" : "recorded as") } **{inday:00}-{Common.MonthNames[inmonth]}**.").ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static async Task CmdSetTimezone(ShardInstance instance, GuildConfiguration gconf, SocketSlashCommand arg) {
|
||||
var user = await GuildUserConfiguration.LoadAsync(gconf.GuildId, arg.User.Id).ConfigureAwait(false);
|
||||
if (!user.IsKnown) {
|
||||
await arg.RespondAsync(":x: You must have a birthday set before you can use this command.",
|
||||
ephemeral: true).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
bool hasZone = user.TimeZone != null;
|
||||
|
||||
string inZone;
|
||||
try {
|
||||
inZone = ParseTimeZone((string)arg.Data.Options.First().Value);
|
||||
} catch (Exception e) {
|
||||
arg.RespondAsync(e.Message).Wait();
|
||||
return;
|
||||
}
|
||||
await user.UpdateAsync(user.BirthMonth, user.BirthDay, inZone).ConfigureAwait(false);
|
||||
|
||||
await arg.RespondAsync($":white_check_mark: Your time zone has been { (hasZone ? "updated" : "set") } to **{inZone}**.")
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static async Task CmdDelTz(ShardInstance instance, GuildConfiguration gconf, SocketSlashCommand arg) {
|
||||
var u = await GuildUserConfiguration.LoadAsync(gconf.GuildId, arg.User.Id).ConfigureAwait(false);
|
||||
if (!u.IsKnown) {
|
||||
await arg.RespondAsync(":white_check_mark: " + MsgNoData);
|
||||
} else if (u.TimeZone is null) {
|
||||
await arg.RespondAsync(":white_check_mark: You do not have any time zone information.");
|
||||
} else {
|
||||
await u.UpdateAsync(u.BirthMonth, u.BirthDay, null);
|
||||
await arg.RespondAsync(":white_check_mark: Your time zone information has been removed.");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task CmdDelBd(ShardInstance instance, GuildConfiguration gconf, SocketSlashCommand arg) {
|
||||
var u = await GuildUserConfiguration.LoadAsync(gconf.GuildId, arg.User.Id).ConfigureAwait(false);
|
||||
if (u.IsKnown) {
|
||||
await u.DeleteAsync().ConfigureAwait(false);
|
||||
await arg.RespondAsync(":white_check_mark: Your birthday information has been removed.");
|
||||
} else {
|
||||
await arg.RespondAsync(":white_check_mark: " + MsgNoData);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue