コード例 #1
0
ファイル: Program.cs プロジェクト: Verlio-H/URCL.NET
 static void Main(string[] args)
 {
     if (File.Exists("URCL.NET.exe") && File.Exists("token.txt"))
     {
         Console.WriteLine("Press any key to abort...");
         var urcl = new UrclInterface("URCL.NET.exe", 11113);
         urcl.Configuration.Add("Emulate");
         urcl.Configuration.Add("DisableBreak");
         var bot = new Bot(urcl, Console.WriteLine, File.ReadAllText("token.txt"));
         bot.Start();
         new AutoResetEvent(false).WaitOne();
     }
     else
     {
         Console.WriteLine("Missing required files. (URCL.NET.exe and token.txt)");
     }
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: LucidaDragon/URCL.NET
        static void Main(string[] args)
        {
            if (File.Exists("URCL.NET.exe") && File.Exists("token.txt"))
            {
                ulong?owner = null;

                if (File.Exists("owner.txt") && ulong.TryParse(File.ReadAllText("owner.txt"), out ulong v))
                {
                    owner = v;
                }

                var urcl = new UrclInterface("URCL.NET.exe", 11113);
                urcl.Configuration.Add("Emulate");
                urcl.Configuration.Add("DisableBreak");
                var bot = new Bot(urcl, Console.WriteLine, File.ReadAllText("token.txt"));
                bot.Start();
                new AutoResetEvent(false).WaitOne();
            }
            else
            {
                Console.WriteLine("Missing required files. (URCL.NET.exe and token.txt)");
            }
        }
コード例 #3
0
        public Bot(UrclInterface urcl, Action <string> output, string token)
        {
            Urcl  = urcl;
            Token = token;

            Worker = new Thread(async() =>
            {
                while (true)
                {
                    Sleep.WaitOne();

                    if (Quit)
                    {
                        break;
                    }

                    while (Jobs.TryDequeue(out Tuple <SocketMessage, Attachment> job))
                    {
                        var m      = job.Item1;
                        var attach = job.Item2;

                        using var fetch = new WebClient();
                        var content     = await fetch.DownloadStringTaskAsync(attach.Url);

                        var buffer = new List <string>();

                        await Urcl.SubmitJob(content, buffer.Add);

                        Reply(m, $"Result of \"{attach.Filename}\":{Environment.NewLine}{string.Join(Environment.NewLine, buffer)}");
                    }
                }
            });
            Worker.Start();

            Client = new DiscordSocketClient();

            Client.Log += (m) =>
            {
                output($"[\"{m.Severity}\"] {m.Message} {m.Exception}");

                return(Task.CompletedTask);
            };

            Client.MessageReceived += (m) =>
            {
                foreach (var user in m.MentionedUsers)
                {
                    if (user.IsBot && user.Id == Client.CurrentUser.Id)
                    {
                        foreach (var attach in m.Attachments)
                        {
                            if (attach.Filename.ToLower().EndsWith("urcl"))
                            {
                                if (attach.Size <= ushort.MaxValue)
                                {
                                    Reply(m, $"\"{attach.Filename}\" is now in queue.");

                                    Jobs.Enqueue(new Tuple <SocketMessage, Attachment>(m, attach));
                                    Sleep.Set();
                                }
                                else
                                {
                                    Reply(m, $"Attached file \"{attach.Filename}\" is too large. (Must be {ushort.MaxValue} bytes or less)");
                                }
                            }
                        }
                    }
                }

                return(Task.CompletedTask);
            };

            Client.Disconnected += async(e) =>
            {
                await Client.LoginAsync(TokenType.Bot, Token);

                await Client.StartAsync();
            };
        }
コード例 #4
0
ファイル: Bot.cs プロジェクト: LucidaDragon/URCL.NET
        public Bot(UrclInterface urcl, Action <string> output, string token, ulong?owner = null)
        {
            Urcl  = urcl;
            Token = token;
            Owner = owner;

            Worker = new Thread(async() =>
            {
                WaitForConnect.WaitOne();

                while (true)
                {
                    output($"{DateTime.Now} Idle");
                    await Client.SetGameAsync("Conway's Game of Life.", null, ActivityType.Playing);
                    await Client.SetStatusAsync(UserStatus.Idle);

                    Sleep.WaitOne();

                    if (Quit)
                    {
                        break;
                    }

                    while (Jobs.TryDequeue(out BotTask job))
                    {
                        output($"{DateTime.Now} Active");
                        await Client.SetStatusAsync(UserStatus.Online);

                        try
                        {
                            var buffer = new List <string>();

                            await Urcl.SubmitJob(await job.GetContent(), job.Language, job.OutputType, job.Tier, buffer.Add);

                            Reply(job.Source, $"Result of \"{job.Name}\":{Environment.NewLine}{string.Join(Environment.NewLine, buffer)}");
                        }
                        catch (Exception ex)
                        {
                            Reply(job.Source, $"API Error: {ex.Message}");
                        }
                    }
                }
            });

            Client = new DiscordSocketClient();

            Client.Log += (m) =>
            {
                output($"{DateTime.Now} {m.Message} {m.Exception}");

                return(Task.CompletedTask);
            };

            Client.Ready += () =>
            {
                WaitForConnect.Set();
                return(Task.CompletedTask);
            };

            Client.MessageReceived += (m) =>
            {
                if (!Self.HasValue && Client.CurrentUser != null)
                {
                    Self = Client.CurrentUser.Id;
                }

                if (Self.HasValue)
                {
                    foreach (var user in m.MentionedUsers)
                    {
                        if (user.IsBot && user.Id == Self.Value)
                        {
                            var foundFile = false;

                            foreach (var attach in m.Attachments)
                            {
                                if (attach.Filename.ToLower().EndsWith("urcl"))
                                {
                                    foundFile = true;

                                    if (attach.Size <= ushort.MaxValue)
                                    {
                                        Reply(m, $"\"{attach.Filename}\" is now in queue.");

                                        Jobs.Enqueue(new BotTask(m, attach));
                                        Sleep.Set();
                                    }
                                    else
                                    {
                                        Reply(m, $"Attached file \"{attach.Filename}\" is too large. (Must be {ushort.MaxValue} bytes or less)");
                                    }
                                }
                            }

                            if (!foundFile)
                            {
                                var match = Regex.Match(m.Content, @"([\w]+)?\s*([\w]+)?\s*([\w]+)?\s*(```((.|\n)*)```)");

                                if (match.Success)
                                {
                                    var lang       = match.Groups[1].Success ? match.Groups[1].Value : "urcl";
                                    var outputType = match.Groups[2].Success ? match.Groups[2].Value : "emulate";
                                    var tier       = match.Groups[3].Success ? match.Groups[3].Value : "any";

                                    Jobs.Enqueue(new BotTask(m, lang, outputType, tier, match.Groups[5].Value));
                                    Sleep.Set();
                                }
                                else
                                {
                                    Reply(m, "Code block was not specified.");
                                }
                            }
                        }
                    }
                }
                else
                {
                    output("Failed to obtain self ID.");
                }

                return(Task.CompletedTask);
            };

            Client.Disconnected += (e) =>
            {
                new Thread(async() =>
                {
                    await Client.LoginAsync(TokenType.Bot, Token);
                    await Client.StartAsync();
                }).Start();

                return(Task.CompletedTask);
            };
        }