private async void OnTimedAutoUnmute(object source, ElapsedEventArgs e) { using (var db = new DbContext()) { var guildRepo = new GuildRepository(db); var muteRepo = new MuteRepository(db); Mute[] mutes = muteRepo.GetAll().ToArray(); foreach (Mute muted in mutes) { if (DateTime.Now.Subtract(DateTime.Parse(muted.MutedAt)).TotalMilliseconds > muted.MuteLength) { var guild = _client.GetGuild(muted.GuildId); if (guild != null && guild.GetUser(muted.UserId) != null) { var guildData = await guildRepo.FetchGuildAsync(guild.Id); var mutedRole = guild.GetRole(guildData.MutedRoleId); if (mutedRole != null && guild.GetUser(muted.UserId).Roles.Any(x => x.Id == mutedRole.Id)) { var channel = guild.GetTextChannel(guildData.ModLogChannelId); if (channel != null && guild.CurrentUser.GuildPermissions.EmbedLinks && (guild.CurrentUser as IGuildUser).GetPermissions(channel as SocketTextChannel).SendMessages && (guild.CurrentUser as IGuildUser).GetPermissions(channel as SocketTextChannel).EmbedLinks) { await guild.GetUser(muted.UserId).RemoveRoleAsync(mutedRole); var footer = new EmbedFooterBuilder() { IconUrl = "http://i.imgur.com/BQZJAqT.png", Text = $"Case #{guildData.CaseNumber}" }; var builder = new EmbedBuilder() { Color = new Color(12, 255, 129), Description = $"**Action:** Automatic Unmute\n**User:** {guild.GetUser(muted.UserId)} ({guild.GetUser(muted.UserId).Id})", Footer = footer }.WithCurrentTimestamp(); await guildRepo.ModifyAsync(x => { x.CaseNumber++; return(Task.CompletedTask); }, guild.Id); await channel.SendMessageAsync("", embed : builder); } } } await muteRepo.RemoveMuteAsync(muted.UserId, muted.GuildId); } } } }
public async static Task RemindMutes() { MuteRepository repo = new MuteRepository(ReadConfig.Config.ConnectionString); UserRepository urepo = new UserRepository(ReadConfig.Config.ConnectionString); bool result = repo.GetAll(out List <Mute> list); if (!result) { _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error getting all mutes in TimerTick."); return; } foreach (var item in list) { if (item.Timestamp + TimeSpan.FromDays(item.Duration) <= DateTime.Now) { result = urepo.Read(item.Mod, out User dbMod); if (!result) { _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error getting a mod from the database."); continue; } DiscordMember mod = null; try { mod = await DiscordObjectService.Instance.Lathland.GetMemberAsync(dbMod.DcID); } catch (NotFoundException) { _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error getting a mod from discord."); } result = urepo.Read(item.User, out User dbUser); if (!result) { _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error getting a user from the database."); continue; } DiscordMember user = null; try { user = await DiscordObjectService.Instance.Lathland.GetMemberAsync(dbUser.DcID); } catch (NotFoundException) { _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error getting a user from discord."); } if (!(user is null) && user.Roles.Contains(DiscordObjectService.Instance.Lathland.GetRole(701446136208293969))) { if (!(await(await mod.CreateDmChannelAsync()).GetMessagesAsync(5)).Any(x => x.Content.Contains("You will be reminded again tomorrow.") && x.CreationTimestamp > DateTime.Now - TimeSpan.FromHours(24))) { await mod.SendMessageAsync($"The user {user.DisplayName}#{user.Discriminator} ({user.Id}) you muted at {item.Timestamp:yyyy-MM-dd hh:mm} for {item.Duration} days, is now muted for {(DateTime.Now - item.Timestamp):dd} days.\n" + $"You will be reminded again tomorrow."); if ((item.Duration < 8 && (item.Timestamp + TimeSpan.FromDays(item.Duration + 2)) < DateTime.Now) || (item.Duration > 7 && (item.Timestamp + TimeSpan.FromDays(item.Duration + 1)) < DateTime.Now) || (item.Duration == 14 && (item.Timestamp + TimeSpan.FromDays(item.Duration)) < DateTime.Now)) { await DiscordObjectService.Instance.Lathland.GetChannel(722905404354592900).SendMessageAsync($"The user {user.DisplayName}#{user.Discriminator} ({user.Id}), muted by {mod.DisplayName}#{mod.Discriminator} ({mod.Id}) at {item.Timestamp:yyyy-MM-dd hh:mm} for {item.Duration} days, is now muted for {(DateTime.Now - item.Timestamp):dd} days."); } } } else if (user is null || !user.Roles.Contains(DiscordObjectService.Instance.Lathland.GetRole(701446136208293969))) { result = repo.Delete(item.Id); if (!result) { _ = DiscordObjectService.Instance.ErrorLogChannel.SendMessageAsync("Error deleting a Mute from database."); continue; } } } }