Example #1
0
        /// <summary>
        /// Get a number of messages from a channel.
        /// </summary>
        /// <param name="c">The channel</param>
        /// <param name="numReq">The number of messages requested</param>
        /// <returns></returns>
        public static async Task<IEnumerable<Message>> GetMessages(Channel c, int numReq)
        {
            int currentCount = 0;
            int numToGetThisLoop = 100;
            int newMsgCount = 100;
            ulong lastMsgId;
            IEnumerable<Message> allMsgs = new List<Message>();
            IEnumerable<Message> newMsgs = new List<Message>();
            if (numReq <= 0) return null;                                         //Quit on bad request

            lastMsgId = (await c.DownloadMessages(1))[0].Id;                        //Start from last message (will be excluded)

            while (currentCount < numReq && newMsgCount == numToGetThisLoop)      //Keep going while we don't have enough, and haven't reached end of channel
            {
                if (numReq - currentCount < 100)                                  //If we need less than 100 to achieve required number
                    numToGetThisLoop = numReq - currentCount;                     //Reduce number to get this loop

                newMsgs = await c.DownloadMessages(numToGetThisLoop, lastMsgId, Relative.Before, false);    //Get N messages before that id
                newMsgCount = newMsgs.Count();                                      //Get the count we downloaded, usually 100
                currentCount += newMsgCount;                                        //Add new messages to count
                lastMsgId = newMsgs.Last().Id;                                      //Get the id to start from on next iteration
                allMsgs = allMsgs.Concat(newMsgs);                                  //Add messages to the list
            }

            return allMsgs;
        }
Example #2
0
 private void registerPurgeCmd()
 {
     commands.CreateCommand("purge")
     .Parameter("n", ParameterType.Required)
     .Parameter("channel", ParameterType.Optional)
     .AddCheck((cm, u, ch) => u.ServerPermissions.Administrator)
     .Do(async(e) =>
     {
         int.TryParse(e.GetArg("n"), out int n);
         if (n > 100)
         {
             await e.Channel.SendMessage("Uh... I can only delete up to a hundred messages at a time. Sorry...");
             return;
         }
         else if (e.GetArg("channel") == "")
         {
             Discord.Message[] messagesToDelete = await e.Channel.DownloadMessages(n);
             await e.Channel.DeleteMessages(messagesToDelete);
         }
         else
         {
             Discord.Channel channel = e.Server.FindChannels(e.GetArg("channel")).FirstOrDefault();
             Debug.WriteLine(channel.Name);
             Discord.Message[] messagesToDelete = await channel.DownloadMessages(n);
             await channel.DeleteMessages(messagesToDelete);
         }
         await e.Channel.SendMessage("Got 'em.");
     });
 }
Example #3
0
        /// <summary>
        /// Get messages from a channel up to a specified date.
        /// </summary>
        /// <param name="c">The channel</param>
        /// <param name="limit">The date to reach</param>
        /// <returns></returns>
        public static async Task<IEnumerable<Message>> GetMessages(Channel c, DateTime limit)
        {
            IEnumerable<Message> allMsgs = new List<Message>();
            IEnumerable<Message> newMsgs = new List<Message>();
            if (limit == null) return null;                                         //Quit on bad request

            var lastMessage = (await c.DownloadMessages(1))[0];                       //Start from last message (will be excluded)
            ulong lastMsgId = lastMessage.Id;
            var currentDate = lastMessage.Timestamp;

            while (currentDate.Date >= limit.Date)                                  //Keep going while we don't reach the date limit
            { 
                newMsgs = await c.DownloadMessages(100, lastMsgId, Relative.Before, false);
                if (!newMsgs.Any()) break;

                lastMsgId = newMsgs.Last().Id;                                      //Get the id to start from on next iteration
                currentDate = newMsgs.Last().Timestamp;                             //Note the current date
                allMsgs = allMsgs.Concat(newMsgs);                                  //Add messages to the list
            }

            return allMsgs;
        }
