Exemple #1
0
        public async Task <bool> JoinCmd(CommandMessage message)
        {
            if (this.musicPlayer != null)
            {
                IVoiceState voiceState = message.Author as IVoiceState;
                if (voiceState.VoiceChannel != null)
                {
                    return(await this.musicPlayer.JoinAudio(message.Guild, voiceState.VoiceChannel));
                }
                else
                {
                    Discord.Rest.RestUserMessage responseMessage = await message.Channel.SendMessageAsync("You need to join a voice channel first, _kupo!_", messageReference : message.MessageReference);

                    await Task.Delay(3000);

                    // Delete response command
                    await responseMessage.DeleteAsync();
                }
            }

            // Delete calling command
            await message.Message.DeleteAsync();

            return(false);
        }
Exemple #2
0
        public async Task <EmbedBuilder> EvaluateAsync(ShardedCommandContext context, string code)
        {
            Discord.Rest.RestUserMessage msg = await context.Channel.SendMessageAsync("Working...");

            code = code.Replace("“", "\"").Replace("‘", "\'").Replace("”", "\"").Replace("’", "\'").Trim('`');
            if (code.Length > 2 && code.Substring(0, 2) == "cs")
            {
                code = code.Substring(2);
            }

            IEnumerable <Assembly> assemblies = GetAssemblies();

            var sb = new StringBuilder();

            var globals = new Globals
            {
                _client   = _client,
                _context  = context,
                _guild    = context.Guild,
                _channel  = context.Channel,
                _user     = context.User as SocketGuildUser,
                _services = _services,
                _message  = context.Message,
                Console   = new FakeConsole(sb),
                _db       = _services.GetService <DbService>(),
                _misc     = this,
                _audio    = _services.GetService <AudioService>(),
                Random    = _random,
                _img      = _services.GetService <ImageService>(),
                _ms       = _services.GetService <MusicService>(),
                _commands = _services.GetService <CommandService>()
            };
            var options = ScriptOptions.Default
                          .AddReferences(assemblies)
                          .AddImports(globals.Imports)
                          .WithAllowUnsafe(true)
                          .WithLanguageVersion(LanguageVersion.CSharp8);

            Stopwatch s       = Stopwatch.StartNew();
            var       script  = CSharpScript.Create(code, options, typeof(Globals));
            var       compile = script.GetCompilation().GetDiagnostics();
            var       cErrors = compile.Where(x => x.Severity == DiagnosticSeverity.Error);

            s.Stop();

            if (cErrors.Any())
            {
                await msg.DeleteAsync();

                return(await GenerateErrorAsync(code, cErrors));
            }

            /*
             * if (code.WillExit(out string message))
             * {
             *  var ex = new CodeExitException(message, new Exception(message));
             *  await msg.DeleteAsync();
             *  return await GenerateErrorAsync(code, ex);
             * }
             */

            Stopwatch            c = Stopwatch.StartNew();
            ScriptState <object> eval;

            try
            {
                eval = await script.RunAsync(globals);
            }
            catch (Exception e)
            {
                await msg.DeleteAsync();

                return(await GenerateErrorAsync(code, e));
            }
            c.Stop();

            var result = eval.ReturnValue;

            if (eval.Exception != null)
            {
                await msg.DeleteAsync();

                return(await GenerateErrorAsync(code, eval.Exception));
            }

            string description;

            if (code.Length < 1000)
            {
                description = $"in: ```cs\n{code}```\nout: \n";
            }
            else
            {
                description = $"in: **[input]({await UploadToBisogaAsync(code)})**\nout: \n";
            }
            string tostringed = result == null ? " " : result.ToString();

            if (result is ICollection r)
            {
                tostringed = r.MakeString();
            }
            else if (result is IReadOnlyCollection <object> x)
            {
                tostringed = x.MakeString();
            }
            else if (result is string str)
            {
                tostringed = str;
            }
            else
            {
                tostringed = result.MakeString();
            }

            if (tostringed.Length > 1000)
            {
                description += $"Here is a **[link]({await UploadToBisogaAsync(tostringed)})** to the result.";
            }
            else
            {
                description += $"```{tostringed}```";
            }

            if (sb.ToString().Length > 0)
            {
                description += $"\nConsole: \n```\n{sb}\n```";
            }

            string footer = "";

            if (result is ICollection coll)
            {
                footer += $"Collection has {coll.Count} members • ";
            }
            else if (result is IReadOnlyCollection <object> colle)
            {
                footer += $"Collection has {colle.Count} members • ";
            }

            footer += $"Return type: {(result == null ? "null" : result.GetType().ToString())} • took {s.ElapsedTicks / 10000d} ms to compile and {c.ElapsedTicks / 10000d} ms to execute";


            var em = new EmbedBuilder()
                     .WithFooter(footer)
                     .WithDescription(description)
                     .WithColor(Color.Green);

            await msg.DeleteAsync();

            return(em);
        }