public async void SendMessageToDiscordChannel(ulong channelId, string message)
        {
            message = message.Replace("\\", "\\\\");
            message = message.Replace("\"", "\\\"");
            message = message.Replace("\n", "\\n");
            string json = DiscordMessageFactory.CreateTextMessage(message);

            string response = await SimpleRequest.SendJsonDataAsync($"{API_URL}/channels/{channelId}/messages",
                                                                    new WebHeaderCollection()
            {
                { "Authorization", $"Bot {BOT_TOKEN}" }
            }, json);

            if (debug)
            {
                Console.WriteLine(response);
            }
        }
Beispiel #2
0
        public async void SendMessageToDiscordChannel(string message, ulong channelId)
        {
            message = message.Replace("\\", "\\\\");
            message = message.Replace("\"", "\\\"");
            message = message.Replace("\n", "\\n");
            string json = DiscordMessageFactory.CreateTextMessage(message);

            string response = null;

            try
            {
                response = await SimpleRequest.SendJsonDataAsync($"{API_URL}/channels/{channelId}/messages",
                                                                 new WebHeaderCollection()
                {
                    { "Authorization", $"Bot {BOT_TOKEN}" }
                }, json);
            }
            catch (Exception e)
            {
                if (e.Message.Contains("(401) Unauthorized"))
                {
                    PrettyPrint.Log("Discord", "Unauthorized access to Discord server. Is your BOT_TOKEN correct?", ConsoleColor.Red);
                }
                else if (e.Message.Contains("(403) Forbidden"))
                {
                    PrettyPrint.Log("Discord", "Forbidden access to Discord channel. Are your Channel IDs & BOT permissions correct?", ConsoleColor.Red);
                }
                else
                {
                    PrettyPrint.Log("Discord", e.Message, ConsoleColor.Red);
                }
            }

            if (debug)
            {
                Console.WriteLine(response);
            }
        }