public async Task UnmuteCmd(CommandContext ctx, [Description("The user you're trying to unmute.")] DiscordUser targetUser) { DiscordGuild guild = ctx.Guild; DiscordChannel logChannel = await Program.discord.GetChannelAsync(Program.cfgjson.LogChannel); // todo: store per-guild DiscordRole mutedRole = guild.GetRole(Program.cfgjson.MutedRole); DiscordMember member = await guild.GetMemberAsync(targetUser.Id); if ((await Program.db.HashExistsAsync("mutes", targetUser.Id)) || member.Roles.Contains(mutedRole)) { await Mutes.UnmuteUserAsync(targetUser); await ctx.RespondAsync($"{Program.cfgjson.Emoji.Information} Successfully unmuted **{targetUser.Username}#{targetUser.Discriminator}**."); } else { try { await Mutes.UnmuteUserAsync(targetUser); await ctx.RespondAsync($"{Program.cfgjson.Emoji.Warning} According to Discord that user is not muted, but I tried to unmute them anyway. Hope it works."); } catch (Exception e) { Console.WriteLine(e.ToString()); await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} That user doesn't appear to be muted, *and* an error ocurred while attempting to unmute them anyway. Please contact the bot owner, the error has been logged."); } } }
public static async Task <UserWarning> GiveWarningAsync(DiscordUser targetUser, DiscordUser modUser, string reason, string contextLink, DiscordChannel channel) { DiscordGuild guild = channel.Guild; ulong warningId = (ulong)Program.db.StringGet("totalWarnings"); // TODO: fix this hell if (warningId == 0) { Program.db.StringSet("totalWarnings", "1"); warningId = 1; } else { warningId += 1; } UserWarning warning = new UserWarning() { TargetUserId = targetUser.Id, ModUserId = modUser.Id, WarnReason = reason, WarnTimestamp = DateTime.Now, WarningId = warningId, ContextLink = contextLink }; Program.db.StringSet("totalWarnings", warningId); Program.db.HashSet(targetUser.Id.ToString(), warning.WarningId, JsonConvert.SerializeObject(warning)); try { DiscordMember member = await guild.GetMemberAsync(targetUser.Id); await member.SendMessageAsync($"{Program.cfgjson.Emoji.Warning} You were warned in **{guild.Name}**, reason: **{reason}**"); } catch { // We failed to DM the user, this isn't important to note. } await Program.logChannel.SendMessageAsync($"{Program.cfgjson.Emoji.Warning} New warning for {targetUser.Mention}!", await FancyWarnEmbedAsync(warning, true, 0xFEC13D, false)); // automute handling var warningsOutput = Program.db.HashGetAll(targetUser.Id.ToString()).ToDictionary( x => x.Name.ToString(), x => JsonConvert.DeserializeObject <UserWarning>(x.Value) ); // Realistically this wouldn't ever be 0, but we'll set it below. int warnsSinceThreshold = 0; foreach (KeyValuePair <string, UserWarning> entry in warningsOutput) { UserWarning entryWarning = entry.Value; TimeSpan span = DateTime.Now - entryWarning.WarnTimestamp; if (span.Days <= Program.cfgjson.WarningDaysThreshold) { warnsSinceThreshold += 1; } } int toMuteHours = 0; var keys = Program.cfgjson.AutoMuteThresholds.Keys.OrderBy(key => Convert.ToUInt64(key)); int chosenKey = 0; foreach (string key in keys) { int keyInt = int.Parse(key); if (keyInt <= warnsSinceThreshold && keyInt > chosenKey) { toMuteHours = Program.cfgjson.AutoMuteThresholds[key]; chosenKey = keyInt; } } if (toMuteHours > 0) { DiscordMember member = await guild.GetMemberAsync(targetUser.Id); await Mutes.MuteUserAsync(member, $"Automatic mute after {warnsSinceThreshold} warnings in the past {Program.cfgjson.WarningDaysThreshold} days.", modUser.Id, guild, channel, TimeSpan.FromHours(toMuteHours)); } return(warning); }
public async Task MuteCmd( CommandContext ctx, [Description("The user you're trying to mute")] DiscordUser targetUser, [RemainingText, Description("Combined argument for the time and reason for the mute. For example '1h rule 7' or 'rule 10'")] string timeAndReason = "No reason specified." ) { DiscordMember targetMember; try { targetMember = await ctx.Guild.GetMemberAsync(targetUser.Id); } catch (DSharpPlus.Exceptions.NotFoundException) { // TODO: Rework mutes to allow this await ctx.Message.DeleteAsync(); var msg = await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} The user you're trying to mute is not in the server!"); await Task.Delay(3000); await msg.DeleteAsync(); return; } if (Warnings.GetPermLevel(ctx.Member) == ServerPermLevel.CommunityManager && (Warnings.GetPermLevel(targetMember) >= ServerPermLevel.CommunityManager || 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, ctx.Channel, muteDuration, true); } }