Archived
1
0
Fork 0
This repository has been archived on 2024-05-21. You can view files and clone it, but cannot push or open issues or pull requests.
maki/Maki/DiscordMessage.cs

90 lines
3.1 KiB
C#

using Maki.Rest;
using Maki.Structures.Messages;
using Maki.Structures.Rest;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Maki
{
public class DiscordMessage
{
public readonly ulong Id;
public readonly DateTime Sent;
public readonly DiscordMember Member;
public readonly DiscordUser User;
public readonly DiscordChannel Channel;
private readonly Discord client;
public string Text { get; internal set; }
public DateTime Edited { get; internal set; }
public IEnumerable<DiscordMember> MentionsUsers { get; internal set; }
public IEnumerable<DiscordRole> MentionsRoles { get; internal set; }
public bool MentionsEveryone { get; internal set; }
public bool IsPinned { get; internal set; }
public bool MentionsMe(bool everyone = false, bool roles = true) =>
(everyone && MentionsEveryone)
|| (Channel.Type != DiscordChannelType.Private && roles && client.MemberManager.Id(Channel.Server, client.Me)?.Roles.Where(x => MentionsRoles.Contains(x)).Count() > 0)
|| MentionsUsers.Select(x => x.User).Contains(client.Me);
public bool IsMe => client.Me == User;
public DiscordServer Server => Channel.Server;
internal DiscordMessage(Discord discord, Message msg, DiscordChannel channel, DiscordMember member = null)
{
client = discord;
Id = msg.Id;
Sent = msg.Sent;
Text = msg.Content;
if (member != null)
{
Member = member;
User = member.User;
} else
{
member = null;
User = client.UserManager.Id(msg.User.Id);
Debug.Assert(User != null, "user is null");
}
Channel = channel;
MentionsUsers = client.MemberManager.Server(channel.Server).Where(x => msg.Mentions.Select(y => y.Id).Contains(x.User.Id));
MentionsRoles = client.RoleManager .Server(channel.Server).Where(x => msg.MentionsRoles.Contains(x.Id));
MentionsEveryone = msg.MentioningEveryone;
IsPinned = msg.IsPinned == true;
}
public DiscordMessage Edit(string text = null, DiscordEmbed embed = null)
{
using (WebRequest wr = new WebRequest(HttpMethod.PATCH, RestEndpoints.ChannelMessage(Channel.Id, Id)))
{
wr.AddJson(new MessageEdit
{
Text = text,
Embed = embed?.ToStruct(),
});
wr.Perform();
if (wr.Status != 200)
return this;
Message msg = wr.ResponseJson<Message>();
Text = msg.Content;
Edited = msg.Edited ?? DateTime.UtcNow;
}
return this;
}
public void Delete()
{
using (WebRequest wr = new WebRequest(HttpMethod.DELETE, RestEndpoints.ChannelMessage(Channel.Id, Id)))
wr.Perform();
}
}
}