The Milestone Commit (#48)

mctaylors:
- updated readme 7 times (and only adding new logo from /about)
-
[removed](aeeb3d4399)
bot footer from created event embed on the second try
-
[changed](4b9b91d9e4)
cdn from discord to upload.systems

Octol1ttle:
- Guild settings code has been overhauled. Instead of instances of a
`GuildConfiguration` class being (de-)serialized when used with listing
and setting options provided by reflection, there are now multiple
`Option` classes responsible for the type of option they are storing.
The classes support getting a value, validating and setting values with
Results, and getting a user-friendly representation of these values.
This makes use of polymorphism, providing clean and easier to use and
refactor code.
- Gateway event responders have been split into their own separate
files, which should make it easier to find and modify responders when
needed.
- Warning suppressions regarding unused and never instantiated classes
have been replaced by `[ImplicitUse]` annotations provided by
`JetBrains.Annotations`. This avoids hiding real issues and provides a
better way to suppress false warnings while being explicit.
- It is no longer possible to execute some slash commands if they are
run without the correct permissions
- Dependencies are now more explicitly defined

neroduckale:
 - Made easter eggs case-insensitive

---------

Signed-off-by: Macintosh II <95250141+mctaylors@users.noreply.github.com>
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
Co-authored-by: Octol1ttle <l1ttleofficial@outlook.com>
Co-authored-by: nrdk <neroduck@vk.com>
This commit is contained in:
Macintxsh 2023-07-18 15:25:02 +03:00 committed by GitHub
parent 3eb17b96c5
commit c6dd3727c3
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 912 additions and 658 deletions

View file

@ -1,90 +0,0 @@
using System.Globalization;
using Remora.Discord.API.Abstractions.Objects;
namespace Boyfriend.Data;
/// <summary>
/// Stores per-guild settings that can be set by a member
/// with <see cref="DiscordPermission.ManageGuild" /> using the /settings command
/// </summary>
public class GuildConfiguration {
/// <summary>
/// Represents a scheduled event notification receiver.
/// </summary>
/// <remarks>
/// Used to selectively mention guild members when a scheduled event has started or is about to start.
/// </remarks>
public enum NotificationReceiver {
Interested,
Role
}
public static readonly Dictionary<string, CultureInfo> CultureInfoCache = new() {
{ "en", new CultureInfo("en-US") },
{ "ru", new CultureInfo("ru-RU") },
{ "mctaylors-ru", new CultureInfo("tt-RU") }
};
public string Language { get; set; } = "en";
/// <summary>
/// Controls what message should be sent in <see cref="PublicFeedbackChannel" /> when a new member joins the server.
/// </summary>
/// <remarks>
/// <list type="bullet">
/// <item>No message will be sent if set to "off", "disable" or "disabled".</item>
/// <item><see cref="Messages.DefaultWelcomeMessage" /> will be sent if set to "default" or "reset"</item>
/// </list>
/// </remarks>
/// <seealso cref="GuildMemberAddResponder" />
public string WelcomeMessage { get; set; } = "default";
/// <summary>
/// Controls whether or not the <see cref="Messages.Ready" /> message should be sent
/// in <see cref="PrivateFeedbackChannel" /> on startup.
/// </summary>
/// <seealso cref="GuildCreateResponder" />
public bool ReceiveStartupMessages { get; set; }
public bool RemoveRolesOnMute { get; set; }
/// <summary>
/// Controls whether or not a guild member's roles are returned if he/she leaves and then joins back.
/// </summary>
/// <remarks>Roles will not be returned if the member left the guild because of /ban or /kick.</remarks>
public bool ReturnRolesOnRejoin { get; set; }
public bool AutoStartEvents { get; set; }
/// <summary>
/// Controls what channel should all public messages be sent to.
/// </summary>
public ulong PublicFeedbackChannel { get; set; }
/// <summary>
/// Controls what channel should all private, moderator-only messages be sent to.
/// </summary>
public ulong PrivateFeedbackChannel { get; set; }
public ulong EventNotificationChannel { get; set; }
public ulong DefaultRole { get; set; }
public ulong MuteRole { get; set; }
public ulong EventNotificationRole { get; set; }
/// <summary>
/// Controls what guild members should be mentioned when a scheduled event has started or is about to start.
/// </summary>
/// <seealso cref="NotificationReceiver" />
public List<NotificationReceiver> EventStartedReceivers { get; set; }
= new() { NotificationReceiver.Interested, NotificationReceiver.Role };
/// <summary>
/// Controls the amount of time before a scheduled event to send a reminder in <see cref="EventNotificationChannel" />.
/// </summary>
public TimeSpan EventEarlyNotificationOffset { get; set; } = TimeSpan.Zero;
// Do not convert this to a property, else serialization will be attempted
public CultureInfo GetCulture() {
return CultureInfoCache[Language];
}
}

View file

@ -1,4 +1,4 @@
using System.Globalization;
using System.Text.Json.Nodes;
using Remora.Rest.Core;
namespace Boyfriend.Data;
@ -8,29 +8,26 @@ namespace Boyfriend.Data;
/// </summary>
/// <remarks>This information is stored on disk as a JSON file.</remarks>
public class GuildData {
public readonly GuildConfiguration Configuration;
public readonly string ConfigurationPath;
public readonly Dictionary<ulong, MemberData> MemberData;
public readonly string MemberDataPath;
public readonly Dictionary<ulong, ScheduledEventData> ScheduledEvents;
public readonly string ScheduledEventsPath;
public readonly JsonNode Settings;
public readonly string SettingsPath;
public GuildData(
GuildConfiguration configuration, string configurationPath,
JsonNode settings, string settingsPath,
Dictionary<ulong, ScheduledEventData> scheduledEvents, string scheduledEventsPath,
Dictionary<ulong, MemberData> memberData, string memberDataPath) {
Configuration = configuration;
ConfigurationPath = configurationPath;
Settings = settings;
SettingsPath = settingsPath;
ScheduledEvents = scheduledEvents;
ScheduledEventsPath = scheduledEventsPath;
MemberData = memberData;
MemberDataPath = memberDataPath;
}
public CultureInfo Culture => Configuration.GetCulture();
public MemberData GetMemberData(Snowflake userId) {
if (MemberData.TryGetValue(userId.Value, out var existing)) return existing;

63
src/Data/GuildSettings.cs Normal file
View file

@ -0,0 +1,63 @@
using Boyfriend.Data.Options;
using Boyfriend.Responders;
using Remora.Discord.API.Abstractions.Objects;
namespace Boyfriend.Data;
/// <summary>
/// Contains all per-guild settings that can be set by a member
/// with <see cref="DiscordPermission.ManageGuild" /> using the /settings command
/// </summary>
public static class GuildSettings {
public static readonly LanguageOption Language = new("Language", "en");
/// <summary>
/// Controls what message should be sent in <see cref="PublicFeedbackChannel" /> when a new member joins the server.
/// </summary>
/// <remarks>
/// <list type="bullet">
/// <item>No message will be sent if set to "off", "disable" or "disabled".</item>
/// <item><see cref="Messages.DefaultWelcomeMessage" /> will be sent if set to "default" or "reset"</item>
/// </list>
/// </remarks>
/// <seealso cref="GuildMemberJoinedResponder" />
public static readonly Option<string> WelcomeMessage = new("WelcomeMessage", "default");
/// <summary>
/// Controls whether or not the <see cref="Messages.Ready" /> message should be sent
/// in <see cref="PrivateFeedbackChannel" /> on startup.
/// </summary>
/// <seealso cref="GuildLoadedResponder" />
public static readonly BoolOption ReceiveStartupMessages = new("ReceiveStartupMessages", false);
public static readonly BoolOption RemoveRolesOnMute = new("RemoveRolesOnMute", false);
/// <summary>
/// Controls whether or not a guild member's roles are returned if he/she leaves and then joins back.
/// </summary>
/// <remarks>Roles will not be returned if the member left the guild because of /ban or /kick.</remarks>
public static readonly BoolOption ReturnRolesOnRejoin = new("ReturnRolesOnRejoin", false);
public static readonly BoolOption AutoStartEvents = new("AutoStartEvents", false);
/// <summary>
/// Controls what channel should all public messages be sent to.
/// </summary>
public static readonly SnowflakeOption PublicFeedbackChannel = new("PublicFeedbackChannel");
/// <summary>
/// Controls what channel should all private, moderator-only messages be sent to.
/// </summary>
public static readonly SnowflakeOption PrivateFeedbackChannel = new("PrivateFeedbackChannel");
public static readonly SnowflakeOption EventNotificationChannel = new("EventNotificationChannel");
public static readonly SnowflakeOption DefaultRole = new("DefaultRole");
public static readonly SnowflakeOption MuteRole = new("MuteRole");
public static readonly SnowflakeOption EventNotificationRole = new("EventNotificationRole");
/// <summary>
/// Controls the amount of time before a scheduled event to send a reminder in <see cref="EventNotificationChannel" />.
/// </summary>
public static readonly TimeSpanOption EventEarlyNotificationOffset = new(
"EventEarlyNotificationOffset", TimeSpan.Zero);
}

View file

@ -1,5 +1,3 @@
using Remora.Rest.Core;
namespace Boyfriend.Data;
/// <summary>
@ -13,6 +11,6 @@ public class MemberData {
public ulong Id { get; }
public DateTimeOffset? BannedUntil { get; set; }
public List<Snowflake> Roles { get; set; } = new();
public List<ulong> Roles { get; set; } = new();
public List<Reminder> Reminders { get; } = new();
}

View file

@ -0,0 +1,34 @@
using System.Text.Json.Nodes;
using Remora.Results;
namespace Boyfriend.Data.Options;
public class BoolOption : Option<bool> {
public BoolOption(string name, bool defaultValue) : base(name, defaultValue) { }
public override string Display(JsonNode settings) {
return Get(settings) ? Messages.Yes : Messages.No;
}
public override Result Set(JsonNode settings, string from) {
if (!TryParseBool(from, out var value))
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue));
settings[Name] = value;
return Result.FromSuccess();
}
private static bool TryParseBool(string from, out bool value) {
value = false;
switch (from) {
case "1" or "y" or "yes" or "д" or "да":
value = true;
return true;
case "0" or "n" or "no" or "н" or "не" or "нет":
value = false;
return true;
default:
return false;
}
}
}

View file

@ -0,0 +1,10 @@
using System.Text.Json.Nodes;
using Remora.Results;
namespace Boyfriend.Data.Options;
public interface IOption {
string Name { get; }
string Display(JsonNode settings);
Result Set(JsonNode settings, string from);
}

View file

@ -0,0 +1,35 @@
using System.Globalization;
using System.Text.Json.Nodes;
using Remora.Discord.Extensions.Formatting;
using Remora.Results;
namespace Boyfriend.Data.Options;
/// <inheritdoc />
public class LanguageOption : Option<CultureInfo> {
private static readonly Dictionary<string, CultureInfo> CultureInfoCache = new() {
{ "en", new CultureInfo("en-US") },
{ "ru", new CultureInfo("ru-RU") },
{ "mctaylors-ru", new CultureInfo("tt-RU") }
};
public LanguageOption(string name, string defaultValue) : base(name, CultureInfoCache[defaultValue]) { }
public override string Display(JsonNode settings) {
return Markdown.InlineCode(settings[Name]?.GetValue<string>() ?? "en");
}
/// <inheritdoc />
public override CultureInfo Get(JsonNode settings) {
var property = settings[Name];
return property != null ? CultureInfoCache[property.GetValue<string>()] : DefaultValue;
}
/// <inheritdoc />
public override Result Set(JsonNode settings, string from) {
if (!CultureInfoCache.ContainsKey(from.ToLowerInvariant()))
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.LanguageNotSupported));
return base.Set(settings, from.ToLowerInvariant());
}
}

