private void Unmute(object stateObj) { Task.Run(async() => { Logger.Log(LogSeverity.Debug, $"Timers", "Auto Unmute"); foreach (var mute in await _muteRepo.AllAsync()) { if (DateTime.UtcNow.Subtract(mute.MutedAt).TotalMilliseconds > mute.MuteLength) { var guild = await(_client as IDiscordClient).GetGuildAsync(mute.GuildId); var user = await guild.GetUserAsync(mute.UserId); if (guild != null && user != null) { var guildData = await _guildRepo.GetGuildAsync(guild.Id); var mutedRole = guild.GetRole(guildData.MutedRoleId); if (mutedRole != null && user.RoleIds.Any(x => x == mutedRole.Id)) { var channel = await guild.GetTextChannelAsync(guildData.ModLogChannelId); ChannelPermissions?perms = null; var currentUser = await guild.GetCurrentUserAsync(); if (channel != null) { perms = currentUser.GetPermissions(channel as SocketTextChannel); } if (channel != null && currentUser.GuildPermissions.EmbedLinks && perms.Value.SendMessages && perms.Value.EmbedLinks) { await user.RemoveRoleAsync(mutedRole); var footer = new EmbedFooterBuilder() { IconUrl = "http://i.imgur.com/BQZJAqT.png", Text = $"Case #{guildData.CaseNumber}" }; var embedBuilder = new EmbedBuilder() { Color = new Color(12, 255, 129), Description = $"**Action:** Automatic Unmute\n**User:** {user} ({mute.UserId})", Footer = footer }.WithCurrentTimestamp(); await _guildRepo.ModifyAsync(guildData, x => x.CaseNumber++); await channel.SendMessageAsync(string.Empty, embed: embedBuilder); } } } await _muteRepo.RemoveMuteAsync(mute.UserId, mute.GuildId); } } }); }
private void Unmute(object stateObj) { Task.Run(async() => { List <Mute> collection = null; try { collection = await _muteRepository.AllAsync(); } catch (Exception e) { Console.WriteLine(e.StackTrace); } foreach (var mute in collection) { if (DateTime.Now.Subtract(mute.MutedAt).TotalMilliseconds <= mute.MuteLength) { return; } try { var guild = await(_client as IDiscordClient).GetGuildAsync(mute.GuildId); var user = await guild.GetUserAsync(mute.UserId); var dbGuild = await _guildRepository.GetGuildAsync(guild.Id); var mutedRole = guild.GetRole(dbGuild.MutedRoleId); await user.RemoveRoleAsync(mutedRole); await _moderationService.ModLogAsync(dbGuild, guild, "Auto unmute", Configuration.UnmuteColor, string.Empty, null, user); await Task.Delay(500); } catch (Exception e) { Console.WriteLine(e.StackTrace); } finally { await _muteRepository.DeleteAsync(mute); } } }); }
private void Unmute(object stateObj) { Task.Run(async() => { Logger.Log(LogSeverity.Debug, $"Timers", "Auto Unmute"); List <Mute> collection = null; try { collection = await _muteRepo.AllAsync(); } catch (Exception e) { Logger.Log(LogSeverity.Error, "Attempting to all async the mute repo.", e.StackTrace); } if (collection == null) { Logger.Log(LogSeverity.Error, "Collection is null", $"Stopping Auto Unmute"); return; } Logger.Log(LogSeverity.Debug, "Successfully loaded collection", $"Collection count: {collection.Count}"); foreach (var mute in collection) { if (DateTime.UtcNow.Subtract(mute.MutedAt).TotalMilliseconds <= mute.MuteLength) { return; } Logger.Log(LogSeverity.Debug, "Mute is passed it's length", $"More information soon..."); try { var guild = await(_client as IDiscordClient).GetGuildAsync(mute.GuildId); Logger.Log(LogSeverity.Debug, "Mute Guild:", guild.Name + $" ({mute.GuildId})"); var user = await guild.GetUserAsync(mute.UserId); Logger.Log(LogSeverity.Debug, "Mute User:"******"{user} ({mute.UserId})"); var dbGuild = await _guildRepo.GetGuildAsync(guild.Id); Logger.Log(LogSeverity.Debug, "DbGuild Fetched", $"Mod Log Channel Id: {dbGuild.ModLogChannelId}"); var mutedRole = guild.GetRole(dbGuild.MutedRoleId); Logger.Log(LogSeverity.Debug, "Muted role fetched", mutedRole.Name + $" ({mutedRole.Id})"); await user.RemoveRoleAsync(mutedRole); Logger.Log(LogSeverity.Debug, "Successfully removed a role", $"Mute: {mute}"); var result = await _moderationService.TryModLogAsync(dbGuild, guild, "Automatic Unmute", new Color(12, 255, 129), string.Empty, null, user); Logger.Log(LogSeverity.Debug, "Mod log attempt", $"Succes: {result}"); await Task.Delay(500); } catch (Exception e) { Logger.Log(LogSeverity.Error, "Auto Unmute role removal", e.StackTrace); } finally { await _muteRepo.DeleteAsync(mute); Logger.Log(LogSeverity.Debug, "Mute entry deleted", "Passed it's length"); } } }); }