/// <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);
            }
        }
示例#2
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);
            }
        }
示例#3
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>
 /// 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));
 }
示例#5
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));
 }