// Only to be used on naughty users. public static async System.Threading.Tasks.Task <bool> MuteUserAsync(DiscordMember naughtyMember, string reason, ulong moderatorId, DiscordGuild guild, DiscordChannel channel = null, TimeSpan muteDuration = default) { bool permaMute = false; DiscordChannel logChannel = await Program.discord.GetChannelAsync(Program.cfgjson.LogChannel); DiscordRole mutedRole = guild.GetRole(Program.cfgjson.MutedRole); DateTime? expireTime = DateTime.Now + muteDuration; DiscordMember moderator = await guild.GetMemberAsync(moderatorId); if (muteDuration == default) { permaMute = true; expireTime = null; } MemberPunishment newMute = new MemberPunishment() { MemberId = naughtyMember.Id, ModId = moderatorId, ServerId = guild.Id, ExpireTime = expireTime }; await Program.db.HashSetAsync("mutes", naughtyMember.Id, JsonConvert.SerializeObject(newMute)); try { await naughtyMember.GrantRoleAsync(mutedRole); } catch { return(false); } try { if (permaMute) { await logChannel.SendMessageAsync($"{Program.cfgjson.Emoji.Muted} {naughtyMember.Mention} was successfully muted by `{moderator.Username}#{moderator.Discriminator}` (`{moderatorId}`).\nReason: **{reason}**"); await naughtyMember.SendMessageAsync($"{Program.cfgjson.Emoji.Muted} You have been muted in **{guild.Name}**!\nReason: **{reason}**"); } else { await logChannel.SendMessageAsync($"{Program.cfgjson.Emoji.Muted} {naughtyMember.Mention} was successfully muted for {Warnings.TimeToPrettyFormat(muteDuration, false)} by `{moderator.Username}#{moderator.Discriminator}` (`{moderatorId}`).\nReason: **{reason}**"); await naughtyMember.SendMessageAsync($"{Program.cfgjson.Emoji.Muted} You have been muted in **{guild.Name}** for {Warnings.TimeToPrettyFormat(muteDuration, false)}!\nReason: **{reason}**"); } } 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, false)}**!"); } } return(true); }
public static async System.Threading.Tasks.Task <bool> CheckMutesAsync() { DiscordChannel logChannel = await Program.discord.GetChannelAsync(Program.cfgjson.LogChannel); Dictionary <string, MemberPunishment> muteList = Program.db.HashGetAll("mutes").ToDictionary( x => x.Name.ToString(), x => JsonConvert.DeserializeObject <MemberPunishment>(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, MemberPunishment> entry in muteList) { MemberPunishment 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); } }
public static async Task <bool> CheckMutesAsync(bool includeRemutes = false) { DiscordChannel logChannel = await Program.discord.GetChannelAsync(Program.cfgjson.LogChannel); Dictionary <string, MemberPunishment> muteList = Program.db.HashGetAll("mutes").ToDictionary( x => x.Name.ToString(), x => JsonConvert.DeserializeObject <MemberPunishment>(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, MemberPunishment> entry in muteList) { MemberPunishment mute = entry.Value; if (DateTime.Now > mute.ExpireTime) { await UnmuteUserAsync(await Program.discord.GetUserAsync(mute.MemberId)); } else if (includeRemutes) { try { var guild = await Program.discord.GetGuildAsync(mute.ServerId); var member = await guild.GetMemberAsync(mute.MemberId); if (member != null) { var muteRole = guild.GetRole(Program.cfgjson.MutedRole); await member.GrantRoleAsync(muteRole); } } catch { // nothing } } } #if DEBUG Console.WriteLine($"Checked mutes at {DateTime.Now} with result: {success}"); #endif return(success); } }
public static async Task <bool> CheckBansAsync() { DiscordChannel logChannel = await Program.discord.GetChannelAsync(Program.cfgjson.LogChannel); Dictionary <string, MemberPunishment> banList = Program.db.HashGetAll("bans").ToDictionary( x => x.Name.ToString(), x => JsonConvert.DeserializeObject <MemberPunishment>(x.Value) ); if (banList == null | banList.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, MemberPunishment> entry in banList) { MemberPunishment banEntry = entry.Value; DiscordGuild targetGuild = await Program.discord.GetGuildAsync(banEntry.ServerId); if (DateTime.Now > banEntry.ExpireTime) { targetGuild = await Program.discord.GetGuildAsync(banEntry.ServerId); await UnbanFromServerAsync(targetGuild, banEntry.MemberId); success = true; } } #if DEBUG Console.WriteLine($"Checked bans at {DateTime.Now} with result: {success}"); #endif return(success); } }
public static async Task <bool> BanFromServerAsync(ulong targetUserId, string reason, ulong moderatorId, DiscordGuild guild, int deleteDays = 7, DiscordChannel channel = null, TimeSpan banDuration = default, bool appealable = false) { DiscordUser naughtyUser = await Program.discord.GetUserAsync(targetUserId); bool permaBan = false; DiscordChannel logChannel = await Program.discord.GetChannelAsync(Program.cfgjson.LogChannel); DiscordRole mutedRole = guild.GetRole(Program.cfgjson.MutedRole); DateTime? expireTime = DateTime.Now + banDuration; DiscordMember moderator = await guild.GetMemberAsync(moderatorId); if (banDuration == default) { permaBan = true; expireTime = null; } MemberPunishment newBan = new MemberPunishment() { MemberId = targetUserId, ModId = moderatorId, ServerId = guild.Id, ExpireTime = expireTime }; await Program.db.HashSetAsync("bans", targetUserId, JsonConvert.SerializeObject(newBan)); try { DiscordMember targetMember = await guild.GetMemberAsync(targetUserId); if (permaBan) { if (appealable) { await targetMember.SendMessageAsync($"{Program.cfgjson.Emoji.Banned} You have been banned from **{guild.Name}**!\nReason: **{reason}**\nYou can appeal the ban here: https://msft.chat/member/#ban-appeal-process"); } else { await targetMember.SendMessageAsync($"{Program.cfgjson.Emoji.Banned} You have been permanently banned from **{guild.Name}**!\nReason: **{reason}**"); } } else { await targetMember.SendMessageAsync($"{Program.cfgjson.Emoji.Banned} You have been banned from **{guild.Name}** for {Warnings.TimeToPrettyFormat(banDuration, false)}!\nReason: **{reason}**"); } } catch { // A DM failing to send isn't important. } try { await guild.BanMemberAsync(targetUserId, deleteDays, reason); if (permaBan) { await logChannel.SendMessageAsync($"{Program.cfgjson.Emoji.Banned} <@{targetUserId}> was permanently banned by `{moderator.Username}#{moderator.Discriminator}` (`{moderatorId}`).\nReason: **{reason}**"); } else { await logChannel.SendMessageAsync($"{Program.cfgjson.Emoji.Banned} <@{targetUserId}> was banned for {Warnings.TimeToPrettyFormat(banDuration, false)} by `{moderator.Username}#{moderator.Discriminator}` (`{moderatorId}`).\nReason: **{reason}**"); } } catch { return(false); } return(true); }