Exemple #1
0
        public static bool is_command(Discord.WebSocket.SocketMessage msg)
        {
            string msg_content = msg.Content;

            msg_content = msg_content.Trim();
            return(msg_content[0] == char_prefix);
        }
Exemple #2
0
        public async Task MessageReceivedAsync(Discord.WebSocket.SocketMessage message)
        {
            if (message.Source != Discord.MessageSource.User)
            {
                return;
            }
            if (!_channels.Contains(message.Channel.Id))
            {
                return;
            }
            if (!message.MentionedUsers.Any(p => p.Id == _discord.CurrentUser.Id))
            {
                return;
            }

            if (message.Attachments.Count > 0)
            {
                foreach (var attachment in message.Attachments)
                {
                    var stream = await _webService.GetFileAsStream(attachment.Url);

                    var image = Image.Load <Rgba32>(stream);
                    EdgeDetect(image);
                    await image.SaveAsync(Path.Combine(ImgPath, attachment.Filename));

                    await message.Channel.SendFileAsync(Path.Combine(ImgPath, attachment.Filename), "Ronners!");

                    image.Dispose();
                }
            }
        }
Exemple #3
0
        public static bool msg_mentioned_user(Discord.WebSocket.SocketUser user, Discord.WebSocket.SocketMessage msg, out string potential_command)
        {
#if DEBUG
            bool throws_false = false;
            if (msg == null)
            {
                Console.WriteLine("Tried to find if a message mentioned a user when message is null");
                throws_false = true;
            }
            if (user == null)
            {
                Console.WriteLine("Tried to find if a message mentioned a user when user is null");
                throws_false = true;
            }
            if (throws_false)
            {
                potential_command = null;
                return(false);
            }
#endif
            if (msg == null || user == null)
            {
                potential_command = null;
                return(false);
            }
            var message_mentioned = msg.MentionedUsers;
#if DEBUG
            uint count = 0;
#endif
            foreach (Discord.WebSocket.SocketUser usr in message_mentioned)
            {
#if DEBUG
                count++;
                Console.WriteLine(count + ". " + usr.ToString());
#endif
                if (same_user(usr, user))
                {
#if DEBUG
                    Console.WriteLine("Msg mentioned " + user.ToString());
#endif
                    potential_command = msg.Content;
                    if (potential_command.StartsWith(bot_const.client_mention_id))
                    {
                        potential_command = potential_command.Substring(bot_const.client_mention_id.Length);
                        potential_command = potential_command.Trim();
                    }
                    else if (potential_command.EndsWith(bot_const.client_mention_id))
                    {
                        potential_command = potential_command.Substring(0, potential_command.Length - bot_const.client_mention_id.Length);
                        potential_command = potential_command.Trim();
                    }
                    return(true);
                }
            }
#if DEBUG
            Console.WriteLine("Msg not mentioned " + user.ToString());
#endif
            potential_command = null;
            return(false);
        }
        private async Task Client_MessageReceived(Discord.WebSocket.SocketMessage arg)
        {
            if (!arg.Author.IsBot && arg.Author == _messageAuthor && arg.Channel == _currentChannel)
            {
                _discordClient.Client.MessageReceived -= Client_MessageReceived;
                EmbedBuilder embed = null;

                if (arg.Attachments.Count > 0)
                {
                    embed = new EmbedBuilder();
                    foreach (Attachment attachment in arg.Attachments)
                    {
                        // check if it is an image is only possible by checking width/height
                        if (attachment.Width != null)
                        {
                            embed.WithImageUrl(attachment.Url);
                        }
                        else
                        {
                            embed.WithDescription(attachment.Url);
                        }
                    }
                }

                await _channel.SendMessageAsync(_user?.Mention + " " + arg.Content, false, embed.Build());

                _channel        = null;
                _user           = null;
                _messageAuthor  = null;
                _currentChannel = null;
            }
        }
Exemple #5
0
        private static Task Ditto_MessageReceived(Discord.WebSocket.SocketMessage socketMessage)
        {
            Task.Run(async() =>
            {
                await HandleMessageAsync(socketMessage).ConfigureAwait(false);
            });

            return(Task.CompletedTask);
        }
Exemple #6
0
        private static Task OnMessageReceived(Discord.WebSocket.SocketMessage socketMessage)
        {
            var _ = Task.Run(async() =>
            {
                await ProcessMessageAsync(socketMessage).ConfigureAwait(false);
            });

            return(Task.CompletedTask);
        }
