mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-02 20:19:55 +03:00
Apply official naming guidelines to Octobot (#306)
1. The root namespace was changed from `Octobot` to `TeamOctolings.Octobot`: > DO prefix namespace names with a company name to prevent namespaces from different companies from having the same name. 2. `Octobot.cs` was renamed to `Program.cs`: > DO NOT use the same name for a namespace and a type in that namespace. 3. `IOption`, `Option` were renamed to `IGuildOption` and `GuildOption` respectively: > DO NOT introduce generic type names such as Element, Node, Log, and Message. 4. `Utility` was moved out of the `Services` namespace. It didn't belong there anyway 5. `Program` static fields were moved to `Utility` 6. Localisation files were moved back to the project source files. Looks like this fixed `Message.Designer.cs` code generation --------- Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
19fadead91
commit
793afd0e06
61 changed files with 447 additions and 462 deletions
47
TeamOctolings.Octobot/Data/GuildData.cs
Normal file
47
TeamOctolings.Octobot/Data/GuildData.cs
Normal file
|
@ -0,0 +1,47 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Rest.Core;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Stores information about a guild. This information is not accessible via the Discord API.
|
||||
/// </summary>
|
||||
/// <remarks>This information is stored on disk as a JSON file.</remarks>
|
||||
public sealed class GuildData
|
||||
{
|
||||
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 readonly bool DataLoadFailed;
|
||||
|
||||
public GuildData(
|
||||
JsonNode settings, string settingsPath,
|
||||
Dictionary<ulong, ScheduledEventData> scheduledEvents, string scheduledEventsPath,
|
||||
Dictionary<ulong, MemberData> memberData, string memberDataPath, bool dataLoadFailed)
|
||||
{
|
||||
Settings = settings;
|
||||
SettingsPath = settingsPath;
|
||||
ScheduledEvents = scheduledEvents;
|
||||
ScheduledEventsPath = scheduledEventsPath;
|
||||
MemberData = memberData;
|
||||
MemberDataPath = memberDataPath;
|
||||
DataLoadFailed = dataLoadFailed;
|
||||
}
|
||||
|
||||
public MemberData GetOrCreateMemberData(Snowflake memberId)
|
||||
{
|
||||
if (MemberData.TryGetValue(memberId.Value, out var existing))
|
||||
{
|
||||
return existing;
|
||||
}
|
||||
|
||||
var newData = new MemberData(memberId.Value);
|
||||
MemberData.Add(memberId.Value, newData);
|
||||
return newData;
|
||||
}
|
||||
}
|
87
TeamOctolings.Octobot/Data/GuildSettings.cs
Normal file
87
TeamOctolings.Octobot/Data/GuildSettings.cs
Normal file
|
@ -0,0 +1,87 @@
|
|||
using Remora.Discord.API.Abstractions.Objects;
|
||||
using TeamOctolings.Octobot.Data.Options;
|
||||
using TeamOctolings.Octobot.Responders;
|
||||
|
||||
namespace TeamOctolings.Octobot.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 guild.
|
||||
/// </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 GuildOption<string> WelcomeMessage = new("WelcomeMessage", "default");
|
||||
|
||||
/// <summary>
|
||||
/// Controls what message should be sent in <see cref="PublicFeedbackChannel" /> when a member leaves the guild.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <list type="bullet">
|
||||
/// <item>No message will be sent if set to "off", "disable" or "disabled".</item>
|
||||
/// <item><see cref="Messages.DefaultLeaveMessage" /> will be sent if set to "default" or "reset".</item>
|
||||
/// </list>
|
||||
/// </remarks>
|
||||
/// <seealso cref="GuildMemberLeftResponder" />
|
||||
public static readonly GuildOption<string> LeaveMessage = new("LeaveMessage", "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 whether or not users who try to hoist themselves should be renamed.
|
||||
/// </summary>
|
||||
public static readonly BoolOption RenameHoistedUsers = new("RenameHoistedUsers", 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");
|
||||
|
||||
/// <summary>
|
||||
/// Controls what channel should welcome messages be sent to.
|
||||
/// </summary>
|
||||
public static readonly SnowflakeOption WelcomeMessagesChannel = new("WelcomeMessagesChannel");
|
||||
|
||||
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 ModeratorRole = new("ModeratorRole");
|
||||
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);
|
||||
}
|
23
TeamOctolings.Octobot/Data/MemberData.cs
Normal file
23
TeamOctolings.Octobot/Data/MemberData.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
namespace TeamOctolings.Octobot.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Stores information about a member
|
||||
/// </summary>
|
||||
public sealed class MemberData
|
||||
{
|
||||
public MemberData(ulong id, List<Reminder>? reminders = null)
|
||||
{
|
||||
Id = id;
|
||||
if (reminders is not null)
|
||||
{
|
||||
Reminders = reminders;
|
||||
}
|
||||
}
|
||||
|
||||
public ulong Id { get; }
|
||||
public DateTimeOffset? BannedUntil { get; set; }
|
||||
public DateTimeOffset? MutedUntil { get; set; }
|
||||
public bool Kicked { get; set; }
|
||||
public List<ulong> Roles { get; set; } = [];
|
||||
public List<Reminder> Reminders { get; } = [];
|
||||
}
|
32
TeamOctolings.Octobot/Data/Options/AllOptionsEnum.cs
Normal file
32
TeamOctolings.Octobot/Data/Options/AllOptionsEnum.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
using JetBrains.Annotations;
|
||||
using TeamOctolings.Octobot.Commands;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Represents all options as enums.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// WARNING: This enum is order-dependent! It's values are used as indexes for
|
||||
/// <see cref="SettingsCommandGroup.AllOptions" />.
|
||||
/// </remarks>
|
||||
public enum AllOptionsEnum
|
||||
{
|
||||
[UsedImplicitly] Language,
|
||||
[UsedImplicitly] WelcomeMessage,
|
||||
[UsedImplicitly] LeaveMessage,
|
||||
[UsedImplicitly] ReceiveStartupMessages,
|
||||
[UsedImplicitly] RemoveRolesOnMute,
|
||||
[UsedImplicitly] ReturnRolesOnRejoin,
|
||||
[UsedImplicitly] AutoStartEvents,
|
||||
[UsedImplicitly] RenameHoistedUsers,
|
||||
[UsedImplicitly] PublicFeedbackChannel,
|
||||
[UsedImplicitly] PrivateFeedbackChannel,
|
||||
[UsedImplicitly] WelcomeMessagesChannel,
|
||||
[UsedImplicitly] EventNotificationChannel,
|
||||
[UsedImplicitly] DefaultRole,
|
||||
[UsedImplicitly] MuteRole,
|
||||
[UsedImplicitly] ModeratorRole,
|
||||
[UsedImplicitly] EventNotificationRole,
|
||||
[UsedImplicitly] EventEarlyNotificationOffset
|
||||
}
|
41
TeamOctolings.Octobot/Data/Options/BoolOption.cs
Normal file
41
TeamOctolings.Octobot/Data/Options/BoolOption.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Results;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
public sealed class BoolOption : GuildOption<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 new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue);
|
||||
}
|
||||
|
||||
settings[Name] = value;
|
||||
return Result.Success;
|
||||
}
|
||||
|
||||
private static bool TryParseBool(string from, out bool value)
|
||||
{
|
||||
value = false;
|
||||
switch (from.ToLowerInvariant())
|
||||
{
|
||||
case "true" or "1" or "y" or "yes" or "д" or "да":
|
||||
value = true;
|
||||
return true;
|
||||
case "false" or "0" or "n" or "no" or "н" or "не" or "нет" or "нъет":
|
||||
value = false;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
57
TeamOctolings.Octobot/Data/Options/GuildOption.cs
Normal file
57
TeamOctolings.Octobot/Data/Options/GuildOption.cs
Normal file
|
@ -0,0 +1,57 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Results;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a per-guild option.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the option.</typeparam>
|
||||
public class GuildOption<T> : IGuildOption
|
||||
where T : notnull
|
||||
{
|
||||
protected readonly T DefaultValue;
|
||||
|
||||
public GuildOption(string name, T defaultValue)
|
||||
{
|
||||
Name = name;
|
||||
DefaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public virtual string Display(JsonNode settings)
|
||||
{
|
||||
return Markdown.InlineCode(Get(settings).ToString() ?? throw new InvalidOperationException());
|
||||
}
|
||||
|
||||
/// <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.Success;
|
||||
}
|
||||
|
||||
public Result Reset(JsonNode settings)
|
||||
{
|
||||
settings[Name] = null;
|
||||
return Result.Success;
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
12
TeamOctolings.Octobot/Data/Options/IGuildOption.cs
Normal file
12
TeamOctolings.Octobot/Data/Options/IGuildOption.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Results;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
public interface IGuildOption
|
||||
{
|
||||
string Name { get; }
|
||||
string Display(JsonNode settings);
|
||||
Result Set(JsonNode settings, string from);
|
||||
Result Reset(JsonNode settings);
|
||||
}
|
38
TeamOctolings.Octobot/Data/Options/LanguageOption.cs
Normal file
38
TeamOctolings.Octobot/Data/Options/LanguageOption.cs
Normal file
|
@ -0,0 +1,38 @@
|
|||
using System.Globalization;
|
||||
using System.Text.Json.Nodes;
|
||||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Results;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
/// <inheritdoc />
|
||||
public sealed class LanguageOption : GuildOption<CultureInfo>
|
||||
{
|
||||
private static readonly Dictionary<string, CultureInfo> CultureInfoCache = new()
|
||||
{
|
||||
{ "en", new CultureInfo("en-US") },
|
||||
{ "ru", new CultureInfo("ru-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)
|
||||
{
|
||||
return CultureInfoCache.ContainsKey(from.ToLowerInvariant())
|
||||
? base.Set(settings, from.ToLowerInvariant())
|
||||
: new ArgumentInvalidError(nameof(from), Messages.LanguageNotSupported);
|
||||
}
|
||||
}
|
40
TeamOctolings.Octobot/Data/Options/SnowflakeOption.cs
Normal file
40
TeamOctolings.Octobot/Data/Options/SnowflakeOption.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using System.Text.RegularExpressions;
|
||||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Rest.Core;
|
||||
using Remora.Results;
|
||||
using TeamOctolings.Octobot.Extensions;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
public sealed partial class SnowflakeOption : GuildOption<Snowflake>
|
||||
{
|
||||
public SnowflakeOption(string name) : base(name, 0UL.ToSnowflake()) { }
|
||||
|
||||
public override string Display(JsonNode settings)
|
||||
{
|
||||
return Name.EndsWith("Channel", StringComparison.Ordinal)
|
||||
? 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(NonNumbers().Replace(from, ""), out var parsed))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue);
|
||||
}
|
||||
|
||||
settings[Name] = parsed;
|
||||
return Result.Success;
|
||||
}
|
||||
|
||||
[GeneratedRegex("[^0-9]")]
|
||||
private static partial Regex NonNumbers();
|
||||
}
|
27
TeamOctolings.Octobot/Data/Options/TimeSpanOption.cs
Normal file
27
TeamOctolings.Octobot/Data/Options/TimeSpanOption.cs
Normal file
|
@ -0,0 +1,27 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Results;
|
||||
using TeamOctolings.Octobot.Parsers;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
public sealed class TimeSpanOption : GuildOption<TimeSpan>
|
||||
{
|
||||
public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { }
|
||||
|
||||
public override TimeSpan Get(JsonNode settings)
|
||||
{
|
||||
var property = settings[Name];
|
||||
return property != null ? TimeSpanParser.TryParse(property.GetValue<string>()).Entity : DefaultValue;
|
||||
}
|
||||
|
||||
public override Result Set(JsonNode settings, string from)
|
||||
{
|
||||
if (!TimeSpanParser.TryParse(from).IsDefined(out var span))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue);
|
||||
}
|
||||
|
||||
settings[Name] = span.ToString();
|
||||
return Result.Success;
|
||||
}
|
||||
}
|
9
TeamOctolings.Octobot/Data/Reminder.cs
Normal file
9
TeamOctolings.Octobot/Data/Reminder.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
namespace TeamOctolings.Octobot.Data;
|
||||
|
||||
public struct Reminder
|
||||
{
|
||||
public DateTimeOffset At { get; init; }
|
||||
public string Text { get; init; }
|
||||
public ulong ChannelId { get; init; }
|
||||
public ulong MessageId { get; init; }
|
||||
}
|
41
TeamOctolings.Octobot/Data/ScheduledEventData.cs
Normal file
41
TeamOctolings.Octobot/Data/ScheduledEventData.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
using System.Text.Json.Serialization;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Stores information about scheduled events. This information is not provided by the Discord API.
|
||||
/// </summary>
|
||||
/// <remarks>This information is stored on disk as a JSON file.</remarks>
|
||||
public sealed class ScheduledEventData
|
||||
{
|
||||
public ScheduledEventData(ulong id, string name, DateTimeOffset scheduledStartTime,
|
||||
GuildScheduledEventStatus status)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
ScheduledStartTime = scheduledStartTime;
|
||||
Status = status;
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
public ScheduledEventData(ulong id, string name, bool earlyNotificationSent, DateTimeOffset scheduledStartTime,
|
||||
DateTimeOffset? actualStartTime, GuildScheduledEventStatus? status, bool scheduleOnStatusUpdated)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
EarlyNotificationSent = earlyNotificationSent;
|
||||
ScheduledStartTime = scheduledStartTime;
|
||||
ActualStartTime = actualStartTime;
|
||||
Status = status;
|
||||
ScheduleOnStatusUpdated = scheduleOnStatusUpdated;
|
||||
}
|
||||
|
||||
public ulong Id { get; }
|
||||
public string Name { get; set; }
|
||||
public bool EarlyNotificationSent { get; set; }
|
||||
public DateTimeOffset ScheduledStartTime { get; set; }
|
||||
public DateTimeOffset? ActualStartTime { get; set; }
|
||||
public GuildScheduledEventStatus? Status { get; set; }
|
||||
public bool ScheduleOnStatusUpdated { get; set; } = true;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue