Example #1
0
        /// <summary>
        /// Gets the prefix of the current command context.
        /// </summary>
        /// <param name="prefixing">The prefixing service to work with.</param>
        /// <param name="context">The command context to look for.</param>
        /// <returns>The context's prefix or the default prefix if the context or its prefix is unset.</returns>
        public static async Task <string> GetPrefixAsync(this IContextingService prefixing, ICommandContext context)
        {
            if (!prefixing.IsDbPrefixContext(context))
            {
                return(prefixing.DefaultPrefix);
            }
            IDbPrefixContext prefixContext = await prefixing.FindDbPrefixContextAsync(context).ConfigureAwait(false);

            return(prefixContext?.Prefix ?? prefixing.DefaultPrefix);
        }
        /// <summary>
        /// Gets if the specified command or its module is locked.
        /// </summary>
        /// <param name="locking">The locking service to work with.</param>
        /// <param name="context">The command context to look for.</param>
        /// <param name="command">The command to check.</param>
        /// <returns>True if the command or its module is locked.</returns>
        public static async Task <bool> IsLockedAsync(this IContextingService locking, ICommandContext context, CommandDetails command)
        {
            IDbLockableContext lockContext = await locking.FindDbLockableContextAsync(context).ConfigureAwait(false);

            if (lockContext == null)
            {
                return(command.IsLockedByDefault || command.Module.IsLockedByDefault);
            }
            return(command.IsLocked(lockContext));
        }
Example #3
0
        /// <summary>
        /// Gets the manager role Id of the current command context.
        /// </summary>
        /// <param name="managing">The managing service to work with.</param>
        /// <param name="context">The command context to look for.</param>
        /// <returns>The context's manager role Id or 0 if the context or its role Id is unset.</returns>
        public static async Task <ulong> GetManagerRoleIdAsync(this IContextingService managing, ICommandContext context)
        {
            if (!managing.IsDbManagerContext(context))
            {
                return(0UL);
            }
            IDbManagerContext manageContext = await managing.FindDbManagerContextAsync(context).ConfigureAwait(false);

            return(manageContext?.ManagerRoleId ?? 0UL);
        }
        /// <summary>
        /// Attempts to unlock the module in the specified context.
        /// </summary>
        /// <param name="locking">The locking service to work with.</param>
        /// <param name="context">The module context to look for the module from.</param>
        /// <param name="module">The module to look for.</param>
        /// <returns>
        /// True if the module was unlocked, false if it was already unlocked, and null if the module cannot
        /// be locked.
        /// </returns>
        public static async Task <bool?> UnlockModuleAsync(this IContextingService locking, ICommandContext context, ModuleDetails module)
        {
            if (!locking.IsDbLockableContext(context) || !module.IsLockable)
            {
                return(null);
            }
            using (var db = locking.GetCommandContextDb()) {
                var lockContext = await locking.FindDbLockableContextAsync(db, context, true).ConfigureAwait(false);

                if ((!module.IsLockedByDefault && lockContext.LockedModules.Remove(module.Name)) ||
                    (module.IsLockedByDefault && lockContext.LockedModules.Add(module.Name)))
                {
                    db.ModifyOnly(lockContext, lc => lc.LockedModules);
                    await db.SaveChangesAsync().ConfigureAwait(false);

                    return(true);
                }
                return(false);
            }
        }
Example #5
0
        /// <summary>
        /// Sets the prefix for the current command context.
        /// </summary>
        /// <param name="prefixing">The prefixing service to work with.</param>
        /// <param name="context">The command context to look for.</param>
        /// <param name="newPrefix">The new prefix to set the context to.</param>
        /// <returns>
        /// True if the prefix was set, false if it was already set to this, and null if the prefix cannot be
        /// changed.
        /// </returns>
        public static async Task <bool?> SetPrefixAsync(this IContextingService prefixing, ICommandContext context, string newPrefix)
        {
            if (!prefixing.IsDbPrefixContext(context))
            {
                return(null);
            }
            using (var db = prefixing.GetCommandContextDb()) {
                IDbPrefixContext prefixContext = await prefixing.FindDbPrefixContextAsync(db, context, true).ConfigureAwait(false);

                if (prefixContext.Prefix != newPrefix)
                {
                    prefixContext.Prefix = newPrefix;
                    db.ModifyOnly(prefixContext, pc => pc.Prefix);
                    await db.SaveChangesAsync().ConfigureAwait(false);

                    return(true);
                }
                return(false);
            }
        }
