private async Task <Embed> ModifyAsyncExec(int qty, IEnumerable <IGuildUser> guildUsers) { var author = (IGuildUser)Context.Message.Author; var currencySymbol = (await _guildPreferences.GetPreferencesAsync(author.GuildId)).CurrencySymbol; var reply = new EmbedBuilder() .WithDefault(embedColor: EmbedColor.Success, author: author); var no = 1; foreach (var user in guildUsers) { var member = (await _guildMemberManager.GetGuildMemberAsync(user)) !; await _currencyManager.ModifyPointsAsync(member, qty); // Format a nice output. reply.AddField($"No. {no++}:", $"{user.Mention}: {member.CurrencyCount - qty} -> {member.CurrencyCount} {currencySymbol}"); } return(reply.Build()); }
public async Task GetPreferences() { // Get preferences. Should be default var pref = await _provider.GetPreferencesAsync(1); // Default preferences are marked with guild id 0 Assert.Equal((ulong)0, pref.GuildId); await _provider.UpdatePreferencesAsync(guildId : 1, configuration => { configuration.CurrencySymbol = "test"; }); // Should update as object reference is the same. Assert.NotEqual((ulong)0, pref.GuildId); // Check if currency symbol saved. Assert.Equal("test", pref.CurrencySymbol); }
public async Task Accept( [Summary("User")] IGuildUser user, [Remainder][Summary("Nickname. Does not change nickname if none was specified.")] string?nickname = null) { var pref = await _guildPreferencesProvider.GetPreferencesAsync(user.GuildId); try { await _guildMemberManager.AcceptMemberAsync(user, nickname, Context.Guild.Roles.SingleOrDefault(r => r.Id == pref.AcceptedMemberRoleId)); } catch (HttpException) { await this.ReplyErrorEmbedAsync( "The bot was unable to modify the person. Is the bot role above the person you are trying to accept?"); return; } await this.ReplySuccessEmbedAsync($"{user.Mention} has accepted. Welcome!"); }
public async Task <ActionResult <GuildConfiguration> > GetGuildPreferencesAsync(ulong guildId) { var preferences = await _guildPreferencesProvider.GetPreferencesAsync(guildId); return(preferences); }
public async Task Prefix() { var pref = await _guildPreferencesProvider.GetPreferencesAsync(Context.Guild.Id); await this.ReplySuccessEmbedAsync($"Prefix: **{pref.Prefix}**"); }
public async Task HandleCommandAsync(SocketMessage messageParam) { // Don't process the command if it was a system message if (!(messageParam is SocketUserMessage message)) { return; } // Create a number to track where the prefix ends and the command begins var argPos = 0; // Check if message was sent in a guild context. If not - ignore. // TODO: Make a redirect messages channel and maybe add command handler to DM if (!(message.Author is SocketGuildUser guildUser)) { return; } // Get the prefix preference of a guild (if applicable) var prefix = (await _guildPreferences.GetPreferencesAsync(guildUser.Guild.Id)).Prefix; // Determine if the message is a command based on the prefix and make sure no bots trigger commands // TODO: Re add || message.Author.IsBot if (!(message.HasStringPrefix(prefix, ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) { return; } // Create a WebSocket-based command context based on the message var context = new SocketCommandContext(_client, message); // Execute the command with the command context we just // created, along with the service provider for precondition checks. // Create a scope to prevent leaks. using var scope = _services.CreateScope(); // TODO: Upgrade this to RunMode.Async // Keep in mind that result does not indicate a return value // rather an object stating if the command executed successfully. var result = await _commands.ExecuteAsync(context, argPos, scope.ServiceProvider); // Delete successful triggers. if (result.IsSuccess) { try { await message.DeleteAsync(); } catch (Discord.Net.HttpException) { // Delete most likely failed due to no ManageMessages permission. Ignore regardless. } } // Optionally, we may inform the user if the command fails // to be executed; however, this may not always be desired, // as it may clog up the request queue should a user spam a // command. else if (result.Error != CommandError.UnknownCommand) { try { await context.Channel.SendMessageAsync(embed : new EmbedBuilder() .WithDefault(result.ErrorReason, EmbedColor.Error).Build()); } catch (Discord.Net.HttpException e) { // 50013 occurs when bot cannot send embedded messages. All error reports use embeds. if (e.DiscordCode == 50013) { await context.Channel.SendMessageAsync( "Bot requires guild permission EmbedLinks to function properly."); } } } }