Exemplo n.º 1
0
        //gets pokemon info
        public static Pokemans getPokeInfo(string name)
        {
            Pokemans       temp       = new Pokemans();
            JsonSerializer serializer = new JsonSerializer();

            Console.WriteLine(name);
            WebClient webClient = new WebClient();
            string    rootPath  = "C:\\Users\\thoma\\Documents\\visual studio 2015\\Projects\\EmoticonBot\\EmoticonBot\\pokemon\\";

            webClient.DownloadFile(string.Format("http://pokeapi.co/api/v2/pokemon/{0}/", name), (rootPath + "pokemon.txt"));
            Console.WriteLine("Successfully downloaded: {0}.txt", name);
            string      jsonData          = System.IO.File.ReadAllText((rootPath + "pokemon.txt"));
            XmlDocument pokemon           = JsonConvert.DeserializeXmlNode(jsonData, "root");
            XmlElement  xelRoot           = pokemon.DocumentElement;
            XmlNodeList types             = pokemon.SelectNodes(@"//root/types");
            XmlNodeList xmlNodesAbilities = pokemon.SelectNodes(@"//root/abilities");
            XmlNodeList statS             = pokemon.SelectNodes(@"//root/stats");


            temp.name  = pokemon.SelectSingleNode("/root/forms/name").InnerText;
            temp.id    = pokemon.SelectSingleNode("/root/id").InnerText;
            temp.image = string.Format("http://pokeapi.co/media/sprites/pokemon/{0}.png", temp.id);
            int countA = 0;
            int countT = 0;
            int count  = 0;

            foreach (XmlNode stat in statS)
            {
                temp.stats[count++] = stat.SelectSingleNode("base_stat").InnerText;
                Console.WriteLine("test: {0}", temp.stats[count - 1]);
            }
            Console.WriteLine("test: {0}", temp.stats[count - 1]);
            foreach (XmlNode xmNode in xmlNodesAbilities)
            {
                temp.abilities[countA++] = xmNode.SelectSingleNode("ability/name").InnerText;
            }
            foreach (XmlNode type in types)
            {
                temp.types[countT++] = type.SelectSingleNode("type/name").InnerText;
            }
            Console.WriteLine(temp.image);
            return(temp);
        }
