/// <summary>
        /// Gets a queryable list of characters on the given guild.
        /// </summary>
        /// <param name="guild">The guild.</param>
        /// <returns>A retrieval result which may or may not have succeeded.</returns>
        public async Task <RetrieveEntityResult <IQueryable <Character> > > GetCharacters(IGuild guild)
        {
            var getServer = await _servers.GetOrRegisterServerAsync(guild);

            if (!getServer.IsSuccess)
            {
                return(RetrieveEntityResult <IQueryable <Character> > .FromError(getServer));
            }

            var server = getServer.Entity;

            return(RetrieveEntityResult <IQueryable <Character> > .FromSuccess(_characters.GetCharacters(server)));
        }
Пример #2
0
        /// <summary>
        /// Deletes the character role for the given Discord role.
        /// </summary>
        /// <param name="role">The character role.</param>
        /// <returns>A deletion result which may or may not have succeeded.</returns>
        public async Task <DeleteEntityResult> DeleteCharacterRoleAsync(CharacterRole role)
        {
            var currentCharactersWithRole = await _characters.GetCharacters(role.Server)
                                            .Where(c => c.Role == role)
                                            .Where(c => c.IsCurrent)
                                            .ToListAsync();

            _database.CharacterRoles.Remove(role);
            await _database.SaveChangesAsync();

            var guild = await _client.GetGuildAsync((ulong)role.Server.DiscordID);

            if (guild is null)
            {
                return(DeleteEntityResult.FromError("Could not retrieve the guild the role was on."));
            }

            foreach (var character in currentCharactersWithRole)
            {
                var owner = await guild.GetUserAsync((ulong)character.Owner.DiscordID);

                var discordRole = guild.GetRole((ulong)role.DiscordID);

                if (owner is null || discordRole is null)
                {
                    return(DeleteEntityResult.FromError("Failed to get the owner or role."));
                }

                var removeRole = await _discord.RemoveUserRoleAsync(owner, discordRole);

                if (!removeRole.IsSuccess)
                {
                    return(DeleteEntityResult.FromError(removeRole));
                }
            }
            return(DeleteEntityResult.FromSuccess());
        }