Beispiel #1
0
 public void SendConsole(object text)
 {
     NonBlockingConsole.WriteLine(text);
 }
Beispiel #2
0
        public void Start()
        {
            #region InitClient

            _client = new DiscordClient();
            #endregion

            #region Commands
            _client.UsingCommands(x =>
            {
                x.PrefixChar = '$';
                x.HelpMode   = HelpMode.Public;
            });

            // -------

            _client.GetService <CommandService>().CreateCommand("roll") //create command greet
            .Description("Roll a dice between <from> to <to>")          //add description, it will be shown when ~help is used
            .Parameter("From", ParameterType.Required)                  //as an argument, we have a person we want to greet
            .Parameter("To", ParameterType.Required)

            .Do(async e =>
            {
                int randResult = 1;

                bool woot = true;
                try
                {
                    randResult = new Random().Next(int.Parse(e.GetArg("From")), (int.Parse(e.GetArg("To"))));
                }
                catch (Exception)
                {
                    await e.Channel.SendMessage("The number was out of bounds, sorry :frowning:");
                    woot = false;
                }
                if (woot)
                {
                    await e.Channel.SendMessage($"This roll made {randResult} ! ");
                    if (randResult == 69)
                    {
                        await e.Channel.SendMessage(@"Kek it is 69 ¯\_(ツ)_/¯");
                    }
                }



                //sends a message to channel with the given text
            });



            _client.GetService <CommandService>().CreateCommand("shutup") //create command greet (totally kek)
            .Description("Says violently to the bot \"SHUT UP PLEASE\"")  //add description, it will be shown when ~help is used
            .Alias(new string[] { "stfu", "shutthefuckup" })
            .Parameter("kekle", ParameterType.Unparsed)
            .Do(async e =>
            {
                if (shutup)
                {
                    await e.Channel.SendMessage("Yay :) I can now share the kek with everyone ! :cake: ");
                    _client.SetGame(new Game($"Keking everyone ! {version}"));
                    shutup = false;
                }
                else
                {
                    await e.Channel.SendMessage("Ok... :cry: ");
                    _client.SetGame(new Game($"Doing nothing, {version}"));
                    shutup = true;
                }


                //sends a message to channel with the given text
            });
            Channel      voiceChannel = null;
            IAudioClient _vClient     = null;
            bool?        isComplete   = true;

            _client.GetService <CommandService>().CreateCommand("stopmusic")
            .Description("Stops the music")
            .Do(async e =>
            {
                try
                {
                    token.Cancel();
                    await voiceChannel.LeaveAudio();
                }
                catch (Exception ex) when(ex is TaskCanceledException || ex is NullReferenceException)
                {
                    await e.Channel.SendMessage("There is no audio playing for now");
                }
                finally
                {
                    await e.Channel.SendMessage("Audio stopped.");
                }
            });
            #endregion

            _client.MessageReceived += async(s, e) =>
            {
                //if (!e.Message.IsAuthor)
                //    await e.Channel.SendMessage(e.Message.Text);



                if (Regex.IsMatch(e.Message.Text.ToLower(), "ke{1,100}k") && !e.Message.IsAuthor && !e.Message.IsMentioningMe() && !shutup && !e.Message.User.IsBot)
                {
                    await e.Channel.SendMessage($"Oh yeah ! {e.User.NicknameMention} is the kekkest guy ever !");

                    SendConsole($"Someone : {e.User.Name} has said topkek, message sent :)");

                    try
                    {
                        voiceChannel = _client.FindServers(e.Server.Name).FirstOrDefault().VoiceChannels.ToList().Find((Channel c) =>
                        {
                            return(c.Name.ToLower().Contains("kek") == true || c.Name.ToLower().Contains("General") == true);
                        });
                        _vClient = await _client.GetService <AudioService>() // We use GetService to find the AudioService that we installed earlier. In previous versions, this was equivelent to _client.Audio()
                                   .Join(voiceChannel);
                    }
                    catch (NullReferenceException)
                    {
                        await e.Channel.SendMessage("No voice channels that contains \"General\" or \"kek\" has been found. Canceling music :(");
                    }
                    catch (Exception ex)
                    {
                        await e.Channel.SendMessage($"wtf i dunno wat happened so i sent you that hope you like this ```{ex.Message} OMG AND ALSO {ex.StackTrace}```");
                    }
                    if (new Random().Next(0, 3) == 1 && voiceChannel != null)
                    {
                        await _vClient.Join(voiceChannel);

                        await e.Channel.SendMessage("LET THE KEK PLAYS");

                        NonBlockingConsole.WriteLine("Is complete : " + isComplete);
                        if (isComplete == true || isComplete == null)
                        {
                            isComplete = false;
                            token      = new CancellationTokenSource();
                            isComplete = await SendAudioAsync(voiceChannel, _vClient, token, 20);
                        }
                    }
                }
            };
            _client.UsingAudio(x =>
            {
                x.Mode = AudioMode.Both;
            });

            #region ConnectingAndTokenPrompt
            _client.ExecuteAndWait(async() =>
            {
                string localToken = null;
                StreamReader file = null;

                try
                {
                    file = File.OpenText(AppDomain.CurrentDomain.BaseDirectory + "token.tok");
                }
                catch (Exception)
                {
                    Console.WriteLine("! -> File not found or inacessible.");
                    goto Start;
                }

                string json = file?.ReadToEnd();
                localToken  = json;
                file?.Close();
                #region kekkle

                /* catch (Exception)
                 *         //{
                 *
                 *         //    if (!(file == null)) // If the file is here
                 *         //        file.Close(); // close it xd
                 *
                 *         //    Console.WriteLine("Please, insert the token here");
                 *         //    Console.Out.Flush();
                 *         //    string token = await GetInputAsync();
                 *         //    using (StreamWriter Tempfile = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "token.json"))
                 *         //    {
                 *
                 *         //        Tempfile.WriteLine(Json.Encode(token));
                 *         //        Tempfile.Close();
                 *         //    }
                 *         //    localToken = token;
                 *         //} */
                #endregion
                Start:
                try
                {
                    Console.WriteLine($"Alright : {Environment.NewLine} da token is {localToken ?? "null :("}");
                    await _client.Connect(localToken, TokenType.Bot);
                }
                catch (Exception)
                {
                    await Task.Delay(3000);
                    file?.Close();        // close it xd

                    Console.WriteLine("Please, insert the token here");
                    Console.Out.Flush();
                    string token = await GetInputAsync();
                    using (StreamWriter Tempfile = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "token.json"))
                    {
                        Tempfile.WriteLine(Json.Encode(token));
                        Tempfile.Close();
                    }
                    localToken = token;
                    goto Start;
                }

                SendConsole("Connected!");
                _client.SetGame(new Game($"Keking everyone ! {version}"));
            });
            #endregion
        }