private async Task PerformActionOnAllAsync(SocketCommandContext context, VoiceChannelMatch channelMatch, VoiceChannelAction action, CancellationToken cancellationToken) { if (!channelMatch.IsSuccess) { return; } // verify it's a guild message SocketTextChannel responseChannel = await this.VerifyGuildChannelAsync(context, cancellationToken).ConfigureAwait(false); if (responseChannel == null) { return; } // verify user's permissions _log.LogTrace("Verifying user {ID} has {Permission} permission in channel {ChannelName} ({ChannelFromID})", channelMatch.User.Id, action.RequiredPermission, channelMatch.Channel.Name, channelMatch.Channel.Id); if (!await this.VerifyUserPermissionsAsync(context, channelMatch.Channel, channelMatch.User.Id, action.RequiredPermission, cancellationToken).ConfigureAwait(false)) { return; } // verify bot's permissions _log.LogTrace("Verifying the bot has {Permission} permission in channel {ChannelName} ({ChannelFromID})", action.RequiredPermission, channelMatch.Channel.Name, channelMatch.Channel.Id); if (!await this.VerifyUserPermissionsAsync(context, channelMatch.Channel, context.Client.CurrentUser.Id, action.RequiredPermission, cancellationToken).ConfigureAwait(false)) { return; } // perform the action on the users SocketGuildUser[] users = channelMatch.Channel.Users.ToArray(); string channelMention = GetVoiceChannelMention(channelMatch.Channel); _log.LogDebug($"{action.ModeWord} {{Count}} users from channel {{ChannelName}} ({{ChannelID}})", users.Length, channelMatch.Channel.Name, channelMatch.Channel.Id); RestUserMessage response = await context.ReplyAsync($"{action.ModeWord} {users.Length} user{(users.Length > 1 ? "s" : null)} in {channelMention}.", cancellationToken).ConfigureAwait(false); int errorCount = 0; foreach (SocketGuildUser user in users) { try { await user.ModifyAsync(action.Action, cancellationToken).ConfigureAwait(false); } catch { errorCount++; } } // display confirmation StringBuilder builder = new StringBuilder(); int successCount = users.Length - errorCount; builder.AppendFormat("{0} user{1} {2} in {3}.", successCount.ToString(), successCount > 1 || successCount == 0 ? "s" : null, action.ActionCompletedWord, channelMention); if (errorCount > 0) { builder.AppendFormat("\nFailed to {3} {0} user{2}. {1}", errorCount.ToString(), this._einherjiOptions.CurrentValue.FailureSymbol, errorCount > 1 ? "s" : null, action.ActionFailedWord); } await response.ModifyAsync(props => props.Content = builder.ToString(), cancellationToken).ConfigureAwait(false); }
private async Task CmdUndeafenAllAsync(SocketCommandContext context, Match match, CancellationToken cancellationToken = default) { using IDisposable logScope = _log.BeginCommandScope(context, this); VoiceChannelMatch channelMatch = await this.MatchChannelAsync(context, match, 1, cancellationToken).ConfigureAwait(false); VoiceChannelAction action = new VoiceChannelAction(user => user.Deaf = false, ChannelPermission.DeafenMembers, "Undeafening", "undeafened", "undeafen"); await PerformActionOnAllAsync(context, channelMatch, action, cancellationToken).ConfigureAwait(false); }