Beispiel #1
0
        public Message ActivityToMessage(Activity activity, string idUser)
        {
            var message = new Message();

            message.Id     = activity.Id;
            message.IdUser = idUser;
            //Текст
            if (activity.Text != null)
            {
                message.Text = activity.Text;
            }
            //Дата
            if (activity.Timestamp != null)
            {
                message.Date = (DateTime)activity.Timestamp;
            }
            //Мессенджер
            message.Messanger = activity.ChannelId;
            //Вложения
            if (activity.Attachments != null)
            {
                message.Attachments = new List <Message.Attachment>();
                foreach (var activityAttachment in activity.Attachments)
                {
                    var attachment = new Message.Attachment();
                    attachment.ContentType = activityAttachment.ContentType;
                    attachment.ContentUrl  = activityAttachment.ContentUrl;
                    attachment.Name        = activityAttachment.Name;
                }
            }

            return(message);
        }
Beispiel #2
0
        public void Start()
        {
            client = new DiscordClient();
            mainLogger = new Logger(Helper.ParseEnum<LogLevel>(ConfigurationManager.AppSettings["LogLevel"]), ConfigurationManager.AppSettings["LogFile"]);
            processList = new Dictionary<string, Process>();

            client.UsingCommands(x =>
                {
                    x.PrefixChar = Convert.ToChar(ConfigurationManager.AppSettings["Prefix"]);
                    x.HelpMode = HelpMode.Public;
                });

            client.GetService<CommandService>().CreateCommand("version")
                .Alias(new string[] { "v" })
                .Description("Shows setlX version.")
                .Do(e =>
                {
                    try
                    {
                        mainLogger.Debug(e.User.Name, "Showed setlX version");
                        ExecuteSetlX("--version", e, "setlX", processList);
                    }
                    catch (Exception ex)
                    {
                        Error(e, "command version |", ex.Message);
                    }
                });

            client.GetService<CommandService>().CreateCommand("setlx")
                .Alias(new string[] { "s", "exec" })
                .Description("Starts interactive mode or executes the first attached setlx file. 'arg' can be: 'code'... ")
                .Parameter("arg", ParameterType.Optional)
                .Do(e =>
                {
                    try

                        if (processList.ContainsKey(e.Channel.Name) && !processList[e.Channel.Name].HasExited)
                        {
                            e.Channel.SendMessage("There is already a setlX process");
                        }
                        else
                        {
                            if (e.Message.Attachments.Count() > 0) // File Mode
                            {
                                Message.Attachment setlxFile = e.Message.Attachments.First();
                                if (setlxFile.Filename.ToLower().EndsWith("stlx"))
                                {
                                    string setlxFilePath = Path.Combine(@"X:\temp", setlxFile.Filename);
                                    FileInfo fi = new FileInfo(setlxFilePath);
                                    if (fi.Exists)
                                    {
                                        mainLogger.Debug(e.User.Name, setlxFilePath, "deleted");
                                        fi.Delete();
                                    }
                                    using (var client = new WebClient())
                                    {
                                        client.DownloadFile(setlxFile.Url, setlxFilePath);
                                        mainLogger.Debug(e.User.Name, setlxFilePath, "downloaded");
                                    }

                                    if (!String.IsNullOrEmpty(e.GetArg("arg")))
                                    {
                                        string arg = e.GetArg("arg");
                                        switch (arg)
                                        {
                                            case "code":
                                                Print(e, File.ReadAllText(setlxFilePath), setlxFile.Filename, "Code");
                                                mainLogger.Debug(e.User.Name, "Code of ", setlxFilePath, "was printed");
                                                break;
                                        }
                                    }

                                    ExecuteSetlX(setlxFilePath, e, setlxFile.Filename, processList);

                                }
                                else
                                {
                                    e.Channel.SendMessage("There is no .stlx file in the attachments...");
                                    mainLogger.Debug(e.User.Name, "There is no .stlx file in the attachments...");
                                }
                            }
                            else //Interactive Mode
                            {
                                ExecuteSetlX(String.Empty, e, "Interactive Mode", processList);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Error(e, "command setlx |", ex.Message);
                    }
                });


            client.GetService<CommandService>().CreateCommand("input")
                .Alias(new string[] { "i" })
                .Description("Writes string in StandartInput of setlX process in your current channel.")
                .Parameter("input", ParameterType.Required)
                .Do(e =>
                {
                    try
                    {
                        if (processList.ContainsKey(e.Channel.Name) && !processList[e.Channel.Name].HasExited)
                        {
                            string input = e.GetArg("input");
                            mainLogger.Debug(e.Message.User.Name, input, "was written in", e.Message.Channel.Name);

                            StreamWriter sw = processList[e.Channel.Name].StandardInput;
                            sw.WriteLine(input);
                        }
                        else
                        {
                            List<string> processDeleteList = new List<string>();
                            foreach (KeyValuePair<string, Process> proc in processList)
                            {
                                if (proc.Value.HasExited)
                                {
                                    processDeleteList.Add(proc.Key);
                                }
                            }
                            processDeleteList.ForEach(x => processList.Remove(x));

                            e.Channel.SendMessage("There is no setlx process in this channel!");
                        }
                    }
                    catch (Exception ex)
                    {
                        Error(e, "command input |", ex.Message);
                    }
                });

            client.GetService<CommandService>().CreateCommand("kill")
                .Alias(new string[] { "k", "exit", "e" })
                .Description("Kills the setlX-process your current channel.")
                .Do(e =>
                {
                    try
                    {
                        processList[e.Channel.Name].Kill();
                        processList.Remove(e.Channel.Name);
                    }
                    catch (Exception ex)
                    {
                        Error(e, "command input |", ex.Message);
                    }
                });

            client.GetService<CommandService>().CreateCommand("list")
                .Alias(new string[] { "l" })
                .Description("Shows you the process list")
                .Hide()
                .Do(e =>
                {
                    try
                    {
                        if (processList != null && processList.Count > 0)
                        {
                            StringBuilder sb = new StringBuilder();

                            foreach (KeyValuePair<string, Process> proc in processList)
                            {
                                sb.Append(proc.Key);
                                sb.Append(" - ");
                                sb.Append(proc.Value.Id);
                                sb.AppendLine();
                            }

                            e.Channel.SendMessage(sb.ToString());
                        }
                        else
                        {
                            e.Channel.SendMessage("There are no setlx processes.");
                        }
                    }
                    catch (Exception ex)
                    {
                        Error(e, "command input |", ex.Message);
                    }
                });

            client.ExecuteAndWait(async () =>
            {
                await client.Connect(ConfigurationManager.AppSettings["BotToken"], TokenType.Bot);
                client.SetGame("C#");

                mainLogger.Info("System", "setlXBot is up!");
            });
        }