public async Task MuteUser(SocketGuildUser user, string duration = null, [Remainder] string reason = null) { TimeSpan? timeSpan = null; SocketGuild guild = Context.Guild; Server server = await DatabaseQueries.GetOrCreateServerAsync(guild.Id); string muteString = ""; reason ??= "<No reason provided>"; if (duration != null) { if (!duration.Any(x => x.Equals('s') || x.Equals('m') || x.Equals('h') || x.Equals('d'))) { throw new FormatException("You did not specify a proper mute time.\nThe proper format is " + "`<user> <dhms>`.\nExample: `<user> 30m`"); } timeSpan = duration.ParseToTimespan(); double time = DateTime.Now.Add(timeSpan.Value).ToOADate(); muteString = $"User will be unmuted `{DateTime.FromOADate(time).Humanize(false)}`"; var muteObject = new MutedUser { ServerId = guild.Id, UserId = user.Id, ExpiresAt = time }; if (await DatabaseQueries.GetAllForServerAsync <MutedUser>(server.ServerId) != null) { var existingObject = await DatabaseQueries.GetFirstMatchAsync <MutedUser>(x => x.UserId == user.Id && x.ServerId == server.ServerId); if (existingObject != null) { await DatabaseQueries.DeleteAsync(existingObject); await ConsoleLogger.LogAsync($"Removed duplicate mute entry for user {user.Id} in guild {guild.Id}.", LogLvl.DEBUG); } } await DatabaseQueries.InsertAsync(muteObject); } SocketRole muteRole = guild.Roles.FirstOrDefault(x => x?.Name.ToLower() == "kaguya-mute"); if (muteRole == null) { await Context.Guild.CreateRoleAsync("kaguya-mute", GuildPermissions.None, Color.Default, false, false, null); await ConsoleLogger.LogAsync($"New mute role created in guild [Name: {guild.Name} | ID: {guild.Id}]", LogLvl.DEBUG); /* * We redefine guild because the object * must now have an updated role collection * in order for this to work. */ guild = Context.Guild; muteRole = guild.Roles.FirstOrDefault(x => x.Name.ToLower() == "kaguya-mute"); await ReplyAsync($"{Context.User.Mention} Mute role not found, so I created one.\n" + "Updating channel permissions. Please wait..."); } int failCount = 0; foreach (SocketGuildChannel channel in guild.Channels) { if (channel.GetPermissionOverwrite(muteRole).HasValue) { continue; } try { await channel.AddPermissionOverwriteAsync(muteRole, OverwritePermissions.InheritAll); await channel.AddPermissionOverwriteAsync(muteRole, new OverwritePermissions( addReactions : PermValue.Deny, speak : PermValue.Deny, sendTTSMessages : PermValue.Deny, connect : PermValue.Deny, createInstantInvite : PermValue.Deny, sendMessages : PermValue.Deny)); await ConsoleLogger.LogAsync($"Permission overwrite added for guild channel.\n" + $"Guild: [Name: {guild.Name} | ID: {guild.Id}]\n" + $"Channel: [Name: {channel.Name} | ID: {channel.Id}]", LogLvl.TRACE); } catch (Exception) { if (failCount >= 3) { await ReplyAsync($"{Context.User.Mention} Could not update permissions for several channels! " + $"**Aborting mute!**"); return; } await ReplyAsync($"{Context.User.Mention} Could not update permissions for {channel}! Muted user " + $"can still type in this channel!"); failCount++; } } try { await user.AddRoleAsync(muteRole); } catch (Exception) { await ReplyAsync($"{Context.User.Mention} failed to mute user. Please ensure my 'Kaguya' role" + $" is at the top of the role hierarchy. I cannot mute users who have a role " + $"higher than me."); } await ConsoleLogger.LogAsync($"User muted. Guild: [Name: {guild.Name} | ID: {guild.Id}] " + $"User: [Name: {user} | ID: {user.Id}]", LogLvl.DEBUG); await ReplyAsync($"{Context.User.Mention} Successfully muted **{user}**. {muteString}"); KaguyaEvents.TriggerMute(new ModeratorEventArgs(server, guild, user, (SocketGuildUser)Context.User, reason, timeSpan)); }
public async Task <bool> MassMention(SocketUserMessage msg, SocketTextChannel channel) { if (msg.MentionedUsers.Distinct().Where(x => x != msg.Author).Where(x => x.IsBot == false).Count() + msg.MentionedRoles.Distinct().Where(x => x.IsMentionable == true).Count() >= 10) { SocketGuildUser user = channel.Guild.GetUser(msg.Author.Id); EmbedAuthorBuilder auth = new EmbedAuthorBuilder() { Name = $"Case {Ban.CaseCount(channel.Guild.Id) + 1} | Mute | {user.Username}#{user.Discriminator}", IconUrl = user.GetAvatarUrl(), }; EmbedBuilder embed = new EmbedBuilder() { Color = new Color(255, 0, 0), Author = auth }; EmbedFooterBuilder footer = new EmbedFooterBuilder() { Text = $"ID: {user.Id}" }; SocketTextChannel banlog = user.Guild.GetTextChannel(263474494327226388); SocketRole role = user.Guild.GetRole(308331455602229268); RequestOptions options = new RequestOptions() { AuditLogReason = $"Muting User | Reason: Auto - Mass mention | Mod: {Client.CurrentUser.Username}#{Client.CurrentUser.Discriminator}" }; await user.AddRoleAsync(role, options); embed.WithCurrentTimestamp(); embed.WithFooter(footer); embed.AddField(y => { y.Name = "User"; y.Value = user.Mention; y.IsInline = true; }); embed.AddField(y => { y.Name = "Moderator"; y.Value = Client.CurrentUser.Mention; y.IsInline = true; }); embed.AddField(y => { y.Name = "Length"; y.Value = $"10 minutes"; y.IsInline = true; }); embed.AddField(y => { y.Name = "Reason"; y.Value = "Auto - Mass mention"; y.IsInline = true; }); string content = Format.Sanitize(msg.Content).Length > 252 ? Format.Sanitize(msg.Content).SplitInParts(252).ElementAt(0) + "..." : Format.Sanitize(msg.Content); embed.AddField(y => { y.Name = "Message"; y.Value = $"**{content}**"; y.IsInline = true; }); Ban ban = new Ban() { GuildID = long.Parse(channel.Guild.Id.ToString()), Case = Ban.CaseCount(channel.Guild.Id) + 1, UserID = long.Parse(user.Id.ToString()), Username = user.Username + "#" + user.Discriminator, Type = "Mute", ModeratorID = long.Parse(Client.CurrentUser.Id.ToString()), Moderator = Client.CurrentUser.Username + "#" + Client.CurrentUser.Discriminator, Reason = "Auto - Mass mention - " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToUniversalTime().ToShortTimeString() }; Ban.Add(ban); try { await user.SendMessageAsync($"You have been muted in **{Format.Sanitize(channel.Guild.Name)}** by {Client.CurrentUser.Mention} for **mass mentioning** and for a length of **10** minutes."); } catch (Discord.Net.HttpException) { //cant send message } MutedUser.Add(channel.Guild.Id, user.Id); await channel.SendMessageAsync($"**{user.Username}#{user.Discriminator}** was muted."); await banlog.SendMessageAsync("", false, embed.Build()); Task task = Task.Run(async() => { await Task.Delay(600000); SocketGuildUser usr = user as SocketGuildUser; if (usr.Roles.Any(e => e.Name.ToUpperInvariant() == "Muted".ToUpperInvariant())) { options.AuditLogReason = "Unmuting User | Reason: Mute expired"; await user.RemoveRoleAsync(role, options); EmbedAuthorBuilder auth2 = new EmbedAuthorBuilder() { Name = $"Case {Ban.CaseCount(channel.Guild.Id) + 1} | Unmute | {user.Username}#{user.Discriminator}", IconUrl = user.GetAvatarUrl(), }; EmbedBuilder embed2 = new EmbedBuilder() { Color = new Color(0, 255, 0), Author = auth2 }; EmbedFooterBuilder footer2 = new EmbedFooterBuilder() { Text = $"ID: {user.Id}" }; embed2.WithCurrentTimestamp(); embed2.WithFooter(footer2); embed2.AddField(y => { y.Name = "User"; y.Value = user.Mention; y.IsInline = true; }); embed2.AddField(y => { y.Name = "Moderator"; y.Value = Client.CurrentUser.Mention; y.IsInline = true; }); embed2.AddField(y => { y.Name = "Reason"; y.Value = "Auto"; y.IsInline = true; }); await banlog.SendMessageAsync("", false, embed2.Build()); try { await user.SendMessageAsync($"You are now unmuted in **{Format.Sanitize(channel.Guild.Name)}**."); } catch (Discord.Net.HttpException) { //cant send message } ban.Type = "Unmute"; ban.Reason = "Auto - " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToUniversalTime().ToShortTimeString(); Ban.Add(ban); MutedUser.Remove(channel.Guild.Id, user.Id); } else { MutedUser.Remove(channel.Guild.Id, user.Id); } }); return(true); } else if (msg.MentionedUsers.Distinct().Where(x => x != msg.Author).Where(x => x.IsBot == false).Count() + msg.MentionedRoles.Distinct().Where(x => x.IsMentionable == true).Count() >= 5) { return(true); } return(false); }