<commit_before>﻿using Discord;
using Discord.API.Rest;
using Discord.Commands;
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;

namespace ContactsBot
{
    public class Moderation : ModuleBase
    {
        const ulong mutedRoleId = 251734975727009793;

        [Command("mute"), Summary("Mutes a user for the specified amount of time")]
        public async Task Mute([Summary("The user to mute")] IGuildUser user, [Summary("The time in minutes to mute the user")] double time = 30)
        {
            var guildUser = user as SocketGuildUser;
            if (guildUser == null) return;

            if (!IsCorrectRole(Context, new[] { "Founders", "Moderators", "Regulars" }))
             {
                await ReplyAsync("Couldn't mute user: Insufficient role");
                return;
            }

            TimeSpan timeToMute = TimeSpan.FromMinutes(time);

            var muteRole = guildUser.Guild.Roles.FirstOrDefault(r => r.Id == mutedRoleId);
            if (muteRole == null)
            {
                await ReplyAsync("Couldn't mute user: The specified role doesn't exist");
            }
            else
                await guildUser.AddRolesAsync(muteRole);

            Timer timer = new Timer(TimerCallback, user, (int)timeToMute.TotalMilliseconds, -1);
            Global.MutedTimers.Add(timer);
            Global.MutedUsers.Add(user, timer);

            await ReplyAsync($"Muted {guildUser.Nickname ?? guildUser.Username} for {time} minutes");
        }

        public async void TimerCallback(object user)
        {
            await Unmute(user as SocketGuildUser, true);
        }

        [Command("unmute"), Summary("Unmutes a user")]
        public async Task Unmute([Summary("The user to unmute")] IGuildUser user, bool isCallback = false)
        {
             if(!isCallback && !IsCorrectRole(Context, new[] { "Founders", "Moderators", "Regulars" }))
            {
                await ReplyAsync("Couldn't unmute user: Insufficient role");
                return;
            }
            var guildUser = user as SocketGuildUser;
            var muteRole = guildUser.Guild.Roles.FirstOrDefault(r => r.Id == mutedRoleId);
            if (muteRole == null)
            {
                await ReplyAsync("Couldn't unmute user: The specified role doesn't exist");
            }
            else
            {
                if (guildUser.RoleIds.Contains(mutedRoleId))
                    await guildUser.RemoveRolesAsync(muteRole);
                else
                    return;
            }

            Global.MutedTimers.Remove(Global.MutedUsers[user]);

            await ReplyAsync($"Unmuted {user.Nickname ?? user.Username}");
        }

        public static bool IsCorrectRole(CommandContext Context, string[] roleNames)
        {
            var roles = Context.Guild.Roles;
            var guildUser = Context.User as SocketGuildUser;
            if (guildUser == null || roles == null)
                return false;

            var rolesFromNames = roles.Where(r => roleNames.Any(n => r.Name == n));
            return guildUser.RoleIds.Any(id => rolesFromNames.Any(r => r.Id == id));
        }
    }

    [Group("message")]
    public class Messages : ModuleBase
    {
        [Command("deleterange")]
        public async Task Delete([Summary("The range of messages to delete")] int range)
        {
            if (Moderation.IsCorrectRole(Context, new[] { "Founders", "Moderators", "Regulars" }))
            {
                var messageList = await Context.Channel.GetMessagesAsync(range).Flatten();
                await Context.Channel.DeleteMessagesAsync(messageList);

                await ReplyAsync($"Deleted the last {range} messages.");
            }
            else
                await ReplyAsync("Couldn't delete messages: Insufficient role");
        }

        [Command("deleterange")]
        public async Task Delete([Summary("The most recent message ID to start deleting at")] ulong startMessage, [Summary("The last message ID to delete")] ulong endMessage)
        {
            if (Moderation.IsCorrectRole(Context, new[] { "Founders", "Moderators", "Regulars" })) // todo: replace this array with something better
            {
                var messageList = (await Context.Channel.GetMessagesAsync(500).Flatten()).ToList();
                int startIndex = messageList.IndexOf(messageList.FirstOrDefault(m => m.Id == startMessage)) + 1;
                int endIndex = messageList.IndexOf(messageList.FirstOrDefault(m => m.Id == endMessage), startIndex);
                await Context.Channel.DeleteMessagesAsync(messageList.GetRange(startIndex, endIndex - startIndex));

                await ReplyAsync($"Deleted {endIndex - startIndex} messages");
            }
            else
                await ReplyAsync("Couldn't delete message: Insufficient role");
        }
    }
}<commit_msg>Fixed deleterange with two message IDs<commit_after>﻿using Discord;
using Discord.API.Rest;
using Discord.Commands;
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;

namespace ContactsBot
{
    public class Moderation : ModuleBase
    {
        const ulong mutedRoleId = 251734975727009793;

