Archived
1
0
Fork 0

more handlers

This commit is contained in:
flash 2017-05-18 00:33:09 +02:00
parent b53af56fff
commit ec096e79c4
11 changed files with 162 additions and 83 deletions

View file

@ -182,7 +182,7 @@ namespace Maki
/// <summary> /// <summary>
/// Someone started typing, fired again after 8 seconds /// Someone started typing, fired again after 8 seconds
/// </summary> /// </summary>
public event Action<DiscordMember, DiscordChannel> OnTypingStart; public event Action<DiscordUser, DiscordChannel> OnTypingStart;
/// <summary> /// <summary>
/// Someone's status and/or game updated /// Someone's status and/or game updated
@ -383,24 +383,25 @@ namespace Maki
DiscordChannel chan = channels.Find(x => x.Id == channel.Id); DiscordChannel chan = channels.Find(x => x.Id == channel.Id);
chan.Name = channel.Name; chan.Name = channel.Name;
/*existing.Type = channel.Type;
existing.LastMessageId = channel.LastMessageId;
if (existing.Type == ChannelType.Private) if (chan.Type == DiscordChannelType.Private)
existing.Recipients = channel.Recipients;
else
{ {
existing.GuildId = channel.GuildId; // update recipients
existing.Topic = channel.Topic; } else
existing.PermissionOverwrites = channel.PermissionOverwrites; {
existing.Position = channel.Position; // update permissions too
chan.Position = channel.Position ?? 0;
} }
if (existing.Type == ChannelType.Voice) if (chan.Type == DiscordChannelType.Voice)
{ {
existing.Bitrate = channel.Bitrate; chan.Bitrate = channel.Bitrate ?? 0;
existing.UserLimit = channel.UserLimit; chan.UserLimit = channel.UserLimit ?? 0;
}*/ } else
{
chan.Topic = channel.Topic;
chan.LastMessageId = channel.LastMessageId ?? 0;
}
OnChannelUpdate?.Invoke(chan); OnChannelUpdate?.Invoke(chan);
} }
@ -408,27 +409,6 @@ namespace Maki
private void ShardManager_OnChannelDelete(GatewayShard shard, Channel channel) private void ShardManager_OnChannelDelete(GatewayShard shard, Channel channel)
{ {
DiscordChannel chan = channels.Find(x => x.Id == channel.Id); DiscordChannel chan = channels.Find(x => x.Id == channel.Id);
chan.Name = channel.Name;
/*chan.Type = channel.Type;
chan.LastMessageId = channel.LastMessageId;
if (chan.Type == ChannelType.Private)
chan.Recipients = channel.Recipients;
else
{
chan.GuildId = channel.GuildId;
chan.Topic = channel.Topic;
chan.PermissionOverwrites = channel.PermissionOverwrites;
chan.Position = channel.Position;
}
if (chan.Type == ChannelType.Voice)
{
chan.Bitrate = channel.Bitrate;
chan.UserLimit = channel.UserLimit;
}*/
channels.Remove(chan); channels.Remove(chan);
OnChannelDelete?.Invoke(chan); OnChannelDelete?.Invoke(chan);
} }
@ -480,7 +460,6 @@ namespace Maki
} }
else else
{ {
// should this explode instead if the guild isn't unavailable?
server = servers.Find(x => x.Id == guild.Id); server = servers.Find(x => x.Id == guild.Id);
server.Name = guild.Name; server.Name = guild.Name;
} }
@ -595,11 +574,10 @@ namespace Maki
{ {
DiscordMember member = members.Find(x => x.User.Id == sMember.User.Id && x.Server.Id == sMember.GuildId); DiscordMember member = members.Find(x => x.User.Id == sMember.User.Id && x.Server.Id == sMember.GuildId);
/*existing.IsDeafened = member.IsDeafened;
existing.IsMuted = member.IsMuted;
existing.JoinedAt = member.JoinedAt;
existing.Roles = member.Roles;*/
member.Nickname = sMember.Nickname; member.Nickname = sMember.Nickname;
member.IsDeaf = sMember.IsDeafened == true;
member.IsMute = sMember.IsMuted == true;
member.roles = new List<ulong>(sMember.Roles);
OnMemberUpdate?.Invoke(member); OnMemberUpdate?.Invoke(member);
} }
@ -624,14 +602,24 @@ namespace Maki
$"Permissions: {role.Role.Value.Permissions}"*/ $"Permissions: {role.Role.Value.Permissions}"*/
DiscordServer server = servers.Find(x => x.Id == sRole.Guild); DiscordServer server = servers.Find(x => x.Id == sRole.Guild);
DiscordRole role = new DiscordRole(this, sRole.Role.Value, server); DiscordRole role = roles.Where(x => x.Id == sRole.Role.Value.Id).FirstOrDefault();
if (role == default(DiscordRole))
{
role = new DiscordRole(this, sRole.Role.Value, server);
roles.Add(role); roles.Add(role);
} else
{
role.Colour.Raw = sRole.Role.Value.Colour.Value;
}
OnRoleCreate?.Invoke(role); OnRoleCreate?.Invoke(role);
} }
private void ShardManager_OnGuildRoleDelete(GatewayShard shard, GuildRole sRole) private void ShardManager_OnGuildRoleDelete(GatewayShard shard, GuildRole sRole)
{ {
DiscordRole role = roles.Find(x => x.Id == sRole.RoleId && x.Server.Id == sRole.Guild); DiscordRole role = roles.Find(x => x.Id == sRole.RoleId && x.Server.Id == sRole.Guild);
members.Where(x => x.roles.Contains(role.Id)).ToList().ForEach(x => x.roles.RemoveAll(y => y == role.Id));
roles.Remove(role); roles.Remove(role);
OnRoleDelete?.Invoke(role); OnRoleDelete?.Invoke(role);
} }
@ -663,9 +651,6 @@ namespace Maki
private void ShardManager_OnGuildMembersChunk(GatewayShard shard, GuildMembersChunk membersChunk) private void ShardManager_OnGuildMembersChunk(GatewayShard shard, GuildMembersChunk membersChunk)
{ {
/*$"Guild Id: {membersChunk.Guild}",
$"Members {string.Join(", ", membersChunk.Members.Select(x => x.User.Id).ToArray())}"*/
for (int i = 0; i < membersChunk.Members.Length; i++) for (int i = 0; i < membersChunk.Members.Length; i++)
{ {
GuildMember member = membersChunk.Members[i]; GuildMember member = membersChunk.Members[i];
@ -692,59 +677,88 @@ namespace Maki
private void ShardManager_OnMessageUpdate(GatewayShard shard, Message message) private void ShardManager_OnMessageUpdate(GatewayShard shard, Message message)
{ {
DiscordMessage msg = messages.Find(x => x.Id == message.Id); DiscordMessage msg = messages.Where(x => x.Id == message.Id).FirstOrDefault();
if (msg == null)
{
RestResponse<Message> getMsg = RestClient.Request<Message>(RestRequestMethod.GET, RestEndpoints.ChannelMessage(message.ChannelId, message.Id));
DiscordChannel channel = channels.Where(x => x.Id == getMsg.Response.ChannelId).FirstOrDefault();
DiscordMember member = members.Where(x => x.User.Id == getMsg.Response.User.Id && (channel.Server == null || channel.Server == x.Server)).FirstOrDefault();
msg = new DiscordMessage(this, getMsg.Response, member, channel);
messages.Add(msg);
}
msg.Edited = DateTime.Now; msg.Edited = DateTime.Now;
if (!string.IsNullOrEmpty(message.Content)) if (!string.IsNullOrEmpty(message.Content))
msg.Text = message.Content; msg.Text = message.Content;
/*existing.IsTTS = message.IsTTS == true; msg.IsPinned = message.IsPinned == true;
existing.IsPinned = message.IsPinned == true;*/
OnMessageUpdate?.Invoke(msg); OnMessageUpdate?.Invoke(msg);
} }
// TODO: account for DMs
private void ShardManager_OnTypingStart(GatewayShard shard, TypingStart typing) private void ShardManager_OnTypingStart(GatewayShard shard, TypingStart typing)
{ {
DiscordChannel channel = channels.Find(x => x.Id == typing.Channel); DiscordChannel channel = channels.Find(x => x.Id == typing.Channel);
DiscordMember member = members.Find(x => x.User.Id == typing.User && x.Server.Id == channel.Server.Id); DiscordUser user = users.Find(x => x.Id == typing.User);
OnTypingStart?.Invoke(member, channel); OnTypingStart?.Invoke(user, channel);
} }
private void ShardManager_OnPresenceUpdate(GatewayShard shard, Presence presence) private void ShardManager_OnPresenceUpdate(GatewayShard shard, Presence presence)
{ {
/*$"User Id: {presence.User.Id}",
$"Roles: " + (presence.Roles != null ? string.Join(", ", presence.Roles) : string.Empty),
"Game: " + (presence.Game.HasValue ? presence.Game.Value.Name : string.Empty),
$"Guild Id: {presence.Guild}",
$"Status: {presence.Status}"*/
DiscordMember member = members.Find(x => x.User.Id == presence.User.Id && x.Server.Id == presence.Guild); DiscordMember member = members.Find(x => x.User.Id == presence.User.Id && x.Server.Id == presence.Guild);
// update user presence member.User.Game = presence.Game.HasValue ? new DiscordGame(presence.Game.Value) : null;
if (presence.Roles != null)
member.roles = new List<ulong>(presence.Roles);
switch (presence.Status.ToLower())
{
case @"online":
member.User.Status = DiscordUserStatus.Online;
break;
case @"away":
member.User.Status = DiscordUserStatus.Away;
break;
case @"dnd":
member.User.Status = DiscordUserStatus.DoNotDisturb;
break;
case @"offline":
default:
member.User.Status = DiscordUserStatus.Offline;
break;
}
OnPresenceUpdate?.Invoke(member); OnPresenceUpdate?.Invoke(member);
} }
private void ShardManager_OnReady(GatewayShard shard, GatewayReady ready) private void ShardManager_OnReady(GatewayShard shard, GatewayReady ready)
{ {
foreach (Channel chan in ready.PrivateChannels)
{
DiscordChannel channel = new DiscordChannel(this, chan);
// this shouldn't ever happen but just in case
if (channels.Where(x => x.Id == channel.Id).Count() > 0)
continue;
channels.Add(channel);
}
foreach (Guild guild in ready.UnavailableGuilds) foreach (Guild guild in ready.UnavailableGuilds)
// should this call an event handler? {
ShardManager_OnGuildCreate(shard, guild); DiscordServer server = new DiscordServer(this, guild);
/* not ready for these yet if (servers.Where(x => x.Id == server.Id).Count() > 0)
foreach (Channel channel in ready.PrivateChannels) continue;
ShardManager_OnChannelCreate(shard, channel);*/
// keep track of self servers.Add(server);
}
/*$"Version: {ready.Version}",
$"User: {ready.User.Id}, {ready.User.Username}, {ready.User.EMail}",
$"Private Channels: {string.Join(", ", ready.PrivateChannels.Select(x => x.Id).ToArray())}",
$"Unavailable Guilds: {string.Join(", ", ready.UnavailableGuilds.Select(x => x.Id).ToArray())}",
$"Session: {ready.Session}"*/
DiscordUser user = new DiscordUser(this, ready.User); DiscordUser user = new DiscordUser(this, ready.User);
users.Add(user); users.Add(user);
@ -810,12 +824,12 @@ namespace Maki
#region IDisposable #region IDisposable
private bool IsDisposed = false; private bool isDisposed = false;
private void Dispose(bool disposing) private void Dispose(bool disposing)
{ {
if (!IsDisposed) { if (!isDisposed) {
IsDisposed = true; isDisposed = true;
Disconnect(); Disconnect();
} }
} }

View file

@ -14,12 +14,27 @@ namespace Maki
public string Name { get; internal set; } public string Name { get; internal set; }
public DiscordChannelType Type { get; internal set; } public DiscordChannelType Type { get; internal set; }
internal DiscordChannel(Discord discord, Channel channel, DiscordServer server) public int Position { get; internal set; }
public string Topic { get; internal set; }
public ulong LastMessageId { get; internal set; }
public int Bitrate { get; internal set; }
public int UserLimit { get; internal set; }
public override string ToString() => $@"<#{Id}>";
internal DiscordChannel(Discord discord, Channel channel, DiscordServer server = null)
{ {
client = discord; client = discord;
Id = channel.Id; Id = channel.Id;
Name = channel.Name; Name = channel.Name;
Type = (DiscordChannelType)channel.Type; Type = (DiscordChannelType)channel.Type;
Position = channel.Position ?? 0;
Topic = channel.Topic;
LastMessageId = channel.LastMessageId ?? 0;
Bitrate = channel.Bitrate ?? 0;
UserLimit = channel.UserLimit ?? 0;
Server = server; Server = server;
} }

21
Maki/DiscordGame.cs Normal file
View file

@ -0,0 +1,21 @@
using Maki.Structures.Presences;
using System;
namespace Maki
{
public class DiscordGame
{
public string Name { get; internal set; }
public bool IsStreaming { get; internal set; }
public Uri Url { get; internal set; }
internal DiscordGame(Game game)
{
Name = game.Name;
IsStreaming = game.Type == GameType.Streaming;
if (!string.IsNullOrEmpty(game.Url))
Url = new Uri(game.Url);
}
}
}

View file

@ -16,11 +16,15 @@ namespace Maki
public DateTime Joined { get; internal set; } public DateTime Joined { get; internal set; }
public string Nickname { get; internal set; } public string Nickname { get; internal set; }
public bool IsDeaf { get; internal set; }
public bool IsMute { get; internal set; }
public string Name => string.IsNullOrEmpty(Nickname) ? User.Username : Nickname; public string Name => string.IsNullOrEmpty(Nickname) ? User.Username : Nickname;
public string NameWithTag => $"{Name}#{User.Tag:0000}"; public string NameWithTag => $"{Name}#{User.Tag:0000}";
public override string ToString() => User.ToString();
private List<ulong> roles = new List<ulong>(); public override string ToString() => $@"<@!{User.Id}>";
internal List<ulong> roles = new List<ulong>();
internal DiscordMember(Discord discord, GuildMember member, DiscordUser user, DiscordServer server) internal DiscordMember(Discord discord, GuildMember member, DiscordUser user, DiscordServer server)
{ {
@ -29,6 +33,8 @@ namespace Maki
Server = server; Server = server;
Nickname = member.Nickname; Nickname = member.Nickname;
Joined = member.JoinedAt ?? DateTime.MinValue; Joined = member.JoinedAt ?? DateTime.MinValue;
IsDeaf = member.IsDeafened == true;
IsMute = member.IsMuted == true;
roles.AddRange(member.Roles); roles.AddRange(member.Roles);
} }

View file

@ -17,10 +17,11 @@ namespace Maki
public DiscordMember[] MentionsUsers { get; internal set; } public DiscordMember[] MentionsUsers { get; internal set; }
public DiscordRole[] MentionsRoles { get; internal set; } public DiscordRole[] MentionsRoles { get; internal set; }
public bool MentionsEveryone { get; internal set; } public bool MentionsEveryone { get; internal set; }
public bool IsPinned { get; internal set; }
public bool MentionsMe(bool everyone = false, bool roles = true) => public bool MentionsMe(bool everyone = false, bool roles = true) =>
(everyone && MentionsEveryone) (everyone && MentionsEveryone)
|| (roles && client.members.Where(x => x.User == client.Me && x.Server == Channel.Server).First().Roles.Where(x => MentionsRoles.Contains(x)).Count() > 0) || (Channel.Type != DiscordChannelType.Private && roles && client.members.Where(x => x.User == client.Me && x.Server == Channel.Server).First().Roles.Where(x => MentionsRoles.Contains(x)).Count() > 0)
|| MentionsUsers.Select(x => x.User).Contains(client.Me); || MentionsUsers.Select(x => x.User).Contains(client.Me);
public bool IsMe => client.Me == Sender.User; public bool IsMe => client.Me == Sender.User;
@ -36,6 +37,7 @@ namespace Maki
MentionsUsers = client.members.Where(x => x.Server == channel.Server && msg.Mentions.Select(y => y.Id).Contains(x.User.Id)).ToArray(); MentionsUsers = client.members.Where(x => x.Server == channel.Server && msg.Mentions.Select(y => y.Id).Contains(x.User.Id)).ToArray();
MentionsRoles = client.roles.Where(x => x.Server == channel.Server && msg.MentionsRoles.Contains(x.Id)).ToArray(); MentionsRoles = client.roles.Where(x => x.Server == channel.Server && msg.MentionsRoles.Contains(x.Id)).ToArray();
MentionsEveryone = msg.MentioningEveryone; MentionsEveryone = msg.MentioningEveryone;
IsPinned = msg.IsPinned == true;
} }
public DiscordMessage Edit(string text) public DiscordMessage Edit(string text)

View file

@ -17,6 +17,8 @@ namespace Maki
public bool IsHoisted { get; internal set; } public bool IsHoisted { get; internal set; }
public bool IsMentionable { get; internal set; } public bool IsMentionable { get; internal set; }
public override string ToString() => $@"<@&{Id}>";
internal DiscordRole(Discord discord, Role role, DiscordServer server) internal DiscordRole(Discord discord, Role role, DiscordServer server)
{ {
client = discord; client = discord;

View file

@ -41,8 +41,14 @@ namespace Maki
if (roleResp.ErrorCode != RestErrorCode.Ok) if (roleResp.ErrorCode != RestErrorCode.Ok)
throw new Exception($"{roleResp.ErrorCode}: {roleResp.ErrorMessage}"); throw new Exception($"{roleResp.ErrorCode}: {roleResp.ErrorMessage}");
DiscordRole role = new DiscordRole(client, roleResp.Response, this); DiscordRole role = client.roles.Where(x => x.Id == roleResp.Response.Id).FirstOrDefault();
if (role == default(DiscordRole))
{
role = new DiscordRole(client, roleResp.Response, this);
client.roles.Add(role); client.roles.Add(role);
}
return role; return role;
} }
} }

View file

@ -17,10 +17,11 @@ namespace Maki
public bool HasMFA { get; internal set; } public bool HasMFA { get; internal set; }
public bool IsVerified { get; internal set; } public bool IsVerified { get; internal set; }
public string EMail { get; internal set; } public string EMail { get; internal set; }
public DiscordUserStatus Status { get; internal set; }
public DiscordGame Game { get; internal set; }
public string NameWithTag => $"{Username}#{Tag:0000}"; public string NameWithTag => $"{Username}#{Tag:0000}";
public string At => $"<@{Id}>"; public override string ToString() => $"<@{Id}>";
public override string ToString() => At;
private string avatarHash; private string avatarHash;

10
Maki/DiscordUserStatus.cs Normal file
View file

@ -0,0 +1,10 @@
namespace Maki
{
public enum DiscordUserStatus
{
Online,
Offline,
Away,
DoNotDisturb,
}
}

View file

@ -59,10 +59,12 @@
<Compile Include="DiscordEmbedField.cs" /> <Compile Include="DiscordEmbedField.cs" />
<Compile Include="DiscordEmbedFooter.cs" /> <Compile Include="DiscordEmbedFooter.cs" />
<Compile Include="DiscordEmbedImage.cs" /> <Compile Include="DiscordEmbedImage.cs" />
<Compile Include="DiscordGame.cs" />
<Compile Include="DiscordMember.cs" /> <Compile Include="DiscordMember.cs" />
<Compile Include="DiscordMessage.cs" /> <Compile Include="DiscordMessage.cs" />
<Compile Include="DiscordRole.cs" /> <Compile Include="DiscordRole.cs" />
<Compile Include="DiscordServer.cs" /> <Compile Include="DiscordServer.cs" />
<Compile Include="DiscordUserStatus.cs" />
<Compile Include="DiscordUser.cs" /> <Compile Include="DiscordUser.cs" />
<Compile Include="Gateway\GatewayEvent.cs" /> <Compile Include="Gateway\GatewayEvent.cs" />
<Compile Include="Rest\RestErrorCode.cs" /> <Compile Include="Rest\RestErrorCode.cs" />

View file

@ -17,7 +17,7 @@ namespace Maki.Rest
/// <summary> /// <summary>
/// User agent that is send alongside requests /// User agent that is send alongside requests
/// </summary> /// </summary>
private const string USER_AGENT = @"DiscordBot (https://github.com/flashwave/nicobot)"; private const string USER_AGENT = @"DiscordBot (https://github.com/flashwave/maki, 1.0.0.0)";
/// <summary> /// <summary>
/// Container for parent DiscordClient instance /// Container for parent DiscordClient instance