Various changes

-Remove webhook logging references
--Including config line. Idea is abandoned for now.
-Remove unneeded comments
-Remove diagnostic messages
--Corresponding problems were solved by moving to dedicated hardware
-Update style on all affected files
This commit is contained in:
Noi 2021-12-05 18:58:10 -08:00
parent 3741222c68
commit 578f2545f2
9 changed files with 298 additions and 385 deletions

View file

@ -119,7 +119,6 @@ class BirthdayRoleUpdate : BackgroundService {
else roleKeeps.Add(member.Id);
}
// TODO Can we remove during the iteration instead of after? investigate later...
foreach (var user in roleRemoves) {
await user.RemoveRoleAsync(r).ConfigureAwait(false);
}

View file

@ -64,7 +64,6 @@ class ShardBackgroundWorker : IDisposable {
_tickCount++;
await service.OnTick(_tickCount, _workerCanceller.Token).ConfigureAwait(false);
} catch (Exception ex) when (ex is not TaskCanceledException) {
// TODO webhook log
Instance.Log(nameof(WorkerLoop), $"{CurrentExecutingService} encountered an exception:\n" + ex.ToString());
}
}

View file

@ -5,7 +5,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>3.2.3</Version>
<Version>3.2.4</Version>
<Authors>NoiTheCat</Authors>
</PropertyGroup>
@ -25,7 +25,7 @@
<PackageReference Include="Discord.Net" Version="3.0.0-dev-20210822.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NodaTime" Version="3.0.9" />
<PackageReference Include="Npgsql" Version="6.0.0" />
<PackageReference Include="Npgsql" Version="6.0.1" />
</ItemGroup>
</Project>

View file