Exemple #7
0
        /// <summary>
        /// Gets called whenever the client receives a message from discord
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        private async Task on_message_received(Discord.WebSocket.SocketMessage msg)
        {
            string potential_command;
            bool   is_command_by_mentioned = false;

            //TODO: make this work
            Console.WriteLine("=============== New Message ===============");
            string content = msg.Content;

            Discord.WebSocket.SocketUserMessage user_msg = msg as Discord.WebSocket.SocketUserMessage;
            Console.WriteLine("Content: " + content);
            Console.WriteLine("Is user message: " + (user_msg != null ? "true" : "false"));
            if (msg.Author.IsBot)
            {
                Console.WriteLine("Message is generated by a bot");
            }
            if (same_user(msg.Author, client.CurrentUser))
            {
                Console.WriteLine("Message generated by myself, aborting processing");
                return;
            }

            if (msg_mentioned_user(client.CurrentUser, user_msg, out potential_command))
            {
#if DEBUG
                await msg.Channel.SendMessageAsync("You mentioned me?");
#endif
                is_command_by_mentioned = true;
            }
            else
            {
                Console.WriteLine("Too bad, not mentioned :(.");
            }
            if (msg.Content == "#_THIS_CHANNEL_ID(\"-reply\")")
            {
                await msg.Channel.SendMessageAsync("This channel's ID is: " + msg.Channel.Id);
            }

#if DEBUG
            System.DateTime benchmark_start = DateTime.Now;
#endif
            if (is_command_by_mentioned || bot_conf.is_command_raw(content, out potential_command))
            {
                string output = "Detected input command of " + potential_command + ".";
#if DEBUG
                System.DateTime benchmark_end = DateTime.Now;
                TimeSpan        delta         = benchmark_end - benchmark_start;
                output += " " + delta.TotalMilliseconds.ToString() + "ms took to complete command detection algorithm.";
#endif
                await msg.Channel.SendMessageAsync(output);

                await ExecuteCommand(potential_command, user_msg);
            }
        }
Exemple #8
0
        private async Task Bot_MessageReceived(Discord.WebSocket.SocketMessage arg)
        {
            if (arg.Content == "(╯°□°)╯︵ ┻━┻")
            {
                var contains = _tableflip.ContainsKey(arg.Channel.Id);
                if ((contains && _tableflip[arg.Channel.Id].AddSeconds(60) < DateTime.Now) || !contains)
                {
                    await arg.Channel.SendMessageAsync("┬─┬ ノ( ゜-゜ノ)");

                    _tableflip.AddOrUpdate(arg.Channel.Id, DateTime.Now, (key, oldValue) => DateTime.Now);
                }
            }
        }
Exemple #9
0
 /// <summary>
 /// Gets called whenever the client receives a message from discord
 /// </summary>
 /// <param name="msg"></param>
 /// <returns></returns>
 private async Task on_message_received(Discord.WebSocket.SocketMessage msg)
 {
     if (msg.Content == "!ping")
     {
         string         output  = "";
         DateTimeOffset created = msg.CreatedAt;
         DateTimeOffset msg_out = msg.Timestamp;
         TimeSpan       delta   = created - msg_out;
         output = "The message, was created at " + created.ToString()
                  + " and was out at " + msg_out.ToString()
                  + " which took " + delta.Milliseconds + "ms.";
         await msg.Channel.SendMessageAsync("pong;\n" + output);
     }
 }
Exemple #10
0
 private static async Task Client_MessageReceived(Discord.WebSocket.SocketMessage imsg)
 {
     try
     {
         if (imsg.Author.IsBot)
         {
             return;
         }
         var msg = imsg as IUserMessage;
         if (msg == null)
         {
             return;
         }
         var channel = imsg.Channel as ITextChannel;
         if (channel == null)
         {
             return;
         }
         if (msg.Author.Id == NadekoBot.Client.CurrentUser.Id)
         {
             return;
         }
         foreach (var subscriber in Subscribers)
         {
             var set = subscriber.Value;
             if (!set.Contains(channel))
             {
                 continue;
             }
             foreach (var chan in set.Except(new[] { channel }))
             {
                 try
                 {
                     await chan.SendMessageAsync(GetMessage(channel, (IGuildUser)msg.Author,
                                                            msg)).ConfigureAwait(false);
                 }
                 catch
                 {
                     // ignored
                 }
             }
         }
     }
     catch
     {
         // ignored
     }
 }
Exemple #11
0
        public static bool is_command(Discord.WebSocket.SocketMessage msg, out string new_string)
        {
            string msg_content = msg.Content;

            msg_content = msg_content.Trim();
            if (msg_content[0] == char_prefix)
            {
                new_string = msg_content.Substring(1);
                return(true);
            }
            else
            {
                new_string = null;
                return(false);
            }
        }
Exemple #12
0
        public static bool msg_mentioned_user(Discord.WebSocket.SocketUser user, Discord.WebSocket.SocketMessage msg)
        {
#if DEBUG
            bool throws_false = false;
            if (msg == null)
            {
                Console.WriteLine("Tried to find if a message mentioned a user when message is null");
                throws_false = true;
            }
            if (user == null)
            {
                Console.WriteLine("Tried to find if a message mentioned a user when user is null");
                throws_false = true;
            }
            if (throws_false)
            {
                return(false);
            }
#endif
            if (msg == null || user == null)
            {
                return(false);
            }
            var message_mentioned = msg.MentionedUsers;
#if DEBUG
            uint count = 0;
#endif
            foreach (Discord.WebSocket.SocketUser usr in message_mentioned)
            {
#if DEBUG
                count++;
                Console.WriteLine(count + ". " + usr.ToString());
#endif
                if (same_user(usr, user))
                {
#if DEBUG
                    Console.WriteLine("Msg mentioned " + user.ToString());
#endif
                    return(true);
                }
            }
#if DEBUG
            Console.WriteLine("Msg not mentioned " + user.ToString());
#endif
            return(false);
        }
