/// <summary>
        /// Removes the given user from the given roleplay, revoking their access to the dedicated channel (if one
        /// exists).
        /// </summary>
        /// <param name="roleplay">The roleplay.</param>
        /// <param name="discordUser">The user.</param>
        /// <returns>A deletion result which may or may not have succeeded.</returns>
        public async Task <DeleteEntityResult> RemoveUserFromRoleplayAsync(Roleplay roleplay, IGuildUser discordUser)
        {
            var getUser = await _users.GetOrRegisterUserAsync(discordUser);

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

            var user = getUser.Entity;

            var removeUserAsync = await _roleplays.RemoveUserFromRoleplayAsync(roleplay, user);

            if (!removeUserAsync.IsSuccess)
            {
                return(removeUserAsync);
            }

            if (!roleplay.DedicatedChannelID.HasValue)
            {
                return(removeUserAsync);
            }

            var revoke = await _dedicatedChannels.RevokeUserAccessAsync(roleplay, discordUser);

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

            return(DeleteEntityResult.FromSuccess());
        }
        /// <summary>
        /// Deletes the dedicated channel for the roleplay.
        /// </summary>
        /// <param name="guild">The context in which the request was made.</param>
        /// <param name="roleplay">The roleplay to delete the channel of.</param>
        /// <returns>A modification result which may or may not have succeeded.</returns>
        public async Task <DeleteEntityResult> DeleteChannelAsync
        (
            IGuild guild,
            Roleplay roleplay
        )
        {
            if (roleplay.DedicatedChannelID is null)
            {
                return(DeleteEntityResult.FromError
                       (
                           "The roleplay doesn't have a dedicated channel."
                       ));
            }

            var getDedicatedChannelResult = await GetDedicatedChannelAsync(guild, roleplay);

            if (getDedicatedChannelResult.IsSuccess)
            {
                await getDedicatedChannelResult.Entity.DeleteAsync();
            }

            roleplay.DedicatedChannelID = null;
            await _database.SaveChangesAsync();

            return(DeleteEntityResult.FromSuccess());
        }
        /// <summary>
        /// Deletes a roleplay and its associated channel, if any.
        /// </summary>
        /// <param name="roleplay">The roleplay.</param>
        /// <returns>A deletion result which may or may not have succeeded.</returns>
        public async Task <DeleteEntityResult> DeleteRoleplayAsync(Roleplay roleplay)
        {
            var guild = await _client.GetGuildAsync((ulong)roleplay.Server.DiscordID);

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

            if (roleplay.DedicatedChannelID.HasValue)
            {
                var deleteChannel = await _dedicatedChannels.DeleteChannelAsync(guild, roleplay);

                if (!deleteChannel.IsSuccess)
                {
                    return(deleteChannel);
                }
            }

            var deleteRoleplay = await _roleplays.DeleteRoleplayAsync(roleplay);

            if (!deleteRoleplay.IsSuccess)
            {
                return(deleteRoleplay);
            }

            return(DeleteEntityResult.FromSuccess());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Removes the given user from the given roleplay.
        /// </summary>
        /// <param name="roleplay">The roleplay to remove the user from.</param>
        /// <param name="removedUser">The user to remove from the roleplay.</param>
        /// <param name="ct">The cancellation token in use.</param>
        /// <returns>An execution result which may or may not have succeeded.</returns>
        public async Task <DeleteEntityResult> RemoveUserFromRoleplayAsync
        (
            Roleplay roleplay,
            User removedUser,
            CancellationToken ct = default
        )
        {
            removedUser = _database.NormalizeReference(removedUser);

            if (!roleplay.HasJoined(removedUser))
            {
                return(DeleteEntityResult.FromError("No matching user found in the roleplay."));
            }

            if (roleplay.IsOwner(removedUser))
            {
                return(DeleteEntityResult.FromError("The owner of a roleplay can't be removed from it."));
            }

            var participantEntry = roleplay.JoinedUsers.FirstOrDefault(p => p.User == removedUser);

            roleplay.ParticipatingUsers.Remove(participantEntry);

            await _database.SaveChangesAsync(ct);

            return(DeleteEntityResult.FromSuccess());
        }
Exemplo n.º 5
0
        /// <summary>
        /// Removes the named image from the given character.
        /// </summary>
        /// <param name="character">The character to remove the image from.</param>
        /// <param name="image">The image.</param>
        /// <returns>An execution result which may or may not have succeeded.</returns>
        public async Task <DeleteEntityResult> RemoveImageFromCharacterAsync(Character character, Image image)
        {
            if (!character.Images.Contains(image))
            {
                return(DeleteEntityResult.FromError("The character has no image with that name."));
            }

            character.Images.Remove(image);
            await _database.SaveChangesAsync();

            return(DeleteEntityResult.FromSuccess());
        }
        /// <summary>
        /// Deletes the given character.
        /// </summary>
        /// <param name="guildUser">The user that owns the character.</param>
        /// <param name="character">The character to delete.</param>
        /// <param name="ct">The cancellation token in use.</param>
        /// <returns>A deletion result which may or may not have succeeded.</returns>
        public async Task <DeleteEntityResult> DeleteCharacterAsync
        (
            IGuildUser guildUser,
            Character character,
            CancellationToken ct = default
        )
        {
            var getCurrentCharacter = await _characters.GetCurrentCharacterAsync(character.Owner, character.Server, ct);

            if (getCurrentCharacter.IsSuccess)
            {
                // Forcibly load the role so we can access it later
                _ = getCurrentCharacter.Entity.Role;
            }

            var deleteCharacter = await _characters.DeleteCharacterAsync(character, ct);

            if (!deleteCharacter.IsSuccess)
            {
                return(deleteCharacter);
            }

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

            var currentCharacter = getCurrentCharacter.Entity;

            if (currentCharacter != character)
            {
                return(DeleteEntityResult.FromSuccess());
            }

            // Update the user's nickname
            var updateNickname = await UpdateUserNickname(guildUser, ct);

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

            var updateRoles = await _characterRoles.UpdateUserRolesAsync(guildUser, getCurrentCharacter.Entity, ct);

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

            return(DeleteEntityResult.FromSuccess());
        }
Exemplo n.º 7
0
        /// <summary>
        /// Deletes the character role for the given Discord role.
        /// </summary>
        /// <param name="role">The character role.</param>
        /// <param name="ct">The cancellation token in use.</param>
        /// <returns>A deletion result which may or may not have succeeded.</returns>
        public async Task <DeleteEntityResult> DeleteCharacterRoleAsync
        (
            CharacterRole role,
            CancellationToken ct = default
        )
        {
            var currentOwnersWithRole = await _database.Characters.ServerScopedServersideQueryAsync
                                        (
                role.Server,
                q => q
                .Where(c => c.Role == role)
                .Where(c => c.IsCurrent)
                .Select(c => c.Owner)
                .Distinct(),
                ct
                                        );

            _database.CharacterRoles.Remove(role);

            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 characterOwner in currentOwnersWithRole)
            {
                var owner = await guild.GetUserAsync((ulong)characterOwner.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));
                }
            }

            await _database.SaveChangesAsync(ct);

            return(DeleteEntityResult.FromSuccess());
        }