@ -1,13 +1,11 @@
using BirthdayBot.Data;
using Newtonsoft.Json.Linq;
using Npgsql;
using System;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using CommandLine;
using CommandLine.Text;
using Newtonsoft.Json.Linq;
using Npgsql;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.RegularExpressions;
namespace BirthdayBot;
@ -22,7 +20,6 @@ class Configuration {
const string KeyShardRange = "ShardRange";
public string BotToken { get; }
public string LogWebhook { get; }
public string? DBotsToken { get; }
public bool QuitOnFails { get; }
@ -41,7 +38,6 @@ class Configuration {
var jc = JObject.Parse(File.ReadAllText(confPath));
BotToken = ReadConfKey<string>(jc, nameof(BotToken), true);
LogWebhook = ReadConfKey<string>(jc, nameof(LogWebhook), true);
DBotsToken = ReadConfKey<string>(jc, nameof(DBotsToken), false);
QuitOnFails = ReadConfKey<bool?>(jc, nameof(QuitOnFails), false) ?? false;

View file

@ -50,41 +50,27 @@ class ShardInstance : IDisposable {
}
/// <summary>
/// Does all necessary steps to stop this shard. This method may block for a few seconds as it waits
/// for the process to finish, but will force its disposal after at most 30 seconds.
/// Does all necessary steps to stop this shard, including canceling background tasks and disconnecting.
/// </summary>
public void Dispose() {
// Unsubscribe from own events
DiscordClient.Log -= Client_Log;
DiscordClient.Ready -= Client_Ready;
DiscordClient.MessageReceived -= Client_MessageReceived;
_background.Dispose();
try {
if (!DiscordClient.LogoutAsync().Wait(15000))
Log("Instance", "Warning: Client has not yet logged out. Continuing cleanup.");
} catch (Exception ex) {
Log("Instance", "Warning: Client threw an exception when logging out: " + ex.Message);
}
try {
if (!DiscordClient.StopAsync().Wait(5000))
Log("Instance", "Warning: Client has not yet stopped. Continuing cleanup.");
} catch (Exception ex) {
Log("Instance", "Warning: Client threw an exception when stopping: " + ex.Message);
}
var clientDispose = Task.Run(DiscordClient.Dispose);
if (!clientDispose.Wait(10000)) Log("Instance", "Warning: Client is hanging on dispose. Will continue.");
else Log("Instance", "Shard instance disposed.");
DiscordClient.LogoutAsync().Wait(5000);
DiscordClient.StopAsync().Wait(5000);
DiscordClient.Dispose();
Log(nameof(ShardInstance), "Shard instance disposed.");
}
public void Log(string source, string message) => Program.Log($"Shard {ShardId:00}] [{source}", message);
#region Event handling
private Task Client_Log(LogMessage arg) {
// TODO revise this some time, filters might need to be modified by now
// Suppress certain messages
if (arg.Message != null) {
// TODO remove below line ideally when D.Net bug is fixed
if (arg.Message.StartsWith("Unknown Dispatch ") || arg.Message.StartsWith("Unknown Channel")) return Task.CompletedTask;
switch (arg.Message) // Connection status messages replaced by ShardManager's output
{
@ -92,23 +78,14 @@ class ShardInstance : IDisposable {
case "Connected":
case "Ready":
case "Failed to resume previous session":
case "Resumed previous session":
case "Disconnecting":
case "Disconnected":
case "WebSocket connection was closed":
case "Server requested a reconnect":
case "Discord.WebSocket.GatewayReconnectException: Server requested a reconnect":
return Task.CompletedTask;
}
//if (arg.Message == "Heartbeat Errored") {
// // Replace this with a custom message; do not show stack trace
// Log("Discord.Net", $"{arg.Severity}: {arg.Message} - {arg.Exception.Message}");
// return Task.CompletedTask;
//}
Log("Discord.Net", $"{arg.Severity}: {arg.Message}");
}
if (arg.Exception != null) Log("Discord.Net", arg.Exception.ToString());
if (arg.Exception != null) Log("Discord.Net exception", arg.Exception.ToString());
return Task.CompletedTask;
}
@ -152,8 +129,7 @@ class ShardInstance : IDisposable {
if (ex is HttpException) return;
Log("Command", ex.ToString());
try {
channel.SendMessageAsync(":x: An unknown error occurred. It has been reported to the bot owner.").Wait();
// TODO webhook report
channel.SendMessageAsync(UserInterface.CommandsCommon.InternalError).Wait();
} catch (HttpException) { } // Fail silently
}
}

View file

@ -17,7 +17,7 @@ internal abstract class CommandsCommon {
public const string BadUserError = ":x: Unable to find user. Specify their `@` mention or their ID.";
public const string ParameterError = ":x: Invalid usage. Refer to how to use the command and try again.";
public const string NoParameterError = ":x: This command does not accept any parameters.";
public const string InternalError = ":x: An internal bot error occurred. The bot maintainer has been notified of the issue.";
public const string InternalError = ":x: An unknown error occurred. If it persists, please notify the bot owner.";
public const string MemberCacheEmptyError = ":warning: Please try the command again.";
public delegate Task CommandHandler(ShardInstance instance, GuildConfiguration gconf,

View file

@ -1,26 +1,20 @@
using BirthdayBot.Data;
using Discord;
using Discord.WebSocket;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace BirthdayBot.UserInterface
{
internal class HelpInfoCommands : CommandsCommon
{
private readonly Embed _helpEmbed;
private readonly Embed _helpConfigEmbed;
namespace BirthdayBot.UserInterface;
public HelpInfoCommands(Configuration cfg) : base(cfg)
{
var embeds = BuildHelpEmbeds();
_helpEmbed = embeds.Item1;
_helpConfigEmbed = embeds.Item2;
}
internal class HelpInfoCommands : CommandsCommon {
private readonly Embed _helpEmbed;
private readonly Embed _helpConfigEmbed;
public override IEnumerable<(string, CommandHandler)> Commands =>
new List<(string, CommandHandler)>() {
public HelpInfoCommands(Configuration cfg) : base(cfg) {
var embeds = BuildHelpEmbeds();
_helpEmbed = embeds.Item1;
_helpConfigEmbed = embeds.Item2;
}
public override IEnumerable<(string, CommandHandler)> Commands =>
new List<(string, CommandHandler)>() {
("help", CmdHelp),
("help-config", CmdHelpConfig),
("helpconfig", CmdHelpConfig),
@ -31,155 +25,137 @@ namespace BirthdayBot.UserInterface
("info", CmdInfo),
("about", CmdInfo),
("invite", CmdInfo)
};
};
private static (Embed, Embed) BuildHelpEmbeds()
{
var cpfx = $"●`{CommandPrefix}";
private static (Embed, Embed) BuildHelpEmbeds() {
var cpfx = $"●`{CommandPrefix}";
// Normal section
var cmdField = new EmbedFieldBuilder()
{
Name = "Commands",
Value = $"{cpfx}help`, `{CommandPrefix}info`, `{CommandPrefix}help-tzdata`\n"
+ $" » Help and informational messages.\n"
+ ListingCommands.DocUpcoming.Export() + "\n"
+ UserCommands.DocSet.Export() + "\n"
+ UserCommands.DocZone.Export() + "\n"
+ UserCommands.DocRemove.Export() + "\n"
+ ListingCommands.DocWhen.Export()
};
var cmdModField = new EmbedFieldBuilder()
{
Name = "Moderator actions",
Value = $"{cpfx}config`\n"
+ $" » Edit bot configuration. See `{CommandPrefix}help-config`.\n"
+ ListingCommands.DocList.Export() + "\n"
+ ManagerCommands.DocOverride.Export()
};
var helpRegular = new EmbedBuilder().AddField(cmdField).AddField(cmdModField);
// Normal section
var cmdField = new EmbedFieldBuilder() {
Name = "Commands",
Value = $"{cpfx}help`, `{CommandPrefix}info`, `{CommandPrefix}help-tzdata`\n"
+ $" » Help and informational messages.\n"
+ ListingCommands.DocUpcoming.Export() + "\n"
+ UserCommands.DocSet.Export() + "\n"
+ UserCommands.DocZone.Export() + "\n"
+ UserCommands.DocRemove.Export() + "\n"
+ ListingCommands.DocWhen.Export()
};
var cmdModField = new EmbedFieldBuilder() {
Name = "Moderator actions",
Value = $"{cpfx}config`\n"
+ $" » Edit bot configuration. See `{CommandPrefix}help-config`.\n"
+ ListingCommands.DocList.Export() + "\n"
+ ManagerCommands.DocOverride.Export()
};
var helpRegular = new EmbedBuilder().AddField(cmdField).AddField(cmdModField);
// Manager section
var mpfx = cpfx + "config ";
var configField1 = new EmbedFieldBuilder()
{
Name = "Basic settings",
Value = $"{mpfx}role (role name or ID)`\n"
+ " » Sets the role to apply to users having birthdays.\n"
+ $"{mpfx}channel (channel name or ID)`\n"
+ " » Sets the announcement channel. Leave blank to disable.\n"
+ $"{mpfx}message (message)`, `{CommandPrefix}config messagepl (message)`\n"
+ $" » Sets a custom announcement message. See `{CommandPrefix}help-message`.\n"
+ $"{mpfx}ping (off|on)`\n"
+ $" » Sets whether to ping the respective users in the announcement message.\n"
+ $"{mpfx}zone (time zone name)`\n"
+ $" » Sets the default server time zone. See `{CommandPrefix}help-tzdata`."
};
var configField2 = new EmbedFieldBuilder()
{
Name = "Access management",
Value = $"{mpfx}modrole (role name, role ping, or ID)`\n"
+ " » Establishes a role for bot moderators. Grants access to `bb.config` and `bb.override`.\n"
+ $"{mpfx}block/unblock (user ping or ID)`\n"
+ " » Prevents or allows usage of bot commands to the given user.\n"
+ $"{mpfx}moderated on/off`\n"
+ " » Prevents or allows using commands for all members excluding moderators."
};
// Manager section
var mpfx = cpfx + "config ";
var configField1 = new EmbedFieldBuilder() {
Name = "Basic settings",
Value = $"{mpfx}role (role name or ID)`\n"
+ " » Sets the role to apply to users having birthdays.\n"
+ $"{mpfx}channel (channel name or ID)`\n"
+ " » Sets the announcement channel. Leave blank to disable.\n"
+ $"{mpfx}message (message)`, `{CommandPrefix}config messagepl (message)`\n"
+ $" » Sets a custom announcement message. See `{CommandPrefix}help-message`.\n"
+ $"{mpfx}ping (off|on)`\n"
+ $" » Sets whether to ping the respective users in the announcement message.\n"
+ $"{mpfx}zone (time zone name)`\n"
+ $" » Sets the default server time zone. See `{CommandPrefix}help-tzdata`."
};
var configField2 = new EmbedFieldBuilder() {
Name = "Access management",
Value = $"{mpfx}modrole (role name, role ping, or ID)`\n"
+ " » Establishes a role for bot moderators. Grants access to `bb.config` and `bb.override`.\n"
+ $"{mpfx}block/unblock (user ping or ID)`\n"
+ " » Prevents or allows usage of bot commands to the given user.\n"
+ $"{mpfx}moderated on/off`\n"
+ " » Prevents or allows using commands for all members excluding moderators."
};
var helpConfig = new EmbedBuilder()
{
Author = new EmbedAuthorBuilder() { Name = $"{CommandPrefix} config subcommands" },
Description = "All the following subcommands are only usable by moderators and server managers."
}.AddField(configField1).AddField(configField2);
var helpConfig = new EmbedBuilder() {
Author = new EmbedAuthorBuilder() { Name = $"{CommandPrefix} config subcommands" },
Description = "All the following subcommands are only usable by moderators and server managers."
}.AddField(configField1).AddField(configField2);
return (helpRegular.Build(), helpConfig.Build());
}
return (helpRegular.Build(), helpConfig.Build());
}
private async Task CmdHelp(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser)
=> await reqChannel.SendMessageAsync(embed: _helpEmbed).ConfigureAwait(false);
private async Task CmdHelp(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser)
=> await reqChannel.SendMessageAsync(embed: _helpEmbed).ConfigureAwait(false);
private async Task CmdHelpConfig(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser)
=> await reqChannel.SendMessageAsync(embed: _helpConfigEmbed).ConfigureAwait(false);
private async Task CmdHelpConfig(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser)
=> await reqChannel.SendMessageAsync(embed: _helpConfigEmbed).ConfigureAwait(false);
private async Task CmdHelpTzdata(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser)
{
const string tzhelp = "You may specify a time zone in order to have your birthday recognized with respect to your local time. "
+ "This bot only accepts zone names from the IANA Time Zone Database (a.k.a. Olson Database).\n\n"
+ "To find your zone: https://xske.github.io/tz/" + "\n"
+ "Interactive map: https://kevinnovak.github.io/Time-Zone-Picker/" + "\n"
+ "Complete list: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones";
var embed = new EmbedBuilder();
embed.AddField(new EmbedFieldBuilder()
{
Name = "Time Zone Support",
Value = tzhelp
});
await reqChannel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
}
private async Task CmdHelpTzdata(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser) {
const string tzhelp = "You may specify a time zone in order to have your birthday recognized with respect to your local time. "
+ "This bot only accepts zone names from the IANA Time Zone Database (a.k.a. Olson Database).\n\n"
+ "To find your zone: https://xske.github.io/tz/" + "\n"
+ "Interactive map: https://kevinnovak.github.io/Time-Zone-Picker/" + "\n"
+ "Complete list: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones";
var embed = new EmbedBuilder();
embed.AddField(new EmbedFieldBuilder() {
Name = "Time Zone Support",
Value = tzhelp
});
await reqChannel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
}
private async Task CmdHelpMessage(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser)
{
const string msghelp = "The `message` and `messagepl` subcommands allow for editing the message sent into the announcement "
+ "channel (defined with `{0}config channel`). This feature is separated across two commands:\n"
+ "●`{0}config message`\n"
+ "●`{0}config messagepl`\n"
+ "The first command sets the message to be displayed when *one* user is having a birthday. The second command sets the "
+ "message for when *two or more* users are having birthdays ('pl' means plural). If only one of the two custom messages "
+ "are defined, it will be used for both cases.\n\n"
+ "To further allow customization, you may place the token `%n` in your message to specify where the name(s) should appear.\n"
+ "Leave the parameter blank to clear or reset the message to its default value.";
const string msghelp2 = "As examples, these are the default announcement messages used by this bot:\n"
+ "`message`: {0}\n" + "`messagepl`: {1}";
var embed = new EmbedBuilder().AddField(new EmbedFieldBuilder()
{
Name = "Custom announcement message",
Value = string.Format(msghelp, CommandPrefix)
}).AddField(new EmbedFieldBuilder()
{
Name = "Examples",
Value = string.Format(msghelp2,
BackgroundServices.BirthdayRoleUpdate.DefaultAnnounce, BackgroundServices.BirthdayRoleUpdate.DefaultAnnouncePl)
});
await reqChannel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
}
private async Task CmdHelpMessage(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser) {
const string msghelp = "The `message` and `messagepl` subcommands allow for editing the message sent into the announcement "
+ "channel (defined with `{0}config channel`). This feature is separated across two commands:\n"
+ "●`{0}config message`\n"
+ "●`{0}config messagepl`\n"
+ "The first command sets the message to be displayed when *one* user is having a birthday. The second command sets the "
+ "message for when *two or more* users are having birthdays ('pl' means plural). If only one of the two custom messages "
+ "are defined, it will be used for both cases.\n\n"
+ "To further allow customization, you may place the token `%n` in your message to specify where the name(s) should appear.\n"
+ "Leave the parameter blank to clear or reset the message to its default value.";
const string msghelp2 = "As examples, these are the default announcement messages used by this bot:\n"
+ "`message`: {0}\n" + "`messagepl`: {1}";
var embed = new EmbedBuilder().AddField(new EmbedFieldBuilder() {
Name = "Custom announcement message",
Value = string.Format(msghelp, CommandPrefix)
}).AddField(new EmbedFieldBuilder() {
Name = "Examples",
Value = string.Format(msghelp2,
BackgroundServices.BirthdayRoleUpdate.DefaultAnnounce, BackgroundServices.BirthdayRoleUpdate.DefaultAnnouncePl)
});
await reqChannel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
}
private async Task CmdInfo(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser)
{
var strStats = new StringBuilder();
var asmnm = System.Reflection.Assembly.GetExecutingAssembly().GetName();
strStats.AppendLine("BirthdayBot v" + asmnm.Version.ToString(3));
//strStats.AppendLine("Server count: " + Discord.Guilds.Count.ToString());
// TODO restore this statistic
strStats.AppendLine("Shard #" + instance.ShardId.ToString("00"));
strStats.AppendLine("Uptime: " + Program.BotUptime);
private async Task CmdInfo(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser) {
var strStats = new StringBuilder();
var asmnm = System.Reflection.Assembly.GetExecutingAssembly().GetName();
strStats.AppendLine("BirthdayBot v" + asmnm.Version!.ToString(3));
//strStats.AppendLine("Server count: " + Discord.Guilds.Count.ToString()); // TODO restore this statistic
strStats.AppendLine("Shard #" + instance.ShardId.ToString("00"));
strStats.AppendLine("Uptime: " + Program.BotUptime);
// TODO fun stats
// current birthdays, total names registered, unique time zones
// TODO fun stats
// current birthdays, total names registered, unique time zones
var embed = new EmbedBuilder()
{
Author = new EmbedAuthorBuilder()
{
Name = "Thank you for using Birthday Bot!",
IconUrl = instance.DiscordClient.CurrentUser.GetAvatarUrl()
},
// TODO this message needs an overhaul
Description = "For more information regarding support, data retention, privacy, and other details, please refer to: "
+ "https://github.com/NoiTheCat/BirthdayBot/blob/master/Readme.md" + "\n\n"
+ "This bot is provided for free, without any paywalled 'premium' features. "
+ "If you've found this bot useful, please consider contributing via the "
+ "bot author's page on Ko-fi: https://ko-fi.com/noithecat."
}.AddField(new EmbedFieldBuilder()
{
Name = "Statistics",
Value = strStats.ToString()
});
await reqChannel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
}
var embed = new EmbedBuilder() {
Author = new EmbedAuthorBuilder() {
Name = "Thank you for using Birthday Bot!",
IconUrl = instance.DiscordClient.CurrentUser.GetAvatarUrl()
},
Description = "For more information regarding support, data retention, privacy, and other details, please refer to: "
+ "https://github.com/NoiTheCat/BirthdayBot/blob/master/Readme.md" + "\n\n"
+ "This bot is provided for free, without any paywalled 'premium' features. "
+ "If you've found this bot useful, please consider contributing via the "
+ "bot author's page on Ko-fi: https://ko-fi.com/noithecat."
}.AddField(new EmbedFieldBuilder() {
Name = "Statistics",
Value = strStats.ToString()
});
await reqChannel.SendMessageAsync(embed: embed.Build()).ConfigureAwait(false);
}
}

View file

@ -129,7 +129,6 @@ internal class ListingCommands : CommandsCommon {
} catch (Exception ex) {
Program.Log("Listing", ex.ToString());
reqChannel.SendMessageAsync(InternalError).Wait();
// TODO webhook report
} finally {
File.Delete(filepath);
}

View file

@ -1,211 +1,179 @@
using BirthdayBot.Data;
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace BirthdayBot.UserInterface
{
internal class UserCommands : CommandsCommon
{
public UserCommands(Configuration db) : base(db) { }
namespace BirthdayBot.UserInterface;
public override IEnumerable<(string, CommandHandler)> Commands
=> new List<(string, CommandHandler)>()
{
internal class UserCommands : CommandsCommon {
public UserCommands(Configuration db) : base(db) { }
public override IEnumerable<(string, CommandHandler)> Commands
=> new List<(string, CommandHandler)>()
{
("set", CmdSet),
("zone", CmdZone),
("remove", CmdRemove)
};
};
#region Date parsing
const string FormatError = ":x: Unrecognized date format. The following formats are accepted, as examples: "
+ "`15-jan`, `jan-15`, `15 jan`, `jan 15`, `15 January`, `January 15`.";
#region Date parsing
const string FormatError = ":x: Unrecognized date format. The following formats are accepted, as examples: "
+ "`15-jan`, `jan-15`, `15 jan`, `jan 15`, `15 January`, `January 15`.";
private static readonly Regex DateParse1 = new(@"^(?<day>\d{1,2})[ -](?<month>[A-Za-z]+)$", RegexOptions.Compiled);
private static readonly Regex DateParse2 = new(@"^(?<month>[A-Za-z]+)[ -](?<day>\d{1,2})$", RegexOptions.Compiled);
private static readonly Regex DateParse1 = new(@"^(?<day>\d{1,2})[ -](?<month>[A-Za-z]+)$", RegexOptions.Compiled);
private static readonly Regex DateParse2 = new(@"^(?<month>[A-Za-z]+)[ -](?<day>\d{1,2})$", RegexOptions.Compiled);
/// <summary>
/// Parses a date input.
/// </summary>
/// <returns>Tuple: month, day</returns>
/// <exception cref="FormatException">
/// Thrown for any parsing issue. Reason is expected to be sent to Discord as-is.
/// </exception>
private static (int, int) ParseDate(string dateInput)
{
var m = DateParse1.Match(dateInput);
if (!m.Success)
{
// Flip the fields around, try again
m = DateParse2.Match(dateInput);
if (!m.Success) throw new FormatException(FormatError);
}
int day, month;
string monthVal;
try
{
day = int.Parse(m.Groups["day"].Value);
}
catch (FormatException)
{
throw new Exception(FormatError);
}
monthVal = m.Groups["month"].Value;
int dayUpper; // upper day of month check
(month, dayUpper) = GetMonth(monthVal);
if (day == 0 || day > dayUpper) throw new FormatException(":x: The date you specified is not a valid calendar date.");
return (month, day);
/// <summary>
/// Parses a date input.
/// </summary>
/// <returns>Tuple: month, day</returns>
/// <exception cref="FormatException">
/// Thrown for any parsing issue. Reason is expected to be sent to Discord as-is.
/// </exception>
private static (int, int) ParseDate(string dateInput) {
var m = DateParse1.Match(dateInput);
if (!m.Success) {
// Flip the fields around, try again
m = DateParse2.Match(dateInput);
if (!m.Success) throw new FormatException(FormatError);
}
/// <summary>
/// Returns information for a given month input.
/// </summary>
/// <param name="input"></param>
/// <returns>Tuple: Month value, upper limit of days in the month</returns>
/// <exception cref="FormatException">
/// Thrown on error. Send out to Discord as-is.
/// </exception>
private static (int, int) GetMonth(string input)
{
return input.ToLower() switch {
"jan" or "january" => (1, 31),
"feb" or "february" => (2, 29),
"mar" or "march" => (3, 31),
"apr" or "april" => (4, 30),
"may" => (5, 31),
"jun" or "june" => (6, 30),
"jul" or "july" => (7, 31),
"aug" or "august" => (8, 31),
"sep" or "september" => (9, 30),
"oct" or "october" => (10, 31),
"nov" or "november" => (11, 30),
"dec" or "december" => (12, 31),
_ => throw new FormatException($":x: Can't determine month name `{input}`. Check your spelling and try again."),
};
int day, month;
string monthVal;
try {
day = int.Parse(m.Groups["day"].Value);
} catch (FormatException) {
throw new Exception(FormatError);
}
#endregion
monthVal = m.Groups["month"].Value;
#region Documentation
public static readonly CommandDocumentation DocSet =
new(new string[] { "set (date)" }, "Registers your birth month and day.",
$"`{CommandPrefix}set jan-31`, `{CommandPrefix}set 15 may`.");
public static readonly CommandDocumentation DocZone =
new(new string[] { "zone (zone)" }, "Sets your local time zone. "
+ $"See also `{CommandPrefix}help-tzdata`.", null);
public static readonly CommandDocumentation DocRemove =
new(new string[] { "remove" }, "Removes your birthday information from this bot.", null);
#endregion
int dayUpper; // upper day of month check
(month, dayUpper) = GetMonth(monthVal);
private async Task CmdSet(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser)
{
if (param.Length < 2)
{
await reqChannel.SendMessageAsync(ParameterError, embed: DocSet.UsageEmbed).ConfigureAwait(false);
return;
}
if (day == 0 || day > dayUpper) throw new FormatException(":x: The date you specified is not a valid calendar date.");
// Date format accepts spaces. Must coalesce parameters to a single string.
var fullinput = "";
foreach (var p in param[1..]) fullinput += " " + p;
fullinput = fullinput[1..]; // trim leading space
return (month, day);
}
int bmonth, bday;
try
{
(bmonth, bday) = ParseDate(fullinput);
}
catch (FormatException ex)
{
// Our parse method's FormatException has its message to send out to Discord.
reqChannel.SendMessageAsync(ex.Message, embed: DocSet.UsageEmbed).Wait();
return;
}
/// <summary>
/// Returns information for a given month input.
/// </summary>
/// <param name="input"></param>
/// <returns>Tuple: Month value, upper limit of days in the month</returns>
/// <exception cref="FormatException">
/// Thrown on error. Send out to Discord as-is.
/// </exception>
private static (int, int) GetMonth(string input) {
return input.ToLower() switch {
"jan" or "january" => (1, 31),
"feb" or "february" => (2, 29),
"mar" or "march" => (3, 31),
"apr" or "april" => (4, 30),
"may" => (5, 31),
"jun" or "june" => (6, 30),
"jul" or "july" => (7, 31),
"aug" or "august" => (8, 31),
"sep" or "september" => (9, 30),
"oct" or "october" => (10, 31),
"nov" or "november" => (11, 30),
"dec" or "december" => (12, 31),
_ => throw new FormatException($":x: Can't determine month name `{input}`. Check your spelling and try again."),
};
}
#endregion
// Parsing successful. Update user information.
bool known; // Extra detail: Bot's response changes if the user was previously unknown.
try
{
var user = await GuildUserConfiguration.LoadAsync(gconf.GuildId, reqUser.Id).ConfigureAwait(false);
known = user.IsKnown;
await user.UpdateAsync(bmonth, bday, user.TimeZone).ConfigureAwait(false);
}
catch (Exception ex)
{
Program.Log("Error", ex.ToString());
// TODO webhook report
reqChannel.SendMessageAsync(InternalError).Wait();
return;
}
await reqChannel.SendMessageAsync($":white_check_mark: Your birthday has been { (known ? "updated" : "recorded") }.")
.ConfigureAwait(false);
#region Documentation
public static readonly CommandDocumentation DocSet =
new(new string[] { "set (date)" }, "Registers your birth month and day.",
$"`{CommandPrefix}set jan-31`, `{CommandPrefix}set 15 may`.");
public static readonly CommandDocumentation DocZone =
new(new string[] { "zone (zone)" }, "Sets your local time zone. "
+ $"See also `{CommandPrefix}help-tzdata`.", null);
public static readonly CommandDocumentation DocRemove =
new(new string[] { "remove" }, "Removes your birthday information from this bot.", null);
#endregion
private async Task CmdSet(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser) {
if (param.Length < 2) {
await reqChannel.SendMessageAsync(ParameterError, embed: DocSet.UsageEmbed).ConfigureAwait(false);
return;
}
private async Task CmdZone(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser)
{
if (param.Length != 2)
{
await reqChannel.SendMessageAsync(ParameterError, embed: DocZone.UsageEmbed).ConfigureAwait(false);
return;
}
// Date format accepts spaces. Must coalesce parameters to a single string.
var fullinput = "";
foreach (var p in param[1..]) fullinput += " " + p;
fullinput = fullinput[1..]; // trim leading space
int bmonth, bday;
try {
(bmonth, bday) = ParseDate(fullinput);
} catch (FormatException ex) {
// Our parse method's FormatException has its message to send out to Discord.
reqChannel.SendMessageAsync(ex.Message, embed: DocSet.UsageEmbed).Wait();
return;
}
// Parsing successful. Update user information.
bool known; // Extra detail: Bot's response changes if the user was previously unknown.
try {
var user = await GuildUserConfiguration.LoadAsync(gconf.GuildId, reqUser.Id).ConfigureAwait(false);
if (!user.IsKnown)
{
await reqChannel.SendMessageAsync(":x: You may only update your time zone when you have a birthday registered."
+ $" Refer to the `{CommandPrefix}set` command.", embed: DocZone.UsageEmbed)
.ConfigureAwait(false);
return;
}
string btz;
try
{
btz = ParseTimeZone(param[1]);
}
catch (Exception ex)
{
reqChannel.SendMessageAsync(ex.Message, embed: DocZone.UsageEmbed).Wait();
return;
}
await user.UpdateAsync(user.BirthMonth, user.BirthDay, btz).ConfigureAwait(false);
await reqChannel.SendMessageAsync($":white_check_mark: Your time zone has been updated to **{btz}**.")
known = user.IsKnown;
await user.UpdateAsync(bmonth, bday, user.TimeZone).ConfigureAwait(false);
} catch (Exception ex) {
Program.Log("Error", ex.ToString());
reqChannel.SendMessageAsync(InternalError).Wait();
return;
}
await reqChannel.SendMessageAsync($":white_check_mark: Your birthday has been { (known ? "updated" : "recorded") }.")
.ConfigureAwait(false);
}
private async Task CmdZone(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser) {
if (param.Length != 2) {
await reqChannel.SendMessageAsync(ParameterError, embed: DocZone.UsageEmbed).ConfigureAwait(false);
return;
}
private async Task CmdRemove(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser)
{
// Parameter count check
if (param.Length != 1)
{
await reqChannel.SendMessageAsync(NoParameterError, embed: DocRemove.UsageEmbed).ConfigureAwait(false);
return;
}
var user = await GuildUserConfiguration.LoadAsync(gconf.GuildId, reqUser.Id).ConfigureAwait(false);
if (!user.IsKnown) {
await reqChannel.SendMessageAsync(":x: You may only update your time zone when you have a birthday registered."
+ $" Refer to the `{CommandPrefix}set` command.", embed: DocZone.UsageEmbed)
.ConfigureAwait(false);
return;
}
// Extra detail: Send a notification if the user isn't actually known by the bot.
bool known;
var u = await GuildUserConfiguration.LoadAsync(gconf.GuildId, reqUser.Id).ConfigureAwait(false);
known = u.IsKnown;
await u.DeleteAsync().ConfigureAwait(false);
if (!known)
{
await reqChannel.SendMessageAsync(":white_check_mark: This bot already does not contain your information.")
.ConfigureAwait(false);
}
else
{
await reqChannel.SendMessageAsync(":white_check_mark: Your information has been removed.")
.ConfigureAwait(false);
}
string btz;
try {
btz = ParseTimeZone(param[1]);
} catch (Exception ex) {
reqChannel.SendMessageAsync(ex.Message, embed: DocZone.UsageEmbed).Wait();
return;
}
await user.UpdateAsync(user.BirthMonth, user.BirthDay, btz).ConfigureAwait(false);
await reqChannel.SendMessageAsync($":white_check_mark: Your time zone has been updated to **{btz}**.")
.ConfigureAwait(false);
}
private async Task CmdRemove(ShardInstance instance, GuildConfiguration gconf,
string[] param, SocketTextChannel reqChannel, SocketGuildUser reqUser) {
// Parameter count check
if (param.Length != 1) {
await reqChannel.SendMessageAsync(NoParameterError, embed: DocRemove.UsageEmbed).ConfigureAwait(false);
return;
}
// Extra detail: Send a notification if the user isn't actually known by the bot.
bool known;
var u = await GuildUserConfiguration.LoadAsync(gconf.GuildId, reqUser.Id).ConfigureAwait(false);
known = u.IsKnown;
await u.DeleteAsync().ConfigureAwait(false);
if (!known) {
await reqChannel.SendMessageAsync(":white_check_mark: This bot already does not contain your information.")
.ConfigureAwait(false);
} else {
await reqChannel.SendMessageAsync(":white_check_mark: Your information has been removed.")
.ConfigureAwait(false);
}
}
}