        [Command("mute"), Summary("Mutes a user for the specified amount of time")]
        public async Task Mute([Summary("The user to mute")] IGuildUser user, [Summary("The time in minutes to mute the user")] double time = 30)
        {
            var guildUser = user as SocketGuildUser;
            if (guildUser == null) return;

            if (!IsCorrectRole(Context, new[] { "Founders", "Moderators", "Regulars" }))
             {
                await ReplyAsync("Couldn't mute user: Insufficient role");
                return;
            }

            TimeSpan timeToMute = TimeSpan.FromMinutes(time);

            var muteRole = guildUser.Guild.Roles.FirstOrDefault(r => r.Id == mutedRoleId);
            if (muteRole == null)
            {
                await ReplyAsync("Couldn't mute user: The specified role doesn't exist");
            }
            else
                await guildUser.AddRolesAsync(muteRole);

            Timer timer = new Timer(TimerCallback, user, (int)timeToMute.TotalMilliseconds, -1);
            Global.MutedTimers.Add(timer);
            Global.MutedUsers.Add(user, timer);

            await ReplyAsync($"Muted {guildUser.Nickname ?? guildUser.Username} for {time} minutes");
        }

        public async void TimerCallback(object user)
        {
            await Unmute(user as SocketGuildUser, true);
        }

        [Command("unmute"), Summary("Unmutes a user")]
        public async Task Unmute([Summary("The user to unmute")] IGuildUser user, bool isCallback = false)
        {
             if(!isCallback && !IsCorrectRole(Context, new[] { "Founders", "Moderators", "Regulars" }))
            {
                await ReplyAsync("Couldn't unmute user: Insufficient role");
                return;
            }
            var guildUser = user as SocketGuildUser;
            var muteRole = guildUser.Guild.Roles.FirstOrDefault(r => r.Id == mutedRoleId);
            if (muteRole == null)
            {
                await ReplyAsync("Couldn't unmute user: The specified role doesn't exist");
            }
            else
            {
                if (guildUser.RoleIds.Contains(mutedRoleId))
                    await guildUser.RemoveRolesAsync(muteRole);
                else
                    return;
            }

            Global.MutedTimers.Remove(Global.MutedUsers[user]);

            await ReplyAsync($"Unmuted {user.Nickname ?? user.Username}");
        }

        public static bool IsCorrectRole(CommandContext Context, string[] roleNames)
        {
            var roles = Context.Guild.Roles;
            var guildUser = Context.User as SocketGuildUser;
            if (guildUser == null || roles == null)
                return false;

            var rolesFromNames = roles.Where(r => roleNames.Any(n => r.Name == n));
            return guildUser.RoleIds.Any(id => rolesFromNames.Any(r => r.Id == id));
        }
    }

    [Group("message")]
    public class Messages : ModuleBase
    {
        [Command("deleterange")]
        public async Task Delete([Summary("The range of messages to delete")] int range)
        {
            if (Moderation.IsCorrectRole(Context, new[] { "Founders", "Moderators", "Regulars" }))
            {
                var messageList = await Context.Channel.GetMessagesAsync(range).Flatten();
                await Context.Channel.DeleteMessagesAsync(messageList);

                await ReplyAsync($"Deleted the last {range} messages.");
            }
            else
                await ReplyAsync("Couldn't delete messages: Insufficient role");
        }

        [Command("deleterange")]
        public async Task Delete([Summary("The most recent message ID to start deleting at")] ulong startMessage, [Summary("The last message ID to delete")] ulong endMessage)
        {
            if (Moderation.IsCorrectRole(Context, new[] { "Founders", "Moderators", "Regulars" })) // todo: replace this array with something better
            {
                var messageList = (await Context.Channel.GetMessagesAsync(500).Flatten()).ToList();
                int startIndex = messageList.FindIndex(m => m.Id == startMessage);
                int endIndex = messageList.FindIndex(m => m.Id == endMessage);
                if(startIndex == -1 || endIndex == -1)
                {
                    await ReplyAsync("Couldn't delete messages: The start or end message ID couldn't be found");
                    return;
                }
                var messageRange = (startIndex > endIndex) ? messageList.GetRange(endIndex - 1, (startIndex - endIndex) + 1) : messageList.GetRange(startIndex, (endIndex - startIndex) + 1);
                await Context.Channel.DeleteMessagesAsync(messageRange);

                await ReplyAsync($"Deleted {Math.Abs(endIndex - startIndex) + 1} messages");
            }
            else
                await ReplyAsync("Couldn't delete messages: Insufficient role");
        }
    }
}
{'fraction_non_alphanumeric': 0.10600366194468536, 'fraction_numerical': 0.005300183097234268, 'mean_word_length': 2.113711371137114, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2955031', 'n_tokens_mistral': 2909, 'n_tokens_neox': 2721, 'n_words': 709}