Exemplo n.º 8
0
        /// <summary>
        /// Deletes the given note.
        /// </summary>
        /// <param name="note">The note to delete.</param>
        /// <returns>A deletion result which may or may note have succeeded.</returns>
        public async Task <DeleteEntityResult> DeleteNoteAsync(UserNote note)
        {
            if (!_database.UserNotes.Any(n => n.ID == note.ID))
            {
                return(DeleteEntityResult.FromError
                       (
                           "That note isn't in the database. This is probably an error in the bot."
                       ));
            }

            _database.UserNotes.Remove(note);
            await _database.SaveChangesAsync();

            return(DeleteEntityResult.FromSuccess());
        }
Exemplo n.º 9
0
        /// <summary>
        /// Deletes the given ban.
        /// </summary>
        /// <param name="ban">The ban to delete.</param>
        /// <returns>A deletion result which may or may ban have succeeded.</returns>
        public async Task <DeleteEntityResult> DeleteBanAsync(UserBan ban)
        {
            if (!_database.UserBans.Any(n => n.ID == ban.ID))
            {
                return(DeleteEntityResult.FromError
                       (
                           "That ban isn't in the database. This is probably an error in the bot."
                       ));
            }

            _database.UserBans.Remove(ban);
            await _database.SaveChangesAsync();

            return(DeleteEntityResult.FromSuccess());
        }
Exemplo n.º 10
0
        /// <summary>
        /// Deletes the given warning.
        /// </summary>
        /// <param name="warning">The warning to delete.</param>
        /// <returns>A deletion result which may or may warning have succeeded.</returns>
        public async Task <DeleteEntityResult> DeleteWarningAsync(UserWarning warning)
        {
            if (!_database.UserWarnings.Any(n => n.ID == warning.ID))
            {
                return(DeleteEntityResult.FromError
                       (
                           "That warning isn't in the database. This is probably an error in the bot."
                       ));
            }

            _database.UserWarnings.Remove(warning);
            await _database.SaveChangesAsync();

            return(DeleteEntityResult.FromSuccess());
        }
Exemplo n.º 11
0
        /// <summary>
        /// Deletes the content data associated with a given dossier.
        /// </summary>
        /// <param name="dossier">The dossier.</param>
        /// <returns>A deletion result which may or may not have succeeded.</returns>
        public Task <DeleteEntityResult> DeleteDossierDataAsync(Dossier dossier)
        {
            var dataPath = GetDossierDataPath(dossier);

            if (!_content.FileSystem.FileExists(dataPath))
            {
                return(Task.FromResult(DeleteEntityResult.FromSuccess()));
            }

            try
            {
                _content.FileSystem.DeleteFile(dataPath);
            }
            catch (Exception e)
            {
                return(Task.FromResult(DeleteEntityResult.FromError(e.Message)));
            }

            return(Task.FromResult(DeleteEntityResult.FromSuccess()));
        }
Exemplo n.º 12
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());
        }