static int WriteLinesToFile(IEnumerable <string> lines) { // classic using (var streamReader = new StreamReader("ReadToEnd.txt")) { Console.Write(streamReader.ReadToEnd()); }; using var file = new System.IO.StreamWriter("WriteLines2.txt"); // with await await using SilkDbContext db = GetContext(); int skippedLines = 0; foreach (string line in lines) { if (!line.Contains("Second")) { file.WriteLine(line); } else { skippedLines++; } } // Notice how skippedLines is in scope here. return(skippedLines); // file is disposed here }
public async Task TempBan( CommandContext ctx, DiscordMember user, string duration, [RemainingText] string reason = "Not provided.") { SilkDbContext db = DbFactory.CreateDbContext(); DiscordMember bot = ctx.Guild.CurrentMember; DateTime now = DateTime.Now; TimeSpan banDuration = GetTimeFromInput(duration); BanFailureReason? banFailed = CanBan(bot, ctx.Member, user); DiscordEmbedBuilder embed = new DiscordEmbedBuilder().WithAuthor(bot.Username, ctx.GetBotUrl(), ctx.Client.CurrentUser.AvatarUrl); if (banFailed is not null) { await SendFailureMessage(ctx, user, embed, banFailed); } else { DiscordEmbedBuilder banEmbed = new DiscordEmbedBuilder() .WithAuthor(ctx.User.Username, ctx.User.GetUrl(), ctx.User.AvatarUrl) .WithDescription($"You've been temporarily banned from {ctx.Guild.Name} for {duration} days.") .AddField("Reason:", reason); await user.SendMessageAsync(embed : banEmbed); await ctx.Guild.BanMemberAsync(user, 0, reason); GuildModel guild = db.Guilds.First(g => g.Id == ctx.Guild.Id); UserModel?bannedUser = db.Users.FirstOrDefault(u => u.Id == user.Id); string formattedBanReason = InfractionFormatHandler.ParseInfractionFormat("temporarily banned", banDuration.TotalDays + " days", user.Mention, reason, guild.Configuration.InfractionFormat ?? defaultFormat); UserInfractionModel infraction = CreateInfraction(formattedBanReason, ctx.User.Id, now); if (bannedUser is null) { bannedUser = new UserModel { Infractions = new List <UserInfractionModel>() }; db.Users.Add(bannedUser); bannedUser.Infractions.Add(infraction); } if (guild.Configuration.GeneralLoggingChannel != default) { embed.WithDescription(formattedBanReason); embed.WithColor(DiscordColor.Green); await ctx.Guild.GetChannel(guild.Configuration.GeneralLoggingChannel).SendMessageAsync(embed: embed); } EventService.Events.Add(new TimedInfraction(user.Id, ctx.Guild.Id, DateTime.Now.Add(banDuration), reason, e => _ = OnBanExpiration((TimedInfraction)e))); } }
public async Task GetChangeLog(CommandContext ctx) { SilkDbContext db = _dbFactory.CreateDbContext(); if (db.ChangeLogs.Count() is 0) { await ctx.RespondAsync("It's quite empty here. Perhaps you're using the internal test bot, or this is a self-hosted instance. There are no changelogs to speak of here. :)").ConfigureAwait(false); return; } DiscordEmbed embed = BuildChangeLog(db.ChangeLogs.OrderBy(c => c.ChangeTime).Last()); await ctx.RespondAsync(embed : embed); }
public async Task Donate(CommandContext ctx, uint amount, DiscordMember recipient) { SilkDbContext db = _dbFactory.CreateDbContext(); GlobalUserModel?sender = db.GlobalUsers.FirstOrDefault(u => u.Id == ctx.User.Id); GlobalUserModel?receiver = db.GlobalUsers.FirstOrDefault(u => u.Id == recipient.Id); if (sender is null && receiver is null) { await ctx.RespondAsync("Hmm. Seems like neither of you have an account here. " + $"Go ahead and do `{ctx.Prefix}daily` for me and " + "I'll give you some cash to send to your friend *:)*"); return; } if (receiver is null) { receiver = new GlobalUserModel { Id = recipient.Id }; db.GlobalUsers.Add(receiver); } if (receiver == sender) { await ctx.RespondAsync("I'd love to duplicate money just as much as the next person, but we have an economy!"); } else if (sender.Cash < amount) { await ctx.RespondAsync($"You're {amount - sender.Cash} dollars too short for that, I'm afraid."); } else if (amount >= 1000) { await VerifyTransactionAsync(ctx, sender, receiver, amount); } else { await DoTransactionAsync(ctx, amount, sender, receiver); } await db.SaveChangesAsync(); }
public async Task Cash(CommandContext ctx) { SilkDbContext db = _dbFactory.CreateDbContext(); GlobalUserModel?account = db.GlobalUsers.FirstOrDefault(u => u.Id == ctx.User.Id); if (account is null) { await ctx.RespondAsync("Seems you don't have an account. " + $"Use `{ctx.Prefix}daily` and I'll set one up for you *:)*"); return; } DiscordEmbedBuilder eb = EmbedHelper .CreateEmbed(ctx, "Account balance:", $"You have {account.Cash} dollars!") .WithAuthor(ctx.User.Username, iconUrl: ctx.User.AvatarUrl); await ctx.RespondAsync(embed : eb); }
public async Task SetPrefix(CommandContext ctx, string prefix) { SilkDbContext db = _dbFactory.CreateDbContext(); (bool valid, string reason) = IsValidPrefix(prefix); if (!valid) { await ctx.RespondAsync(reason); return; } GuildModel guild = db.Guilds.First(g => g.Id == ctx.Guild.Id); guild.Prefix = prefix; _prefixCache.UpdatePrefix(ctx.Guild.Id, prefix); await db.SaveChangesAsync(); await ctx.RespondAsync($"Done! I'll respond to `{prefix}` from now on."); }
public async Task Images( CommandContext ctx, [HelpDescription("How many messages to scan for messages; defaults to 10, limit of 100.")] int amount = 10) { SilkDbContext db = _dbFactory.CreateDbContext(); amount = amount > 99 ? 100 : ++amount; IEnumerable <DiscordMessage> images = await GetImages(ctx.Channel, amount); if (images.Count() == 0) { await ctx.RespondAsync($"Failed to query images in the last {amount} messages."); return; } await ctx.RespondAsync($"Queried {images.Count()} images."); await ctx.Channel.DeleteMessagesAsync(images); }
public async Task ServerInfo(CommandContext ctx) { DiscordGuild guild = ctx.Guild; SilkDbContext db = _dbFactory.CreateDbContext(); var staffMembers = db.Guilds.Include(g => g.Users) .First(g => g.Id == guild.Id) .Users .Where(u => u.Flags.HasFlag(UserFlag.Staff)); int staffCount = staffMembers.Count(); DiscordEmbedBuilder embed = new DiscordEmbedBuilder() .WithTitle($"Guild info for {guild.Name}:") .WithColor(DiscordColor.Gold) .WithFooter($"Silk! | Requested by: {ctx.User.Id}", ctx.Client.CurrentUser.AvatarUrl); embed.WithThumbnail(guild.IconUrl); if (guild.PremiumSubscriptionCount.Value > 0) { embed.AddField("Boosts:", $"{guild.PremiumSubscriptionCount.Value} boosts (level {guild.PremiumTier})"); } if (guild.Features.Count > 0) { embed.AddField("Enabled guild features: ", string.Join(", ", guild.Features)); } embed.AddField("Verification Level:", guild.VerificationLevel.ToString().ToUpper()); embed.AddField("Member Count:", guild.MemberCount.ToString()); embed.AddField("Owner:", guild.Owner.Mention); embed.AddField("Approximate staff member count:", staffCount.ToString()); await ctx.RespondAsync(embed : embed); }
public async Task SClean(CommandContext ctx) { SilkDbContext db = _dbFactory.CreateDbContext(); GuildModel prefix = db.Guilds.First(g => g.Id == ctx.Guild.Id); await ctx.RespondAsync($"Are you looking for `{prefix.Prefix}help SClean`?"); }