public async Task MuteCmd(CommandContext ctx, DiscordMember targetMember, [RemainingText] string timeAndReason = "No reason specificed.") { if (Warnings.GetPermLevel(ctx.Member) == ServerPermLevel.TrialMod && (Warnings.GetPermLevel(targetMember) >= ServerPermLevel.TrialMod || targetMember.IsBot)) { await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} {ctx.User.Mention}, as a Trial Moderator you cannot perform moderation actions on other staff members or bots."); return; } await ctx.Message.DeleteAsync(); TimeSpan muteDuration = default; string possibleTime = timeAndReason.Split(' ').First(); if (possibleTime.Length != 1) { string reason = timeAndReason; // Everything BUT the last character should be a number. string possibleNum = possibleTime.Remove(possibleTime.Length - 1); if (int.TryParse(possibleNum, out int timeLength)) { char possibleTimePeriod = possibleTime.Last(); muteDuration = ModCmds.ParseTime(possibleTimePeriod, timeLength); } else { muteDuration = default; } if (muteDuration != default || possibleNum == "0") { if (!timeAndReason.Contains(" ")) { reason = "No reason specified."; } else { reason = timeAndReason.Substring(timeAndReason.IndexOf(' ') + 1, timeAndReason.Length - (timeAndReason.IndexOf(' ') + 1)); } } // await ctx.RespondAsync($"debug: {possibleNum}, {possibleTime}, {muteDuration.ToString()}, {reason}"); Mutes.MuteUserAsync(targetMember, reason, ctx.User.Id, ctx.Guild, null, muteDuration); reason = reason.Replace("`", "\\`").Replace("*", "\\*"); if (muteDuration == default) { await ctx.RespondAsync($"{Program.cfgjson.Emoji.Muted} {targetMember.Mention} has been muted: **{reason}**"); } else { await ctx.RespondAsync($"{Program.cfgjson.Emoji.Muted} {targetMember.Mention} has been muted for **{Warnings.TimeToPrettyFormat(muteDuration, false)}**: **{reason}**"); } } }
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously public override async Task <bool> ExecuteCheckAsync(CommandContext ctx, bool help) { if (ctx.Guild.Id != Program.cfgjson.ServerID) { return(false); } var level = Warnings.GetPermLevel(ctx.Member); if (level >= this.TargetLvl) { return(true); } else if (!help) { await ctx.RespondAsync($"{Program.cfgjson.Emoji.NoPermissions} Invalid permissions to use command **{ctx.Command.Name}**!"); } return(false); }
public async Task WarnCmd( CommandContext ctx, [Description("The channel you wish for the warning message to appear in.")] DiscordChannel targetChannel, [Description("The user you are warning. Accepts many formats.")] DiscordUser targetUser, [RemainingText, Description("The reason for giving this warning.")] string reason = null ) { DiscordMember targetMember; try { targetMember = await ctx.Guild.GetMemberAsync(targetUser.Id); if (Warnings.GetPermLevel(ctx.Member) == ServerPermLevel.TrialMod && (Warnings.GetPermLevel(targetMember) >= ServerPermLevel.TrialMod || targetMember.IsBot)) { await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} {ctx.User.Mention}, as a Trial Moderator you cannot perform moderation actions on other staff members or bots."); return; } } catch { // do nothing :/ } await ctx.Message.DeleteAsync(); if (reason == null) { await ctx.Member.SendMessageAsync($"{Program.cfgjson.Emoji.Warning} Reason must be included for the warning command to work."); return; } DiscordMessage msg = await targetChannel.SendMessageAsync($"{Program.cfgjson.Emoji.Warning} {targetUser.Mention} was warned: **{reason.Replace("`", "\\`").Replace("*", "\\*")}**", false, null); await ctx.RespondAsync($"{Program.cfgjson.Emoji.Warning} {targetUser.Mention} was warned in {targetChannel.Mention}: **{reason.Replace("`", "\\`").Replace("*", "\\*")}**"); UserWarning warning = await GiveWarningAsync(targetUser, ctx.User, reason, MessageLink(msg), ctx.Channel); }
// 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); }
// 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); }
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 in **{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 in **{guild.Name}**!\nReason: **{reason}**"); } } else { await targetMember.SendMessageAsync($"{Program.cfgjson.Emoji.Banned} You have been banned in **{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); }