Exemplo n.º 2
0
        private async Task Bot_MessageReceived(SocketMessage message)
        {
            var m = message as SocketUserMessage;

            if (m == null || m.Author.Id == 207432805406343168)
            {
                return;
            }
            if (message.Content.ToString().IndexOf(":") >= 0)
            {
                Console.WriteLine("{0} said: {1}", message.Author.Username, message.Content.ToString());
                string[] words = System.IO.File.ReadAllLines("C:\\Users\\thoma\\Documents\\visual studio 2015\\Projects\\EmoticonBot\\EmoticonBot\\configs\\commands.txt");

                //help command displays help information for users, the information is read from the file commands.txt which is stored in the config folder for the bot
                if (message.Content.ToString().ToLower().Contains(":halp"))
                {
                    string help    = "```Commands(Start all commands with ':'. Example-> :kappa):\n";
                    int    counter = 0;
                    foreach (string word in words)
                    {
                        counter++;
                        help += word.Substring(1, word.IndexOf(" ") - 1) + ", ";
                        if (counter % 4 == 0)
                        {
                            help += "\n";
                        }
                    }//end of for loop
                    help = help.Remove(help.Length - 2, 1);
                    help = help.Insert(help.Length - 1, ".```");
                    System.Threading.Thread.Sleep(150);
                    await message.Channel.SendMessageAsync(help);

                    Console.WriteLine("help called");
                }//end halp

                //coinflip command, calls coinflip and returns the filepath to either a heads or a tails. The image is then sent to discord
                else if (message.Content.ToString().ToLower().Contains(":coinflip"))
                {
                    string image = coinFlip();


                    await message.Channel.SendFileAsync(image);
                }


                //summon command, calls the summon function which makes the bot join the voice channel of the person who called it. Also sets _vClient equal to the return IAudioClient from the join function
                else if (message.Content.ToString().ToLower().Contains(":summon"))
                {
                    summon(message);
                }
                //end summon

                //play command, will determine if user is trying to play a local file(sound bites etc.), a youtube link, and more types of media will be added later(soundcloud?)
                //
                else if (message.Content.ToString().ToLower().Contains(":play"))
                {
                    try
                    {
                        if (ac == null)
                        {
                            await message.Channel.SendMessageAsync("Please type :summon before attempting to play a song");

                            return;
                        }
                        string linkOrName = message.Content.ToString().Substring(message.Content.ToString().IndexOf(" ") + 1);

                        string rootPath = "C:\\Users\\thoma\\Documents\\visual studio 2015\\Projects\\EmoticonBot\\EmoticonBot\\audio\\";
                        if (message.Content.IndexOf(" ") > -1)
                        {
                            if (RemoteFileExists(linkOrName))
                            {
                                if (linkOrName.ToLower().Contains("youtube"))
                                {
                                    Song song = downloadSong(linkOrName);

                                    //mp4ToMp3(song.filename + ".mp4", song.filename + ".mp3");

                                    if (song.name != " ")
                                    {
                                        await message.Channel.SendMessageAsync(String.Format("Now Playing: {0}" + "\n", song.name));

                                        songs.addToQueue(song, message);

                                        if (songs.isPlaying == false)
                                        {
                                            songs.isPlaying = true;
                                            System.Threading.Thread thread = new System.Threading.Thread(() => musicPlayer(songs));
                                            thread.Name = "Song player";
                                            thread.Start();
                                        }
                                    }
                                    else
                                    {
                                        await message.Channel.SendMessageAsync("```An error occured while trying to play the requested song```");
                                    }
                                }
                            }

                            else
                            {
                                await message.Channel.SendMessageAsync("```Invalid link!```");
                            }
                        }
                    }
                    catch (NullReferenceException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    catch
                    {
                        await message.Channel.SendMessageAsync("```An error occured while trying to play/download your file. Either it could not be found or it was invalid```");
                    }
                }


                //9gag link changer, a 9gag link has been detected so the link will be converted to a stripped down version depending on the type of media. If the media type is gif then it will straight send a gif rather than a link
                else if (message.Content.ToLower().Contains("9gag.com"))
                {
                    string msg      = message.Content;
                    string basePath = "http://img-9gag-fun.9cache.com/photo/";
                    string final    = " ";
                    string rest     = msg.Substring(msg.IndexOf("gag/") + 4);
                    //parsing link to find appropiate base link
                    if (rest.IndexOf("?") >= 0)
                    {
                        rest = rest.Substring(0, rest.IndexOf("?"));
                    }

                    //media type is gif
                    if (RemoteFileExists(basePath + rest + "_460sa.gif"))
                    {
                        final = basePath + rest + "_460sa.gif";
                    }

                    //media type is mp4
                    else if (RemoteFileExists(basePath + rest + "_460sv.mp4"))
                    {
                        final = basePath + rest + "_460sv.mp4";
                    }

                    //media type is webm
                    else if (RemoteFileExists(basePath + rest + "_460svwm.webm"))
                    {
                        final = basePath + rest + "_460svwm.webm";
                    }

                    //media type is jpg dont do anything
                    else if (RemoteFileExists(basePath + rest + "_700b.jpg"))
                    {
                        ;
                    }
                    ///as long as the file wasnt a jpg, send the new link/ or gif
                    if (final != " ")
                    {
                        await message.Channel.SendMessageAsync("9gag link detected, better link or gif here:");

                        await message.Channel.SendMessageAsync(final);
                    }
                }//end 9gag

                //pokemon command, this command calls on the pokeapi and grabs information about the request pokemon also saves the image of that pokemon
                else if (message.Content.ToLower().Contains(":pokemon"))
                {
                    try
                    {
                        string   pokeName = message.Content.ToLower().Substring(message.Content.ToLower().IndexOf(" ") + 1);
                        Pokemans pokeTemp = new Pokemans();
                        pokeTemp = Pokemans.getPokeInfo(pokeName);
                        WebClient webClient = new WebClient();
                        string    imagePath = string.Format("C:\\Users\\thoma\\Documents\\visual studio 2015\\Projects\\EmoticonBot\\EmoticonBot\\pokemon\\{0}.png", pokeTemp.id);
                        if (!System.IO.File.Exists(imagePath))
                        {
                            webClient.DownloadFile(pokeTemp.image, imagePath);
                        }
                        else
                        {
                            Console.WriteLine("image already exists in cache");
                        }
                        string lmessage = string.Format("```Name: {0}\nSpeed: {1}\nSpecial defense: {2}\nSpecial attack: {3}\nDefense: {4}\nAttack: {5}\nHP: {6}\nAbility 1: {7}\n", pokeTemp.name, pokeTemp.stats[0], pokeTemp.stats[1], pokeTemp.stats[2], pokeTemp.stats[3], pokeTemp.stats[4], pokeTemp.stats[5], pokeTemp.abilities[0]);

                        if (pokeTemp.abilities[1] != " ")
                        {
                            lmessage += "Ability 2: " + pokeTemp.abilities[1] + "\n";
                        }
                        if (pokeTemp.abilities[2] != " ")
                        {
                            lmessage += "Ability 3: " + pokeTemp.abilities[2] + "\n";
                        }
                        lmessage += "Type: " + pokeTemp.types[0] + "\n";
                        if (pokeTemp.types[1] != " ")
                        {
                            lmessage += "Sub-type: " + pokeTemp.types[1] + "\n";
                        }
                        lmessage += "```";
                        await message.Channel.SendFileAsync(imagePath);

                        System.Threading.Thread.Sleep(125);
                        await message.Channel.SendMessageAsync(lmessage);
                    }//end try (Pokemon error check)
                    catch
                    {
                        await message.Channel.SendMessageAsync("Couldn't find pokemon in the database!");

                        Console.WriteLine("Couldn't find pokemon in the database!");
                    } //end catch (Pokemon error check)
                }     //end pokemon command

                //weather command, calls the weather function and sends information about the weather in the requseted location
                else if (message.Content.ToLower().Contains(":weather"))
                {
                    string location = message.Content.ToLower();
                    weather(location, message);
                }//end weather command

                //cards command, does a bunch of stuff with a deck of cards. Uses an online card api for the info api: http://deckofcardsapi.com
                else if (message.Content.ToLower().Contains(":cards"))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    Card           cardDrawn  = new Card();
                    //Console.WriteLine("this is a test");
                    WebClient   webClient = new WebClient();
                    string      deckId    = "";
                    string      rootPath  = "C:\\Users\\thoma\\Documents\\visual studio 2015\\Projects\\EmoticonBot\\EmoticonBot\\cards\\";
                    string      jsonData  = System.IO.File.ReadAllText(rootPath + "deck.txt");
                    XmlDocument deck      = JsonConvert.DeserializeXmlNode(jsonData, "root");
                    string      drawName  = message.Author.Username;
                    if (!Game.isGameRunning)
                    {
                        deckId   = deck.SelectSingleNode("/root/deck_id").InnerText;
                        jsonData = System.IO.File.ReadAllText(rootPath + "deck.txt");
                        webClient.DownloadFile("http://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1", (rootPath + "deck.txt"));
                    }
                    if (message.Content.ToLower().Contains("draw"))
                    {
                        Console.WriteLine(deckId);

                        if (!Game.isGameRunning)
                        {
                            cardDrawn = deckDraw(message, " ", drawName);
                        }
                        else
                        {
                            cardDrawn = deckDraw(message, Game.deckId, drawName);
                        }
                        await message.Channel.SendFileAsync(cardDrawn.imagePath);

                        Console.WriteLine(cardDrawn.valueS);
                    }
                    else if (message.Content.ToLower().Contains("new") && !Game.isGameRunning)
                    {
                        await message.Channel.SendMessageAsync("A new game of cards has been started, to participate in the game type :draw, to end the game type :cards quit");

                        Game.isGameRunning = true;
                        Game.deckId        = deckId;
                        Console.WriteLine(deckId);
                    }
                    else if (message.Content.ToLower().Contains("quit") && Game.isGameRunning)
                    {
                        Game.isGameRunning = false;
                        if (Game.currentLeader != null)
                        {
                            await message.Channel.SendMessageAsync(string.Format("game has been ended by: {0}, the winner was: {1} with a {2} of {3}", message.Author.Username, Game.currentLeader, Game.currentHighCard.cardVal.ToLower(), Game.currentHighCard.suit.ToLower()));
                        }
                        Game.deckId          = "";
                        Game.currentHigh     = 0;
                        Game.currentHighCard = cardDrawn;
                        Game.currentLeader   = "";
                    }
                }//end card game

                //turns my wemo on or off, only can be called by me
                else if (message.Content.ToLower().Contains(":wemo"))
                {
                    if (message.Author.Id == 140268276415594496)
                    {
                        Console.WriteLine("Calling wemo");
                        wemo(message.Content.ToLower());
                    }
                }//end wemo command

                //purge command, deletes the requested amount of messages
                else if (message.Content.ToLower().Contains(":purge"))
                {
                    //purgeMessages(message);
                }
                else if (message.Content.ToLower().Contains(":sort"))
                {
                    Potter p = harrypotter();
                    if (p != null)
                    {
                        await message.Channel.SendMessageAsync(p.Message);

                        await message.Channel.SendFileAsync(p.Image);
                    }
                }
                else if (message.Content.ToLower().Contains(":screenshot"))
                {
                    string link = message.Content;
                    if (!(link.IndexOf(' ') > 0))
                    {
                        await message.Channel.SendMessageAsync("Failed to find inspect link from message make sure message format is :screenshot inspectlinkhere");
                    }

                    link = link.Substring(link.IndexOf(' '));
                    string id     = link.Substring(link.IndexOf("%") + 1);
                    string status = String.Empty;
                    string final  = String.Format("https://metjm.net/shared/screenshots-v5.php?cmd=request_new_link&inspect_link={0}", id);
                    string html   = getEntry(final);
                    long   fid    = 0;
                    if (html.Contains("screen_age"))
                    {
                        secReq re = JsonConvert.DeserializeObject <secReq>(html);
                        status = getEntry(String.Format("https://metjm.net/shared/screenshots-v5.php?cmd=request_screenshot_status&id={0}", re.result.screen_id));
                        fid    = re.result.screen_id;
                    }
                    else
                    {
                        req3 re2 = JsonConvert.DeserializeObject <req3>(html);
                        status = getEntry(String.Format("https://metjm.net/shared/screenshots-v5.php?cmd=request_screenshot_status&id={0}", re2.result.screen_id));
                        fid    = re2.result.screen_id;
                    }
                    Thread.Sleep(1000);
                    Console.WriteLine(status);
                    Console.WriteLine(html);
                    Csitem request = JsonConvert.DeserializeObject <Csitem>(status);
                    if (request.result.image_url == null)
                    {
                        await message.Channel.SendMessageAsync("Invalid link");

                        return;
                    }
                    while (status.Contains("place_in_queue"))
                    {
                        await message.Channel.SendMessageAsync(String.Format("Waiting to get screenshot, you are number {0} in queue, checking again in 5 seconds", status.Substring(status.IndexOf("place_in_queue") + 16, 1)));

                        Thread.Sleep(5001);
                        status = getEntry(String.Format("https://metjm.net/shared/screenshots-v5.php?cmd=request_screenshot_status&id={0}", fid));
                    }
                    request = JsonConvert.DeserializeObject <Csitem>(status);
                    await message.Channel.SendMessageAsync(request.result.image_url);
                }
                else
                {
                    try
                    {
                        string lmessage   = message.Content.ToLower();
                        string temp       = "";
                        string tempF      = "";
                        int    index      = 0;
                        int    indexSpace = 0;
                        bool   status     = true;
                        while (status)
                        {
                            string[] lines = System.IO.File.ReadAllLines(@"C:\\Users\\thoma\Documents\\visual studio 2015\\Projects\\EmoticonBot\\EmoticonBot\\configs\\commands.txt");
                            index = lmessage.IndexOf(":");
                            Console.WriteLine(index);
                            if (index >= 0)
                            {
                                lmessage = lmessage.Substring(index + 1);
                                //Console.WriteLine(message);
                                indexSpace = lmessage.IndexOf(" ");
                                if (indexSpace >= 0)
                                {
                                    temp = lmessage.Substring(0, indexSpace);
                                    // Console.WriteLine("displaying temp: {0}", temp);
                                }
                                else
                                {
                                    temp   = lmessage;
                                    status = false;
                                }
                                foreach (string line in lines)
                                {
                                    tempF = line.Substring(1, line.IndexOf(" ") - 1);
                                    if (temp == tempF)
                                    {
                                        string ifile = line.Substring(line.IndexOf(" ") + 1); //image file name is pulled from config.txt
                                        Console.WriteLine(temp);
                                        await message.Channel.SendFileAsync(ifile);           //send file specified from config.txt
                                    }
                                }//end of for loop
                                status = false;
                                lines  = null;
                            }
                            else
                            {
                                status = false;
                                continue;
                            }
                        }
                    }
                    catch
                    {
                        Console.WriteLine("shit dont have no colon, no worries");//string parsing dun messed up, who knows what the problem could be
                    }
                }
            } //end of author check
        }     //end of Bot_MessageReceived