예제 #1
0
 // I don't know the best way to do this, I'm limited by my lack of C# knowledge and how I understand the deserializer to work
 private static List <Types.EmbedArray> BuildEmbedList(Types.EmbedsArray input)
 {
     return(new List <Types.EmbedArray>()
     {
         input.embed1,
         input.embed2,
         input.embed3,
         input.embed4,
         input.embed5,
         input.embed6,
         input.embed7,
         input.embed8,
         input.embed9,
         input.embed10
     });
 }
예제 #2
0
        internal static async Task HandleRequest(string[] args)
        {
            try
            {
                // Remove arma quotations
                string            url      = args[0].Trim('"');
                string            content  = args[1].Trim('"').Replace("\"\"", "\"");
                string            username = args[2].Trim('"');
                string            avatar   = args[3].Trim('"');
                bool              tts      = Convert.ToBoolean(args[4]);
                Types.EmbedsArray embeds   = Converter.DeserializeObject <Types.EmbedsArray>(args[5]);

                // Discord 2000 character limit
                if (content.Length > 1999)
                {
                    content = content.Substring(0, 1999);
                }

                // Bare bones
                JObject package = new JObject(
                    new JProperty("content", content),
                    new JProperty("username", username),
                    new JProperty("avatar_url", avatar),
                    new JProperty("tts", tts)
                    );

                // Build embeds array
                List <Types.EmbedArray> embedList = BuildEmbedList(embeds);
                JArray embedProperty = new JArray();
                for (int i = 0; i < 10; i++)
                {
                    Types.EmbedArray embed = embedList.ElementAt(i);
                    if (embed == null)
                    {
                        break;
                    }
                    JObject embedObject = BuildEmbedObject(embed);
                    if (embedObject.Count > 0)
                    {
                        embedProperty.Add(embedObject);
                    }
                }
                if (embedProperty.Count() > 0)
                {
                    package.Add(new JProperty("embeds", embedProperty));
                }

                // Execute webhook
                using (HttpClient APIClient = new HttpClient())
                {
                    APIClient.BaseAddress = new Uri("https://discordapp.com/api/webhooks/");
                    APIClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await APIClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(package), Encoding.UTF8, "application/json"));

                    await Tools.LogAsyncReply(response.Content);
                }
            }
            catch (Exception e)
            {
                Tools.Logger(e);
            }
        }