Exemplo n.º 1
0
        public async Task PurgeIntros()
        {
            int interval = 50;

            Console.WriteLine("interval set");


            // check if command user is administrator

            SocketGuildUser ServerUser = Context.User as SocketGuildUser;

            if (!ServerUser.GuildPermissions.Administrator)
            {
                await Context.Channel.SendMessageAsync($":x: You don't have administrator permissions in this discord server! Ask an administrator or the owner to execute this command!");

                return;
            }

            Console.WriteLine("Trying to delete messages lol");
            await Context.Channel.SendMessageAsync("Trying to delete messages!");

            Console.WriteLine("Trying to delete messages lol 2");


            var GuildUsers = Context.Guild.Users;

            int messagesScanned = 0;
            int messagesDeleted = 0;

            //string[] channelNamesToPurge = serverSettings.introChannels.ToArray();

            ulong[] channelsToPurge = DataMethods.GetIntroChannelIDs(Context.Guild.Id);


            foreach (ulong channelToPurge in channelsToPurge)
            {
                int messagesScannedThisChannel = 0;
                int messagesDeletedThisChannel = 0;

                SocketTextChannel scannedChannel = Context.Guild.TextChannels.Where(x => x.Id == channelToPurge).FirstOrDefault();

                var MessagesFromChannel = await Context.Guild.TextChannels.Where(x => x.Id == channelToPurge).FirstOrDefault().GetMessagesAsync(interval).FlattenAsync();

                ulong lastMsgID = 0;
                //IMessage bombTest = await Context.Guild.TextChannels.Where(x => x.Name == channelNameToPurge).FirstOrDefault().GetMessageAsync(lastID);

                //int msgCount = MessagesFromChannel.Count();

                //await Context.Channel.SendMessageAsync($"First messages from #{channelNameToPurge} supposedly loaded! Message count: {msgCount}");

                foreach (IMessage CurrentMsg in MessagesFromChannel)
                {
                    //await Context.Channel.SendMessageAsync($"Looping message!");
                    //bool userFound = false;

                    lastMsgID = CurrentMsg.Id;
                    messagesScanned++;
                    messagesScannedThisChannel++;

                    //await Context.Channel.SendMessageAsync($"Looping message - ID {lastID}!");

                    /*
                     * foreach (SocketGuildUser CurrentUser in GuildUsers)
                     * {
                     *  if (CurrentMsg.Author.Id == CurrentUser.Id)
                     *  {
                     *      userFound = true;
                     *  }
                     * }
                     */

                    SocketGuildUser msgAuthor = Context.Guild.GetUser(CurrentMsg.Author.Id);

                    //if (!userFound)
                    if (msgAuthor == null)
                    {
                        await CurrentMsg.DeleteAsync();

                        messagesDeleted++;
                        messagesDeletedThisChannel++;
                    }
                }

                //await Context.Channel.SendMessageAsync($"First messages scanned in #{channelNameToPurge}!");


                while (true)
                {
                    IMessage currMessage = await Context.Guild.TextChannels.Where(x => x.Id == channelToPurge).FirstOrDefault().GetMessageAsync(lastMsgID);

                    //await Context.Channel.SendMessageAsync("We're in the while loop!");

                    if (currMessage == null)
                    {
                        var mgsToCount = await Context.Guild.TextChannels.Where(x => x.Id == channelToPurge).FirstOrDefault().GetMessagesAsync(limit: 1, fromMessageId: lastMsgID, dir: Direction.Before).FlattenAsync();

                        int theCount = mgsToCount.Count();

                        //await Context.Channel.SendMessageAsync("currMessage is returning null!");

                        if (theCount < 1)
                        {
                            break;
                        }
                    }
                    else
                    {
                        //messagesScanned--;
                        //messagesScannedThisChannel--;

                        var mgsToCount = await Context.Guild.TextChannels.Where(x => x.Id == channelToPurge).FirstOrDefault().GetMessagesAsync(limit: 2, fromMessageId: lastMsgID, dir: Direction.Before).FlattenAsync();

                        int theCount = mgsToCount.Count();


                        //await Context.Channel.SendMessageAsync($"currMessage is not returning null! count = {theCount}");

                        if (theCount < 2)
                        {
                            break;
                        }
                    }

                    //await Context.Channel.SendMessageAsync($"Deleting from message ID: {lastID}");
                    MessagesFromChannel = await Context.Guild.TextChannels.Where(x => x.Id == channelToPurge).FirstOrDefault().GetMessagesAsync(limit: interval, fromMessageId: lastMsgID, dir: Direction.Before).FlattenAsync();

                    foreach (IMessage CurrentMsg in MessagesFromChannel)
                    {
                        //bool userFound = false;

                        lastMsgID = CurrentMsg.Id;
                        messagesScanned++;
                        messagesScannedThisChannel++;
                        SocketGuildUser msgAuthor = Context.Guild.GetUser(CurrentMsg.Author.Id);

                        /*
                         * foreach (SocketGuildUser CurrentUser in GuildUsers)
                         * {
                         *  if (CurrentMsg.Author.Id == CurrentUser.Id)
                         *  {
                         *      userFound = true;
                         *  }
                         * }
                         */

                        //if (!userFound)
                        if (msgAuthor == null)
                        {
                            await CurrentMsg.DeleteAsync();

                            messagesDeleted++;
                            messagesDeletedThisChannel++;
                        }
                    }
                }

                await Context.Channel.SendMessageAsync($"All messages scanned from {scannedChannel.Mention}! Channel total: {messagesScannedThisChannel} messages scanned, {messagesDeletedThisChannel} messages deleted!");
            }



            await Context.Channel.SendMessageAsync($"All messages scanned! Total: {messagesScanned} messages scanned, {messagesDeleted} messages deleted!");
        }