/// <summary> /// Set template /// </summary> /// <param name="commandContext">Command context</param> /// <param name="appointmentId">Id of the appointment</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task <bool> SetTemplate(CommandContextContainer commandContext, long appointmentId) { var success = false; var dialogHandler = new DialogHandler(commandContext); await using (dialogHandler.ConfigureAwait(false)) { var templateId = await dialogHandler.Run <RaidTemplateSelectionDialogElement, long>() .ConfigureAwait(false); if (templateId > 0) { using (var dbFactory = RepositoryFactory.CreateInstance()) { success = dbFactory.GetRepository <RaidAppointmentRepository>() .Refresh(obj => obj.Id == appointmentId, obj => obj.TemplateId = templateId); if (success) { await RefreshAppointment(appointmentId).ConfigureAwait(false); } } } } return(success); }
/// <summary> /// Assigning roles /// </summary> /// <param name="commandContext">Command context</param> /// <param name="registrationId">Id of the registration</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task AssignRoles(CommandContextContainer commandContext, long registrationId) { var dialogHandler = new DialogHandler(commandContext); await using (dialogHandler.ConfigureAwait(false)) { using (var dbFactory = RepositoryFactory.CreateInstance()) { dbFactory.GetRepository <RaidRegistrationRoleAssignmentRepository>() .RemoveRange(obj => obj.RegistrationId == registrationId); do { var mainRole = await dialogHandler.Run <RaidRoleSelectionDialogElement, long?>(new RaidRoleSelectionDialogElement(_localizationService, null)) .ConfigureAwait(false); if (mainRole != null) { var subRole = await dialogHandler.Run <RaidRoleSelectionDialogElement, long?>(new RaidRoleSelectionDialogElement(_localizationService, mainRole)) .ConfigureAwait(false); dbFactory.GetRepository <RaidRegistrationRoleAssignmentRepository>() .AddOrRefresh(obj => obj.RegistrationId == registrationId && obj.MainRoleId == mainRole.Value && obj.SubRoleId == subRole, obj => { obj.RegistrationId = registrationId; obj.MainRoleId = mainRole.Value; obj.SubRoleId = subRole; }); } else { break; } }while (await dialogHandler.Run <RaidRoleSelectionNextDialogElement, bool>(new RaidRoleSelectionNextDialogElement(_localizationService, false)) .ConfigureAwait(false)); await dialogHandler.DeleteMessages() .ConfigureAwait(false); } } }
/// <summary> /// Editing the participants /// </summary> /// <param name="commandContext">Command Context</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task EditParticipants(CommandContextContainer commandContext) { var dialogHandler = new DialogHandler(commandContext); await using (dialogHandler.ConfigureAwait(false)) { var appointmentId = await dialogHandler.Run <CalendarAppointmentSelectionDialogElement, long>() .ConfigureAwait(false); bool repeat; var participants = new AppointmentParticipantsContainer { AppointmentId = appointmentId }; do { repeat = await dialogHandler.Run <CalendarParticipantsEditDialogElement, bool>(new CalendarParticipantsEditDialogElement(_localizationService, _userManagementService, participants)) .ConfigureAwait(false); }while (repeat); } }
/// <summary> /// Setup assistant /// </summary> /// <param name="commandContext">Command context</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task RunSetupAssistant(CommandContextContainer commandContext) { var dialogHandler = new DialogHandler(commandContext); await using (dialogHandler.ConfigureAwait(false)) { var creationData = await dialogHandler.RunForm <FractalLfgCreationFormData>() .ConfigureAwait(false); var entry = new FractalLfgConfigurationEntity { Title = creationData.Title, Description = creationData.Description, AliasName = creationData.AliasName, DiscordChannelId = commandContext.Channel.Id, DiscordMessageId = (await commandContext.Channel.SendMessageAsync(LocalizationGroup.GetText("BuildingProgress", "Building...")).ConfigureAwait(false)).Id }; using (var dbFactory = RepositoryFactory.CreateInstance()) { dbFactory.GetRepository <FractalLfgConfigurationRepository>() .Add(entry); } await _messageBuilder.RefreshMessageAsync(entry.Id).ConfigureAwait(false); var currentBotMessage = await commandContext.Channel .SendMessageAsync(LocalizationGroup.GetText("CreationCompletedMessage", "Creation completed! All creation messages will be deleted in 30 seconds.")).ConfigureAwait(false); dialogHandler.DialogContext.Messages.Add(currentBotMessage); await Task.Delay(TimeSpan.FromSeconds(30)).ConfigureAwait(false); await dialogHandler.DeleteMessages() .ConfigureAwait(false); } }
/// <summary> /// Commit a raid appointment /// </summary> /// <param name="commandContext">Command context</param> /// <param name="aliasName">Alias name</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task CommitRaidAppointment(CommandContextContainer commandContext, string aliasName) { using (var dbFactory = RepositoryFactory.CreateInstance()) { var now = DateTime.Now; var appointment = await dbFactory.GetRepository <RaidAppointmentRepository>() .GetQuery() .Where(obj => obj.TimeStamp < now && obj.IsCommitted == false && obj.RaidDayConfiguration.AliasName == aliasName) .Select(obj => new { obj.Id, obj.ConfigurationId, obj.TimeStamp }) .FirstOrDefaultAsync() .ConfigureAwait(false); if (appointment?.Id > 0) { var users = new List <RaidCommitUserData>(); var experienceLevels = dbFactory.GetRepository <RaidExperienceLevelRepository>() .GetQuery() .Select(obj => new { obj.Id, obj.DiscordEmoji, obj.Rank, obj.ParticipationPoints }) .ToList(); var fallbackExperienceLevel = experienceLevels.OrderByDescending(obj => obj.Rank) .First(); foreach (var entry in dbFactory.GetRepository <RaidRegistrationRepository>() .GetQuery() .Where(obj => obj.AppointmentId == appointment.Id) .Select(obj => new { UserId = obj.User .DiscordAccounts .Select(obj2 => obj2.Id) .FirstOrDefault(), obj.User.RaidExperienceLevelId, obj.LineupExperienceLevelId }) .ToList()) { var experienceLevel = experienceLevels.FirstOrDefault(obj => obj.Id == entry.RaidExperienceLevelId) ?? fallbackExperienceLevel; users.Add(new RaidCommitUserData { DiscordUserId = entry.UserId, Points = experienceLevel.ParticipationPoints * (entry.LineupExperienceLevelId == null ? 3.0 : 1.0), DiscordEmoji = experienceLevel.DiscordEmoji }); } var container = new RaidCommitContainer { AppointmentId = appointment.Id, AppointmentTimeStamp = appointment.TimeStamp, Users = users, }; var dialogHandler = new DialogHandler(commandContext); await using (dialogHandler.ConfigureAwait(false)) { while (await dialogHandler.Run <RaidCommitDialogElement, bool>(new RaidCommitDialogElement(_localizationService, _userManagementService, container)).ConfigureAwait(false)) { } await commandContext.Channel .SendMessageAsync(LocalizationGroup.GetText("CommitCompleted", "The raid appointment has been committed.")) .ConfigureAwait(false); await _messageBuilder.RefreshMessageAsync(appointment.ConfigurationId) .ConfigureAwait(false); } } else { await commandContext.Message .RespondAsync(LocalizationGroup.GetText("NoOpenAppointment", "There is no uncommitted appointment available.")) .ConfigureAwait(false); } } }
/// <summary> /// Removing an account /// </summary> /// <param name="commandContextContainer">Command context</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task Remove(CommandContextContainer commandContextContainer) { if (commandContextContainer.Message.Channel.IsPrivate == false) { await commandContextContainer.Message .RespondAsync(LocalizationGroup.GetText("SwitchToPrivate", "I answered your command as a private message.")) .ConfigureAwait(false); } await commandContextContainer.SwitchToDirectMessageContext() .ConfigureAwait(false); using (var dbFactory = RepositoryFactory.CreateInstance()) { var names = dbFactory.GetRepository <AccountRepository>() .GetQuery() .Where(obj => obj.User.DiscordAccounts.Any(obj2 => obj2.Id == commandContextContainer.User.Id)) .Select(obj => obj.Name) .Take(2) .ToList(); if (names.Count == 0) { await commandContextContainer.Channel .SendMessageAsync(LocalizationGroup.GetText("NoAccountExisting", "You don't have any accounts configured.")) .ConfigureAwait(false); } else { var name = names.Count == 1 ? names.First() : await DialogHandler.Run <AccountSelectionDialogElement, string>(commandContextContainer) .ConfigureAwait(false); if (string.IsNullOrWhiteSpace(name) == false) { var dialogHandler = new DialogHandler(commandContextContainer); await using (dialogHandler.ConfigureAwait(false)) { if (await dialogHandler.Run <AccountWantToDeleteDialogElement, bool>(new AccountWantToDeleteDialogElement(_localizationService, name)).ConfigureAwait(false)) { var userData = await commandContextContainer.GetCurrentUser() .ConfigureAwait(false); if (dbFactory.GetRepository <GuildWarsAccountDailyLoginCheckRepository>() .RemoveRange(obj => obj.Account.UserId == userData.Id && obj.Name == name) && dbFactory.GetRepository <GuildWarsAccountRepository>() .Remove(obj => obj.UserId == userData.Id && obj.Name == name)) { await commandContextContainer.Channel .SendMessageAsync(LocalizationGroup.GetText("AccountDeleted", "Your account has been successfully deleted.")) .ConfigureAwait(false); } else { throw dbFactory.LastError; } } } } } } }