Example #6
0
        /// <summary>
        /// Sets the manager role Id for the current command context.
        /// </summary>
        /// <param name="managing">The managing service to work with.</param>
        /// <param name="context">The command context to look for.</param>
        /// <param name="newRoleId">The new manager role Id to set the context to.</param>
        /// <returns>
        /// True if the role Id was set, false if it was already set to this, and null if the role Id cannot
        /// be changed.
        /// </returns>
        public static async Task <bool?> SetManagerRoleIdAsync(this IContextingService managing, ICommandContext context, ulong newRoleId)
        {
            if (!managing.IsDbManagerContext(context))
            {
                return(null);
            }
            using (var db = managing.GetCommandContextDb()) {
                IDbManagerContext manageContext = await managing.FindDbManagerContextAsync(db, context, true).ConfigureAwait(false);

                if (manageContext.ManagerRoleId != newRoleId)
                {
                    manageContext.ManagerRoleId = newRoleId;
                    db.ModifyOnly(manageContext, pc => pc.ManagerRoleId);
                    await db.SaveChangesAsync().ConfigureAwait(false);

                    return(true);
                }
                return(false);
            }
        }
        /// <summary>
        /// Gets if the specified module is locked.
        /// </summary>
        /// <param name="locking">The locking service to work with.</param>
        /// <param name="context">The command context to look for.</param>
        /// <param name="module">The module to check.</param>
        /// <returns>True if the module is locked.</returns>
        public static async Task <bool> IsModuleLockedAsync(this IContextingService locking, ICommandContext context, ModuleDetails module)
        {
            IDbLockableContext lockContext = await locking.FindDbLockableContextAsync(context).ConfigureAwait(false);

            return(module.IsModuleLocked(lockContext));
        }
 /// <summary>
 /// Finds the lockable context in the database.
 /// </summary>
 /// <param name="locking">The locking service to work with.</param>
 /// <param name="context">The command context to look for.</param>
 /// <returns>The context, or null if it does not exist.</returns>
 public static async Task <IDbLockableContext> FindDbLockableContextAsync(this IContextingService locking, ICommandContext context)
 {
     using (var db = locking.GetCommandContextDb())
         return(await locking.FindDbLockableContextAsync(db, context, false).ConfigureAwait(false));
 }
Example #9
0
 /// <summary>
 /// Resets the prefix for the current command context to the default.
 /// </summary>
 /// <param name="prefixing">The prefixing service to work with.</param>
 /// <param name="context">The command context to look for.</param>
 /// <returns>
 /// True if the prefix was set, false if it was already set to this, and null if the prefix cannot be
 /// changed.
 /// </returns>
 public static Task <bool?> ResetPrefixAsync(this IContextingService prefixing, ICommandContext context)
 {
     return(prefixing.SetPrefixAsync(context, null));
 }
Example #10
0
 /// <summary>
 /// Gets the prefix of the current prefix context.
 /// </summary>
 /// <param name="prefixContext">The prefix context to look in.</param>
 /// <param name="prefixing">The prefixing context to get the default prefix from.</param>
 /// <returns>The context's prefix or the default prefix if the context or its prefix is unset.</returns>
 public static string GetPrefix(this IDbPrefixContext prefixContext, IContextingService prefixing)
 {
     return(prefixContext?.Prefix ?? prefixing.DefaultPrefix);
 }
Example #11
0
 /// <summary>
 /// Finds the prefix context in the database.
 /// </summary>
 /// <param name="prefixing">The prefixing service to work with.</param>
 /// <param name="context">The command context to look for.</param>
 /// <returns>The context, or null if it does not exist.</returns>
 public static async Task <IDbPrefixContext> FindDbPrefixContextAsync(this IContextingService prefixing,
                                                                      ICommandContext context)
 {
     using (var db = prefixing.GetCommandContextDb())
         return(await prefixing.FindDbPrefixContextAsync(db, context, false).ConfigureAwait(false));
 }
Example #12
0
 /// <summary>
 /// Resets the manager role Id for the current command context to the default.
 /// </summary>
 /// <param name="managing">The managing service to work with.</param>
 /// <param name="context">The command context to look for.</param>
 /// <returns>
 /// True if the role Id was set, false if it was already set to this, and null if the role Id cannot
 /// be changed.
 /// </returns>
 public static Task <bool?> ResetManagerRoleIdAsync(this IContextingService managing, ICommandContext context)
 {
     return(managing.SetManagerRoleIdAsync(context, 0));
 }