/// <summary> /// If you screwed up, you can use this method to edit a given message. This sends out an http patch request with a replacement message /// </summary> /// <param name="MessageID">The ID of the message you want to edit.</param> /// <param name="replacementMessage">What you want the text to be edited to.</param> /// <param name="channel">The channel the message is in</param> /// <returns>the new and improved DiscordMessage object.</returns> public DiscordMessage EditMessage(string MessageID, string replacementMessage, DiscordChannel channel) { string url = Endpoints.BaseAPI + Endpoints.Channels + $"/{channel.ID}" + Endpoints.Messages + $"/{MessageID}"; try { string replacement = JsonConvert.SerializeObject( new { content = replacementMessage, mentions = new string[0] } ); JObject result = JObject.Parse(WebWrapper.Patch(url, token, replacement)); DiscordMessage m = new DiscordMessage { RawJson = result, Attachments = result["attachments"].ToObject<DiscordAttachment[]>(), Author = channel.Parent.GetMemberByKey(result["author"]["id"].ToString()), TypeOfChannelObject = channel.GetType(), channel = channel, Content = result["content"].ToString(), ID = result["id"].ToString(), timestamp = result["timestamp"].ToObject<DateTime>() }; return m; } catch (Exception ex) { DebugLogger.Log("Exception ocurred while editing: " + ex.Message, MessageLevel.Error); } return null; }
/// <summary> /// Sends a message to a channel, what else did you expect? /// </summary> /// <param name="message">The text to send</param> /// <param name="channel">DiscordChannel object to send the message to.</param> /// <returns>A DiscordMessage object of the message sent to Discord.</returns> public DiscordMessage SendMessageToChannel(string message, DiscordChannel channel) { string url = Endpoints.BaseAPI + Endpoints.Channels + $"/{channel.ID}" + Endpoints.Messages; try { JObject result = JObject.Parse(WebWrapper.Post(url, token, JsonConvert.SerializeObject(Utils.GenerateMessage(message)))); if (result["content"].IsNullOrEmpty()) throw new InvalidOperationException("Request returned a blank message, you may not have permission to send messages yet!"); DiscordMessage m = new DiscordMessage { ID = result["id"].ToString(), Attachments = result["attachments"].ToObject<DiscordAttachment[]>(), Author = channel.Parent.GetMemberByKey(result["author"]["id"].ToString()), channel = channel, TypeOfChannelObject = channel.GetType(), Content = result["content"].ToString(), RawJson = result, timestamp = result["timestamp"].ToObject<DateTime>() }; return m; } catch (Exception ex) { DebugLogger.Log($"Error ocurred while sending message to channel ({channel.Name}): {ex.Message}", MessageLevel.Error); } return null; }
/// <summary> /// Returns a List of DiscordMessages. /// </summary> /// <param name="channel">The channel to return them from.</param> /// <param name="count">How many to return</param> /// <param name="idBefore">Messages before this message ID.</param> /// <param name="idAfter">Messages after this message ID.</param> /// <returns></returns> public List<DiscordMessage> GetMessageHistory(DiscordChannel channel, int count, int? idBefore, int ?idAfter) { string request = "https://discordapp.com/api/channels/" + channel.ID + $"/messages?&limit={count}"; if (idBefore != null) request += $"&before={idBefore}"; if (idAfter != null) request += $"&after={idAfter}"; JArray result = null; try { string res = WebWrapper.Get(request, token); result = JArray.Parse(res); } catch(Exception ex) { DebugLogger.Log($"Error ocurred while getting message history for channel {channel.Name}: {ex.Message}", MessageLevel.Error); } if(result != null) { List<DiscordMessage> messageList = new List<DiscordMessage>(); /// NOTE /// For some reason, the d object is excluded from this. foreach (var item in result.Children()) { messageList.Add(new DiscordMessage { id = item["id"].ToString(), channel = channel, attachments = item["attachments"].ToObject<DiscordAttachment[]>(), TypeOfChannelObject = channel.GetType(), author = GetMemberFromChannel(channel, item["author"]["id"].ToString()), content = item["content"].ToString(), RawJson = item.ToObject<JObject>(), timestamp = DateTime.Parse(item["timestamp"].ToString()) }); } return messageList; } return null; }