public async Task AddAsync(StellarisMod mod, CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { _log.LogDebug("Adding mod {Name} to database", mod.Name); await _collection.InsertOneAsync(mod, null, cancellationToken).ConfigureAwait(false); if (_cachingOptions.CurrentValue.Enabled) { _stellarisModsCache.AddOrReplace(mod.ID, mod, _cachingOptions.CurrentValue.Lifetime); } } finally { _lock.Release(); } }
private async Task CmdAddModAsync(SocketCommandContext context, Match match, CancellationToken cancellationToken = default) { using IDisposable logScope = _log.BeginCommandScope(context, this); Task CreateInvalidUseResponse() => context.ReplyAsync($"{_einherjiOptions.FailureSymbol} Please specify both name and URL of the mod.\nProper usage of this command:\n***{_commandsOptions.Prefix}stellaris mods add <name> | <url>***", cancellationToken); if (context.User.Id != _einherjiOptions.AuthorID) { await context.ReplyAsync($"{_einherjiOptions.FailureSymbol} You can't order me to do that.", cancellationToken).ConfigureAwait(false); return; } if (match.Groups.Count < 3) { await CreateInvalidUseResponse().ConfigureAwait(false); return; } string name = match.Groups[1].Value.Trim(); string url = match.Groups[2].Value.Trim(); if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(url)) { await CreateInvalidUseResponse().ConfigureAwait(false); return; } if (url.Contains(' ', StringComparison.Ordinal)) { await context.ReplyAsync($"{_einherjiOptions.FailureSymbol} URL can't contain any spaces.", cancellationToken).ConfigureAwait(false); return; } StellarisMod mod = new StellarisMod(name, url); _log.LogDebug("Adding stellaris mod {ModName}", mod.Name); await _stellarisModsStore.AddAsync(mod, cancellationToken).ConfigureAwait(false); await context.ReplyAsync($"{_einherjiOptions.SuccessSymbol} Added mod:\n\n{ModToMessageString(mod)}", cancellationToken).ConfigureAwait(false); }
private static string ModToListString(StellarisMod mod, int num) => $"{num}. **{mod.Name}** - [Click to Download]({mod.URL})";
private static string ModToMessageString(StellarisMod mod) => $"**{mod.Name}**\n{mod.URL}";