Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            if (!restart)
            {
                rpg = new KokoroBotRPG();
                loadFiles();
            }
            else
            {
                restart = false;
            }
            {
                DiscordClientConfig config = new DiscordClientConfig();
                config.VoiceMode = DiscordVoiceMode.Outgoing;
                config.VoiceBufferLength = 40;
                var client = new DiscordClient(config);
                
                //Display all log messages in the console
                client.LogMessage += (s, e) => Console.WriteLine($"[{e.Severity}] {e.Source}: {e.Message}");
                
                client.MessageReceived += async (s, e) =>
                {
                    Console.WriteLine(e.Message.User.Name + ": " + e.Message.Text);
                    if (!e.Message.IsAuthor)
                    {
                        var currentChannel = e.Channel;
                        if (e.User.Id == 95543627391959040)
                        {
                            if (e.Message.Text == "-mute")
                            {
                                mute = !mute;
                                await client.SendMessage(currentChannel, "KokoroBot is now mute: " + mute.ToString());
                            }
                            else if (e.Message.Text == "-clear")
                            {
                                if (voiceclient != null)
                                    voiceclient.ClearVoicePCM();
                            }
                            else if (e.Message.Text == "-save")
                            {
                                saveFiles();
                                await client.SendMessage(currentChannel, "I have saved everything :3");
                            }
                            else if (e.Message.Text == "-dc")
                            {
                                quit = true;
                                running = false;
                            }
                            else if (e.Message.Text == "-restart")
                            {
                                await client.SendMessage(currentChannel, "Cya on the other side :3");
                                restart = true;
                                running = false;
                                await client.Disconnect();
                            }
                            else if (e.Message.Text.StartsWith("-join"))
                            {
                                var channels = e.Server.Channels.Where((Channel chan) => {
                                    return e.Message.Text.Substring(5).TrimStart(' ') == chan.Name && chan.Type == ChannelType.Voice;  });
                                if (channels.Any())
                                {
                                    var channel = channels.First();
                                    Console.WriteLine("KokoroBot tries to join Channel: " + channel.Name);
                                    voiceclient = await client.JoinVoiceServer(channel);
                                    voiceserver = e.Message.Server;
                                }
                            }
                            else if (e.Message.Text == "-leave")
                            {
                                if (voiceclient != null)
                                {
                                    voiceclient.ClearVoicePCM();
                                    await client.LeaveVoiceServer(voiceserver);
                                    voiceclient = null;
                                    voiceserver = null;
                                }
                            }
                        }
                        else if (e.User.Name == "part")
                        {
                            await client.SendMessage(currentChannel, "I don't like you. B-b-baka. >.<");
                            return;
                        }
                        if (!mute) {
                            if (e.Message.Text.Length > 0)
                            {
                                string[] splitmessage = e.Message.Text.Split(' ');
                                if (splitmessage[0] == "-kardfacts")
                                {
                                    if (splitmessage.Length > 2)
                                    {
                                        if (splitmessage[1] == "add")
                                        {
                                            try
                                            {
                                                string finalstr = "";
                                                for (int i = 2; i < splitmessage.Length; i++)
                                                {
                                                    if (i != 2)
                                                        finalstr += ' ' + splitmessage[i];
                                                    else
                                                        finalstr = splitmessage[i];
                                                }
                                                if (finalstr.Length > 5)
                                                {
                                                    kardFactsStrings.Add(finalstr);
                                                    await client.SendMessage(currentChannel, "A new fact about Kard has been added. (Yay ^-^):");
                                                    currentChannel = e.Channel;
                                                    await client.SendMessage(currentChannel, finalstr);
                                                }
                                                else
                                                {
                                                    throw new IOException("Hue.");
                                                }
                                            }
                                            catch (Exception)
                                            {
                                                await client.SendMessage(currentChannel, "That hurt <.< Don't do this again, ok? :3");
                                            }
                                        }
                                    }
                                    else
                                    {
                                        await client.SendMessage(currentChannel, kardFacts());
                                    }
                                }
                                else if (e.Message.Text.StartsWith("-play"))
                                {
                                    Task.Run(() => { PlaySoundWav(e); });
                                }
                                else if(e.Message.Text.StartsWith("-getclientid"))
                                {
                                    if (e.Message.Text.Length > "-getclientid ".Length)
                                    {
                                        try {
                                            await client.SendMessage(currentChannel, e.Server.Members.Where((User u) =>
                                           {
                                               return u.Name.StartsWith(e.Message.Text.Substring("-getclientid ".Length));
                                           }).First().Id.ToString());
                                        }
                                        catch(Exception)
                                        {
                                            await client.SendMessage(currentChannel, "User does not exist. B-baka.");
                                        }
                                    }
                                    else
                                    {
                                        await client.SendMessage(currentChannel, e.Message.User.Id.ToString());
                                    }
                                }
                                else if(e.Message.Text.StartsWith(":"))
                                {
                                    await rpg.HandleCommands(e, client, currentChannel);
                                }
                                else if (await handleSimpleCommands(e, client, currentChannel) == false)
                                {
                                    await handleTiroCommands(e, client, currentChannel);
                                }
                            }

                            
                        }
                    }
                };

                //Convert our sync method to an async one and block the Main function until the bot disconnects
                client.Run(async () =>
                {
                    //Connect to the Discord server using our email and password
                    await client.Connect(Sensitive.email, Sensitive.passwd);

                    while (running)
                    {
                        var inputTask = Task.Run<string>((Func<string>)Console.ReadLine);
                        await inputTask;
                        string dbgCommand = inputTask.Result;
                        if( dbgCommand == "exit" || restart || quit)
                        {
                            running = false;
                            await client.Disconnect();
                        } else if ( dbgCommand == "listservers")
                        {
                            foreach(Server s in client.AllServers)
                            {
                                Console.WriteLine("#######################################");
                                Console.WriteLine("Servername: " + s.Name);
                                Console.WriteLine("Voicechannels: ");
                                foreach(Channel c in s.VoiceChannels)
                                {
                                    Console.WriteLine("    "+c.Name);
                                }
                                Console.WriteLine("Channels: ");
                                foreach (Channel c in s.Channels)
                                {
                                    Console.WriteLine("    "+c.Name);
                                }
                            }
                        }
                    }
                    //If we are not a member of any server, use our invite code (made beforehand in the official Discord Client)
                });
            }
            if (!restart)
            { 
                saveFiles();
            }
            else
            {
                Main(new string[] { });
            }
        }