Exemple #13
0
        public static bool is_command(Discord.WebSocket.SocketMessage msg)
        {
            //If msg is null, ignore it
            if (nullify_system_message)
            {
                Discord.WebSocket.SocketUserMessage user_msg = msg as Discord.WebSocket.SocketUserMessage;
                if (user_msg == null)
                {
                    return(false);
                }
            }
            else
            {
                if (msg == null)
                {
                    return(false);
                }
            }

            //set up processor (sorry if it's getting less and less readable)
            #region processor
            Dictionary <lookup_type, char[]> char_lookup = new Dictionary <lookup_type, char[]>();
            if (check_prefix_determine_command)
            {
                char_lookup[lookup_type.prefix] = new char[1] {
                    char_command_prefix
                };
            }
            if (check_suffix_determine_command)
            {
                char_lookup[lookup_type.suffix] = new char[1] {
                    char_command_suffix
                };
            }
            string content = msg.Content;
            custom_string_processor processor = new custom_string_processor(char_lookup, null, true);
            #endregion

            return(processor.string_satisfied(content));
        }
Exemple #14
0
            private Task Client_MessageReceived(Discord.WebSocket.SocketMessage msg)
            {
                var _ = Task.Run(async() =>
                {
                    await moveLock.WaitAsync().ConfigureAwait(false);
                    try
                    {
                        var curUser = _users[curUserIndex];
                        if (_phase == Phase.Ended || msg.Author?.Id != curUser.Id)
                        {
                            return;
                        }

                        int index;
                        if (int.TryParse(msg.Content, out index) &&
                            --index >= 0 &&
                            index <= 9 &&
                            _state[index / 3, index % 3] == null)
                        {
                            _state[index / 3, index % 3] = curUserIndex;

                            // i'm lazy
                            if (_state[index / 3, 0] == _state[index / 3, 1] && _state[index / 3, 1] == _state[index / 3, 2])
                            {
                                _state[index / 3, 0] = curUserIndex + 2;
                                _state[index / 3, 1] = curUserIndex + 2;
                                _state[index / 3, 2] = curUserIndex + 2;

                                _phase = Phase.Ended;
                            }
                            else if (_state[0, index % 3] == _state[1, index % 3] && _state[1, index % 3] == _state[2, index % 3])
                            {
                                _state[0, index % 3] = curUserIndex + 2;
                                _state[1, index % 3] = curUserIndex + 2;
                                _state[2, index % 3] = curUserIndex + 2;

                                _phase = Phase.Ended;
                            }
                            else if (curUserIndex == _state[0, 0] && _state[0, 0] == _state[1, 1] && _state[1, 1] == _state[2, 2])
                            {
                                _state[0, 0] = curUserIndex + 2;
                                _state[1, 1] = curUserIndex + 2;
                                _state[2, 2] = curUserIndex + 2;

                                _phase = Phase.Ended;
                            }
                            else if (curUserIndex == _state[0, 2] && _state[0, 2] == _state[1, 1] && _state[1, 1] == _state[2, 0])
                            {
                                _state[0, 2] = curUserIndex + 2;
                                _state[1, 1] = curUserIndex + 2;
                                _state[2, 0] = curUserIndex + 2;

                                _phase = Phase.Ended;
                            }
                            string reason = "";

                            if (_phase == Phase.Ended) // if user won, stop receiving moves
                            {
                                reason  = "Matched three!";
                                _winner = _users[curUserIndex];
                                NadekoBot.Client.MessageReceived -= Client_MessageReceived;
                                OnEnded?.Invoke(this);
                            }
                            else if (IsDraw())
                            {
                                reason = "A draw!";
                                _phase = Phase.Ended;
                                NadekoBot.Client.MessageReceived -= Client_MessageReceived;
                                OnEnded?.Invoke(this);
                            }

                            var sendstate = Task.Run(async() =>
                            {
                                var del1 = msg.DeleteAsync();
                                var del2 = previousMessage?.DeleteAsync();
                                try { previousMessage = await _channel.EmbedAsync(GetEmbed(reason)); } catch { }
                                try { await del1; } catch { }
                                try { await del2; } catch { }
                            });
                            curUserIndex ^= 1;

                            timeoutTimer.Change(15000, Timeout.Infinite);
                        }
                    }
                    finally
                    {
                        moveLock.Release();
                    }
                });

                return(Task.CompletedTask);
            }