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 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 Task Unmute(IGuildUser userToUnmute, [Remainder] string reason = null) { if (!userToUnmute.RoleIds.Any(x => x == Context.DbGuild.MutedRoleId)) { ReplyError("You cannot unmute a user who isn't muted."); } await userToUnmute.RemoveRoleAsync(Context.Guild.GetRole(Context.DbGuild.MutedRoleId)); await _muteRepo.RemoveMuteAsync(userToUnmute.Id, userToUnmute.GuildId); await SendAsync($"{Context.User.Boldify()} has successfully unmuted {userToUnmute.Boldify()}."); await _moderationService.InformSubjectAsync(Context.User, "Unmute", userToUnmute, reason); await _moderationService.ModLogAsync(Context, "Unmute", new Color(12, 255, 129), reason, userToUnmute); }
public async Task Unmute(IGuildUser userToUnmute, [Remainder] string reason = "No reason.") { var mutedRoleId = (await GuildRepository.FetchGuildAsync(Context.Guild.Id)).MutedRoleId; if (userToUnmute.RoleIds.All(x => x != mutedRoleId)) { throw new Exception("You cannot unmute a user who isn't muted."); } await InformSubjectAsync(Context.User, "Unmute", userToUnmute, reason); await userToUnmute.RemoveRoleAsync(Context.Guild.GetRole((ulong)mutedRoleId)); await MuteRepository.RemoveMuteAsync(userToUnmute.Id, Context.Guild.Id); await Logger.ModLog(Context, "Unmute", new Color(12, 255, 129), reason, userToUnmute); await ReplyAsync($"{Context.User.Mention} has successfully unmuted {userToUnmute.Mention}!"); }