mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-24 01:14:12 +00:00
Update precondition logic; make methods obsolete
The current way of loading database information makes things confusing and difficult to follow, particularly with retrieving guild data and its second parameter. A much better alternative should be considered, and accessed by means of extension methods.
This commit is contained in:
parent
59820bced1
commit
6c498045b3
3 changed files with 17 additions and 8 deletions
|
@ -10,17 +10,22 @@ namespace BirthdayBot.ApplicationCommands;
|
||||||
/// That is, they must either have the Manage Server permission or be a member of the designated bot moderator role.
|
/// That is, they must either have the Manage Server permission or be a member of the designated bot moderator role.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
class RequireBotModeratorAttribute : PreconditionAttribute {
|
class RequireBotModeratorAttribute : PreconditionAttribute {
|
||||||
public override string ErrorMessage => ":x: Only bot moderators may use this command.";
|
public const string FailMsg = "User did not pass the mod check.";
|
||||||
|
public const string Reply = ":x: You must be a moderator to use this command.";
|
||||||
|
|
||||||
public override async Task<PreconditionResult> CheckRequirementsAsync(IInteractionContext context,
|
public override string ErrorMessage => FailMsg;
|
||||||
ICommandInfo commandInfo, IServiceProvider services) {
|
|
||||||
|
public override async Task<PreconditionResult> CheckRequirementsAsync(
|
||||||
|
IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) {
|
||||||
if (context.User is not SocketGuildUser user) {
|
if (context.User is not SocketGuildUser user) {
|
||||||
return PreconditionResult.FromError("Mod check automatically failed due to non-guild context.");
|
return PreconditionResult.FromError("Failed due to non-guild context.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var gconf = await GuildConfiguration.LoadAsync(context.Guild.Id, false);
|
if (user.GuildPermissions.ManageGuild) return PreconditionResult.FromSuccess();
|
||||||
var isMod = gconf!.IsBotModerator(user);
|
var gconf = await ((SocketGuild)context.Guild).GetConfigAsync().ConfigureAwait(false);
|
||||||
if (isMod) return PreconditionResult.FromSuccess();
|
if (gconf.ModeratorRole.HasValue && user.Roles.Any(r => r.Id == gconf.ModeratorRole.Value))
|
||||||
else return PreconditionResult.FromError("User did not pass the mod check.");
|
return PreconditionResult.FromSuccess();
|
||||||
|
|
||||||
|
return PreconditionResult.FromError(FailMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,6 +125,7 @@ class GuildConfiguration {
|
||||||
/// Checks if the given user can be considered a bot moderator.
|
/// Checks if the given user can be considered a bot moderator.
|
||||||
/// Checks for either the Manage Guild permission or if the user is within a predetermined role.
|
/// Checks for either the Manage Guild permission or if the user is within a predetermined role.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Obsolete("Usage should be phased out when text commands are removed. Use PreconditionAttribute from now on.", error: false)]
|
||||||
public bool IsBotModerator(SocketGuildUser user)
|
public bool IsBotModerator(SocketGuildUser user)
|
||||||
=> user.GuildPermissions.ManageGuild || (ModeratorRole.HasValue && user.Roles.Any(r => r.Id == ModeratorRole.Value));
|
=> user.GuildPermissions.ManageGuild || (ModeratorRole.HasValue && user.Roles.Any(r => r.Id == ModeratorRole.Value));
|
||||||
|
|
||||||
|
@ -165,6 +166,7 @@ class GuildConfiguration {
|
||||||
/// If true, this method shall not create a new entry and will return null if the guild does
|
/// If true, this method shall not create a new entry and will return null if the guild does
|
||||||
/// not exist in the database.
|
/// not exist in the database.
|
||||||
/// </param>
|
/// </param>
|
||||||
|
[Obsolete("Begin using extension method to retrieve necessary data instead.", false)]
|
||||||
public static async Task<GuildConfiguration?> LoadAsync(ulong guildId, bool nullIfUnknown) {
|
public static async Task<GuildConfiguration?> LoadAsync(ulong guildId, bool nullIfUnknown) {
|
||||||
// TODO nullable static analysis problem: how to indicate non-null return when nullIfUnknown parameter is true?
|
// TODO nullable static analysis problem: how to indicate non-null return when nullIfUnknown parameter is true?
|
||||||
using (var db = await Database.OpenConnectionAsync().ConfigureAwait(false)) {
|
using (var db = await Database.OpenConnectionAsync().ConfigureAwait(false)) {
|
||||||
|
|
|
@ -106,6 +106,7 @@ class GuildUserConfiguration {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attempts to retrieve a user's configuration. Returns a new, updateable instance if none is found.
|
/// Attempts to retrieve a user's configuration. Returns a new, updateable instance if none is found.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Obsolete("Migrate to using extension methods to retrieve necessary data instead.", false)]
|
||||||
public static async Task<GuildUserConfiguration> LoadAsync(ulong guildId, ulong userId) {
|
public static async Task<GuildUserConfiguration> LoadAsync(ulong guildId, ulong userId) {
|
||||||
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
||||||
using var c = db.CreateCommand();
|
using var c = db.CreateCommand();
|
||||||
|
@ -122,6 +123,7 @@ class GuildUserConfiguration {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets all known user configuration records associated with the specified guild.
|
/// Gets all known user configuration records associated with the specified guild.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Obsolete("Migrate to using extension methods to retrieve necessary data instead.", false)]
|
||||||
public static async Task<IEnumerable<GuildUserConfiguration>> LoadAllAsync(ulong guildId) {
|
public static async Task<IEnumerable<GuildUserConfiguration>> LoadAllAsync(ulong guildId) {
|
||||||
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
using var db = await Database.OpenConnectionAsync().ConfigureAwait(false);
|
||||||
using var c = db.CreateCommand();
|
using var c = db.CreateCommand();
|
||||||
|
|
Loading…
Reference in a new issue