View file

@ -0,0 +1,46 @@
using System.Text.Json.Nodes;
using Remora.Discord.Extensions.Formatting;
using Remora.Results;
namespace Boyfriend.Data.Options;
/// <summary>
/// Represents an per-guild option.
/// </summary>
/// <typeparam name="T">The type of the option.</typeparam>
public class Option<T> : IOption
where T : notnull {
internal readonly T DefaultValue;
public Option(string name, T defaultValue) {
Name = name;
DefaultValue = defaultValue;
}
public string Name { get; }
public virtual string Display(JsonNode settings) {
return Markdown.InlineCode(Get(settings).ToString()!);
}
/// <summary>
/// Sets the value of the option from a <see cref="string" /> to the provided JsonNode.
/// </summary>
/// <param name="settings">The <see cref="JsonNode" /> to set the value to.</param>
/// <param name="from">The string from which the new value of the option will be parsed.</param>
/// <returns>A value setting result which may or may not have succeeded.</returns>
public virtual Result Set(JsonNode settings, string from) {
settings[Name] = from;
return Result.FromSuccess();
}
/// <summary>
/// Gets the value of the option from the provided <paramref name="settings" />.
/// </summary>
/// <param name="settings">The <see cref="JsonNode" /> to get the value from.</param>
/// <returns>The value of the option.</returns>
public virtual T Get(JsonNode settings) {
var property = settings[Name];
return property != null ? property.GetValue<T>() : DefaultValue;
}
}

