public static async System.Threading.Tasks.Task <bool> CheckMutesAsync() { DiscordChannel logChannel = await Program.discord.GetChannelAsync(Program.cfgjson.LogChannel); Dictionary <string, MemberMute> muteList = Program.db.HashGetAll("mutes").ToDictionary( x => x.Name.ToString(), x => JsonConvert.DeserializeObject <MemberMute>(x.Value) ); if (muteList == null | muteList.Keys.Count == 0) { return(false); } else { // The success value will be changed later if any of the unmutes are successful. bool success = false; foreach (KeyValuePair <string, MemberMute> entry in muteList) { MemberMute mute = entry.Value; if (DateTime.Now > mute.ExpireTime) { await UnmuteUserAsync(await Program.discord.GetUserAsync(mute.MemberId)); } } #if DEBUG Console.WriteLine($"Checked mutes at {DateTime.Now} with result: {success}"); #endif return(success); } }
// Only to be used on naughty users. public static async System.Threading.Tasks.Task <bool> MuteUserAsync(DiscordMember naughtyMember, TimeSpan muteDuration, string reason, ulong moderatorId, DiscordGuild guild, DiscordChannel channel = null) { DiscordChannel logChannel = await Program.discord.GetChannelAsync(Program.cfgjson.LogChannel); DiscordRole mutedRole = guild.GetRole(Program.cfgjson.MutedRole); DateTime expireTime = DateTime.Now + muteDuration; MemberMute newMute = new MemberMute() { MemberId = naughtyMember.Id, ExpireTime = expireTime, ModId = moderatorId, ServerId = guild.Id }; await Program.db.HashSetAsync("mutes", naughtyMember.Id, JsonConvert.SerializeObject(newMute)); try { await naughtyMember.GrantRoleAsync(mutedRole); } catch { return(false); } await logChannel.SendMessageAsync($"{Program.cfgjson.Emoji.Muted} Successfully muted {naughtyMember.Mention} until `{expireTime}` (In roughly {muteDuration.TotalHours} hours)"); try { await naughtyMember.SendMessageAsync($"{Program.cfgjson.Emoji.Muted} You have been muted in **{guild.Name}** for {Warnings.TimeToPrettyFormat(muteDuration)}!"); } catch { // A DM failing to send isn't important, but let's put it in chat just so it's somewhere. if (!(channel is null)) { await channel.SendMessageAsync($"{Program.cfgjson.Emoji.Muted} {naughtyMember.Mention} was muted for **{Warnings.TimeToPrettyFormat(muteDuration)}**!"); } } return(true); }