private async Task CmdRemoveGameAsync(SocketCommandContext context, Match match, CancellationToken cancellationToken = default) { using IDisposable logScope = _log.BeginCommandScope(context, this); if (context.User.Id != _einherjiOptions.CurrentValue.AuthorID) { await SendInsufficientPermissionsAsync(context.Channel, cancellationToken).ConfigureAwait(false); return; } if (match.Groups.Count < 2 || match.Groups[1]?.Length < 1) { await SendNameRequiredAsync(context.Channel, cancellationToken).ConfigureAwait(false); return; } // check if game exists string gameName = match.Groups[1].Value.Trim(); _log.LogDebug("Creating patchbot game {GameName}", gameName); PatchbotGame game = await _patchbotGamesStore.GetAsync(gameName, cancellationToken).ConfigureAwait(false); if (game == null) { _log.LogDebug("Patchbot game {GameName} not found", gameName); await SendGameNotFoundAsync(context.Channel, gameName, cancellationToken).ConfigureAwait(false); return; } await _patchbotGamesStore.DeleteAsync(game, cancellationToken).ConfigureAwait(false); await context.ReplyAsync($"{_einherjiOptions.CurrentValue.SuccessSymbol} Game `{game.Name}` removed.", cancellationToken).ConfigureAwait(false); }
private async Task CmdUnsubscribeAsync(SocketCommandContext context, Match match, CancellationToken cancellationToken = default) { using IDisposable logScope = _log.BeginCommandScope(context, this); if (match.Groups.Count < 2 || match.Groups[1]?.Length < 1) { await SendNameRequiredAsync(context.Channel, cancellationToken).ConfigureAwait(false); return; } string gameName = match.Groups[1].Value.Trim(); _log.LogDebug("Unsubscribing user {UserID} to patchbot game {GameName}", context.User.Id, gameName); PatchbotGame game = await _patchbotGamesStore.GetAsync(gameName, cancellationToken).ConfigureAwait(false); if (game == null) { _log.LogDebug("Patchbot game {GameName} not found", gameName); await SendGameNotFoundAsync(context.Channel, gameName, cancellationToken).ConfigureAwait(false); return; } if (game.SubscriberIDs.Remove(context.User.Id)) { await _patchbotGamesStore.DeleteAsync(game, cancellationToken).ConfigureAwait(false); } await context.ReplyAsync($"{_einherjiOptions.CurrentValue.SuccessSymbol} You will no longer be pinged about `{game.Name}` updates.", cancellationToken).ConfigureAwait(false); }
private async Task PingGameAsync(SocketMessage message, string gameName, CancellationToken cancellationToken = default) { PatchbotGame game = await _patchbotGamesStore.GetAsync(gameName, cancellationToken).ConfigureAwait(false); if (game == null) { _log.LogWarning("Patchbot game {GameName} not found", gameName); return; } // if no one subscribes to this game, abort if (game.SubscriberIDs.Count == 0) { return; } // get only subscribers that are present in this channel IEnumerable <SocketGuildUser> presentSubscribers = (message.Channel as SocketGuildChannel).Users.Where(user => game.SubscriberIDs.Contains(user.Id)); // abort if none of the users has access to the channel if (!presentSubscribers.Any()) { return; } // ping them all _log.LogDebug("Pinging patchbot game {GameName} to {UsersCount} subscribers", game.Name, presentSubscribers.Count()); await message.ReplyAsync($"{string.Join(' ', presentSubscribers.Select(user => user.Mention))}\n{message.GetJumpUrl()}", cancellationToken).ConfigureAwait(false); }
private async Task CmdAddGameAsync(SocketCommandContext context, Match match, CancellationToken cancellationToken = default) { using IDisposable logScope = _log.BeginCommandScope(context, this); if (context.User.Id != _einherjiOptions.CurrentValue.AuthorID) { await SendInsufficientPermissionsAsync(context.Channel, cancellationToken).ConfigureAwait(false); return; } // get names if (match.Groups.Count < 2 || match.Groups[1]?.Length < 1) { await SendNameAndAliasesRequiredAsync(context.Channel, cancellationToken).ConfigureAwait(false); return; } string[] names = match.Groups[1].Value.Split(_namesSeparator, StringSplitOptions.RemoveEmptyEntries) .Select(name => name.Trim()).Where(name => !string.IsNullOrWhiteSpace(name)).ToArray(); if (names.Length == 0) { await SendNameAndAliasesRequiredAsync(context.Channel, cancellationToken).ConfigureAwait(false); return; } // check if game doesn't yet exist string gameName = names.First(); PatchbotGame game = await _patchbotGamesStore.GetAsync(gameName, cancellationToken).ConfigureAwait(false); if (game == null) { _log.LogDebug("Creating patchbot game {GameName}", gameName); game = new PatchbotGame(names.First(), names.TakeLast(names.Length - 1)); } // if it does, just add new aliases else { _log.LogDebug("Adding {AliasesCount} aliases to patchbot game {GameName}", names.Length, game.Name); for (int i = 0; i < names.Length; i++) { if (game.Aliases.Contains(names[i], StringComparer.OrdinalIgnoreCase)) { continue; } game.Aliases.Add(names[i]); } } await _patchbotGamesStore.SetAsync(game, cancellationToken).ConfigureAwait(false); await context.ReplyAsync($"{_einherjiOptions.CurrentValue.SuccessSymbol} Game `{game.Name}` updated.", cancellationToken).ConfigureAwait(false); }