View file

@ -0,0 +1,27 @@
using System.Text.Json.Nodes;
using Remora.Discord.Extensions.Formatting;
using Remora.Rest.Core;
using Remora.Results;
namespace Boyfriend.Data.Options;
public class SnowflakeOption : Option<Snowflake> {
public SnowflakeOption(string name) : base(name, 0UL.ToSnowflake()) { }
public override string Display(JsonNode settings) {
return Name.EndsWith("Channel") ? Mention.Channel(Get(settings)) : Mention.Role(Get(settings));
}
public override Snowflake Get(JsonNode settings) {
var property = settings[Name];
return property != null ? property.GetValue<ulong>().ToSnowflake() : DefaultValue;
}
public override Result Set(JsonNode settings, string from) {
if (!ulong.TryParse(from, out var parsed))
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue));
settings[Name] = parsed;
return Result.FromSuccess();
}
}

View file

@ -0,0 +1,28 @@
using System.Text.Json.Nodes;
using Remora.Commands.Parsers;
using Remora.Results;
namespace Boyfriend.Data.Options;
public class TimeSpanOption : Option<TimeSpan> {
private static readonly TimeSpanParser Parser = new();
public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { }
public override TimeSpan Get(JsonNode settings) {
var property = settings[Name];
return property != null ? ParseTimeSpan(property.GetValue<string>()).Entity : DefaultValue;
}
public override Result Set(JsonNode settings, string from) {
if (!ParseTimeSpan(from).IsDefined(out var span))
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue));
settings[Name] = span.ToString();
return Result.FromSuccess();
}
private static Result<TimeSpan> ParseTimeSpan(string from) {
return Parser.TryParseAsync(from).AsTask().GetAwaiter().GetResult();
}
}

View file

@ -1,9 +1,7 @@
using Remora.Rest.Core;
namespace Boyfriend.Data;
public struct Reminder {
public DateTimeOffset RemindAt;
public DateTimeOffset At;
public string Text;
public Snowflake Channel;
public ulong Channel;
}