Example #4
0
        private async void  ChannelChanged(Channel channel)
        {
            try
            {
                friendsListGrid.Visibility = Visibility.Collapsed;
                chatWindow.Visibility      = Visibility.Visible;
                ActiveChannel = channel;
                DeterminePeopleButtonVisibility(ActiveChannel);
                SetMenuBarHeader(ActiveChannel);
                chatWindow.ClearChatWindow();
                Message[] messages = await channel.DownloadMessages(MaxMessageDownloadCount);

                var reverseMessages = messages.Reverse().ToArray();
                ProcessMessages(reverseMessages);
            }
            catch (TaskCanceledException)
            {
                App.TryGracefulRelaunch();
            }
            catch (OperationCanceledException)
            {
                App.TryGracefulRelaunch();
            }
            catch (HttpRequestException)
            {
                var popup = new MessageDialog(
                    "You have lost internet connection. Please try again when internet is available.",
                    "Connection Lost");
                popup.Commands.Add(new UICommand("Exit App")
                {
                    Id = 0
                });
                var result = await popup.ShowAsync();

                CoreApplication.Exit();
            }
            catch (Exception exception)
            {
                App.LogUnhandledError(exception);
            }
        }
Example #5
0
        /// <summary>
        /// Get a number of messages from a channel by a specific user.
        /// </summary>
        /// <param name="c">The channel</param>
        /// <param name="u">The user</param>
        /// <param name="numReq">The number of messages requested</param>
        /// <returns></returns>
        public static async Task<IEnumerable<Message>> GetMessages(Channel c, User u, int numReq)
        {
            int currentCount = 0;
            int numToGetThisLoop = 100;
            int newMsgCount = 100;
            ulong lastMsgId;
            IEnumerable<Message> allMsgs = new List<Message>();
            IEnumerable<Message> newMsgs = new List<Message>();
            if (numReq <= 0) return null;                                         //Quit on bad request

            lastMsgId = (await c.DownloadMessages(1))[0].Id;                        //Start from last message (will be excluded)

            while (currentCount < numReq && newMsgCount == numToGetThisLoop)      //Keep going while we don't have enough, and haven't reached end of channel
            {
                newMsgs = await c.DownloadMessages(numToGetThisLoop, lastMsgId, Relative.Before, false);    //Get N messages before that id
                newMsgCount = newMsgs.Count();                                      //Get the count we downloaded, usually 100
                lastMsgId = newMsgs.Last().Id;                                      //Get the id to start from on next iteration
                newMsgs = newMsgs.Where(x => x.User == u);                          //Add only messages of user
                currentCount += newMsgs.Count();                                    //Add new messages to count
                allMsgs = allMsgs.Concat(newMsgs);                                  //Add messages to the list
            }

            return allMsgs.Take(numReq);
        }
        public async void RemoveMessages(Discord.Server server)
        {
            if (server.Id != DiscordIds.AtlasId)
            {
                SettingsRepo      settingsRepo = new SettingsRepo(new SettingsContext());
                Discord.Channel   channel      = server.GetChannel(settingsRepo.GetLfgChannel(server.Id));
                Discord.Message[] temp         = await channel.DownloadMessages(100);

                bool found = false;
                try
                {
                    while (temp.Length > 1 && temp.Last().Text != "queue has been cleared!")
                    {
                        await channel.DeleteMessages(temp);

                        found = true;
                        temp  = await channel.DownloadMessages(100);
                    }
                }
                catch
                {
                    found = true;
                }
                if (found == true)
                {
                    await channel.SendMessage("Queue has been cleared!");
                }
            }
            else if (server.Id == DiscordIds.AtlasId)
            {
                List <Channel> channels = new List <Channel>();
                foreach (var channel in server.TextChannels)
                {
                    if (channel.Name.Contains("queue"))
                    {
                        channels.Add(channel);
                    }
                }
                foreach (var channel in channels)
                {
                    Discord.Message[] temp = await channel.DownloadMessages();

                    bool found = false;
                    try
                    {
                        while (temp.Length > 1 && temp.Last().Text != "queue has been cleared!")
                        {
                            await channel.DeleteMessages(temp);

                            found = true;
                            temp  = await channel.DownloadMessages();
                        }
                    }
                    catch
                    {
                        found = true;
                    }
                    if (found)
                    {
                        await channel.SendMessage("Queue has been cleared!");
                    }
                }
            }
        }