This reverts commit 7237c6b3bf.
tags/docs-0.9
| @@ -3,74 +3,45 @@ using System; | |||
| using System.Collections.Concurrent; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Threading.Tasks; | |||
| namespace Discord.Modules | |||
| { | |||
| public class ModuleManager | |||
| { | |||
| /*public event AsyncEventHandler<ServerEventArgs> ServerEnabled { add { _serverEnabled.Add(value); } remove { _serverEnabled.Remove(value); } } | |||
| private readonly AsyncEvent<ServerEventArgs> _serverEnabled = new AsyncEvent<ServerEventArgs>(nameof(ServerEnabled)); | |||
| public event AsyncEventHandler<ServerEventArgs> ServerDisabled { add { _serverDisabled.Add(value); } remove { _serverDisabled.Remove(value); } } | |||
| private readonly AsyncEvent<ServerEventArgs> _serverDisabled = new AsyncEvent<ServerEventArgs>(nameof(ServerDisabled)); | |||
| public event AsyncEventHandler<ChannelEventArgs> ChannelEnabled { add { _channelEnabled.Add(value); } remove { _channelEnabled.Remove(value); } } | |||
| private readonly AsyncEvent<ChannelEventArgs> _channelEnabled = new AsyncEvent<ChannelEventArgs>(nameof(ChannelEnabled)); | |||
| public event AsyncEventHandler<ChannelEventArgs> ChannelDisabled { add { _channelDisabled.Add(value); } remove { _channelDisabled.Remove(value); } } | |||
| private readonly AsyncEvent<ChannelEventArgs> _channelDisabled = new AsyncEvent<ChannelEventArgs>(nameof(ChannelDisabled));*/ | |||
| public event AsyncEventHandler<ServerEventArgs> LeftServer { add { _leftServer.Add(value); } remove { _leftServer.Remove(value); } } | |||
| private readonly AsyncEvent<ServerEventArgs> _leftServer = new AsyncEvent<ServerEventArgs>(nameof(LeftServer)); | |||
| public event AsyncEventHandler<ServerEventArgs> ServerUpdated { add { _serverUpdated.Add(value); } remove { _serverUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<ServerEventArgs> _serverUpdated = new AsyncEvent<ServerEventArgs>(nameof(ServerUpdated)); | |||
| public event AsyncEventHandler<ServerEventArgs> ServerUnavailable { add { _serverUnavailable.Add(value); } remove { _serverUnavailable.Remove(value); } } | |||
| private readonly AsyncEvent<ServerEventArgs> _serverUnavailable = new AsyncEvent<ServerEventArgs>(nameof(ServerUnavailable)); | |||
| public event AsyncEventHandler<ServerEventArgs> ServerAvailable { add { _serverAvailable.Add(value); } remove { _serverAvailable.Remove(value); } } | |||
| private readonly AsyncEvent<ServerEventArgs> _serverAvailable = new AsyncEvent<ServerEventArgs>(nameof(ServerAvailable)); | |||
| public event AsyncEventHandler<BanEventArgs> UserBanned { add { _userBanned.Add(value); } remove { _userBanned.Remove(value); } } | |||
| private readonly AsyncEvent<BanEventArgs> _userBanned = new AsyncEvent<BanEventArgs>(nameof(UserBanned)); | |||
| public event AsyncEventHandler<BanEventArgs> UserUnbanned { add { _userUnbanned.Add(value); } remove { _userUnbanned.Remove(value); } } | |||
| private readonly AsyncEvent<BanEventArgs> _userUnbanned = new AsyncEvent<BanEventArgs>(nameof(UserUnbanned)); | |||
| public event AsyncEventHandler<ChannelEventArgs> ChannelCreated { add { _channelCreated.Add(value); } remove { _channelCreated.Remove(value); } } | |||
| private readonly AsyncEvent<ChannelEventArgs> _channelCreated = new AsyncEvent<ChannelEventArgs>(nameof(ChannelCreated)); | |||
| public event AsyncEventHandler<ChannelEventArgs> ChannelDestroyed { add { _channelDestroyed.Add(value); } remove { _channelDestroyed.Remove(value); } } | |||
| private readonly AsyncEvent<ChannelEventArgs> _channelDestroyed = new AsyncEvent<ChannelEventArgs>(nameof(ChannelDestroyed)); | |||
| public event AsyncEventHandler<ChannelEventArgs> ChannelUpdated { add { _channelUpdated.Add(value); } remove { _channelUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<ChannelEventArgs> _channelUpdated = new AsyncEvent<ChannelEventArgs>(nameof(ChannelUpdated)); | |||
| public event AsyncEventHandler<RoleEventArgs> RoleCreated { add { _roleCreated.Add(value); } remove { _roleCreated.Remove(value); } } | |||
| private readonly AsyncEvent<RoleEventArgs> _roleCreated = new AsyncEvent<RoleEventArgs>(nameof(RoleCreated)); | |||
| public event AsyncEventHandler<RoleEventArgs> RoleUpdated { add { _roleUpdated.Add(value); } remove { _roleUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<RoleEventArgs> _roleUpdated = new AsyncEvent<RoleEventArgs>(nameof(RoleUpdated)); | |||
| public event AsyncEventHandler<RoleEventArgs> RoleDeleted { add { _roleDeleted.Add(value); } remove { _roleDeleted.Remove(value); } } | |||
| private readonly AsyncEvent<RoleEventArgs> _roleDeleted = new AsyncEvent<RoleEventArgs>(nameof(RoleDeleted)); | |||
| public event AsyncEventHandler<UserEventArgs> UserJoined { add { _userJoined.Add(value); } remove { _userJoined.Remove(value); } } | |||
| private readonly AsyncEvent<UserEventArgs> _userJoined = new AsyncEvent<UserEventArgs>(nameof(UserJoined)); | |||
| public event AsyncEventHandler<UserEventArgs> UserLeft { add { _userLeft.Add(value); } remove { _userLeft.Remove(value); } } | |||
| private readonly AsyncEvent<UserEventArgs> _userLeft = new AsyncEvent<UserEventArgs>(nameof(UserLeft)); | |||
| public event AsyncEventHandler<UserEventArgs> UserUpdated { add { _userUpdated.Add(value); } remove { _userUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<UserEventArgs> _userUpdated = new AsyncEvent<UserEventArgs>(nameof(UserUpdated)); | |||
| public event AsyncEventHandler<UserEventArgs> UserPresenceUpdated { add { _userPresenceUpdated.Add(value); } remove { _userPresenceUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<UserEventArgs> _userPresenceUpdated = new AsyncEvent<UserEventArgs>(nameof(UserPresenceUpdated)); | |||
| public event AsyncEventHandler<UserEventArgs> UserVoiceStateUpdated { add { _userVoiceStateUpdated.Add(value); } remove { _userVoiceStateUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<UserEventArgs> _userVoiceStateUpdated = new AsyncEvent<UserEventArgs>(nameof(UserVoiceStateUpdated)); | |||
| public event AsyncEventHandler<UserChannelEventArgs> UserIsTypingUpdated { add { _userIsTypingUpdated.Add(value); } remove { _userIsTypingUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<UserChannelEventArgs> _userIsTypingUpdated = new AsyncEvent<UserChannelEventArgs>(nameof(UserIsTypingUpdated)); | |||
| public event AsyncEventHandler<UserIsSpeakingEventArgs> UserIsSpeakingUpdated { add { _userIsSpeakingUpdated.Add(value); } remove { _userIsSpeakingUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<UserIsSpeakingEventArgs> _userIsSpeakingUpdated = new AsyncEvent<UserIsSpeakingEventArgs>(nameof(UserIsSpeakingUpdated)); | |||
| public event AsyncEventHandler<MessageEventArgs> MessageReceived { add { _messageReceived.Add(value); } remove { _messageReceived.Remove(value); } } | |||
| private readonly AsyncEvent<MessageEventArgs> _messageReceived = new AsyncEvent<MessageEventArgs>(nameof(MessageReceived)); | |||
| public event AsyncEventHandler<MessageEventArgs> MessageSent { add { _messageSent.Add(value); } remove { _messageSent.Remove(value); } } | |||
| private readonly AsyncEvent<MessageEventArgs> _messageSent = new AsyncEvent<MessageEventArgs>(nameof(MessageSent)); | |||
| public event AsyncEventHandler<MessageEventArgs> MessageDeleted { add { _messageDeleted.Add(value); } remove { _messageDeleted.Remove(value); } } | |||
| private readonly AsyncEvent<MessageEventArgs> _messageDeleted = new AsyncEvent<MessageEventArgs>(nameof(MessageDeleted)); | |||
| public event AsyncEventHandler<MessageEventArgs> MessageUpdated { add { _messageUpdated.Add(value); } remove { _messageUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<MessageEventArgs> _messageUpdated = new AsyncEvent<MessageEventArgs>(nameof(MessageUpdated)); | |||
| public event AsyncEventHandler<MessageEventArgs> MessageReadRemotely { add { _messageReadRemotely.Add(value); } remove { _messageReadRemotely.Remove(value); } } | |||
| private readonly AsyncEvent<MessageEventArgs> _messageReadRemotely = new AsyncEvent<MessageEventArgs>(nameof(MessageReadRemotely)); | |||
| public event EventHandler<ServerEventArgs> ServerEnabled; | |||
| public event EventHandler<ServerEventArgs> ServerDisabled; | |||
| public event EventHandler<ChannelEventArgs> ChannelEnabled; | |||
| public event EventHandler<ChannelEventArgs> ChannelDisabled; | |||
| public event EventHandler<ServerEventArgs> LeftServer; | |||
| public event EventHandler<ServerEventArgs> ServerUpdated; | |||
| public event EventHandler<ServerEventArgs> ServerUnavailable; | |||
| public event EventHandler<ServerEventArgs> ServerAvailable; | |||
| public event EventHandler<BanEventArgs> UserBanned; | |||
| public event EventHandler<BanEventArgs> UserUnbanned; | |||
| public event EventHandler<ChannelEventArgs> ChannelCreated; | |||
| public event EventHandler<ChannelEventArgs> ChannelDestroyed; | |||
| public event EventHandler<ChannelEventArgs> ChannelUpdated; | |||
| public event EventHandler<RoleEventArgs> RoleCreated; | |||
| public event EventHandler<RoleEventArgs> RoleUpdated; | |||
| public event EventHandler<RoleEventArgs> RoleDeleted; | |||
| public event EventHandler<UserEventArgs> UserJoined; | |||
| public event EventHandler<UserEventArgs> UserLeft; | |||
| public event EventHandler<UserEventArgs> UserUpdated; | |||
| public event EventHandler<UserEventArgs> UserPresenceUpdated; | |||
| public event EventHandler<UserEventArgs> UserVoiceStateUpdated; | |||
| public event EventHandler<UserChannelEventArgs> UserIsTypingUpdated; | |||
| public event EventHandler<UserIsSpeakingEventArgs> UserIsSpeakingUpdated; | |||
| public event EventHandler<MessageEventArgs> MessageReceived; | |||
| public event EventHandler<MessageEventArgs> MessageSent; | |||
| public event EventHandler<MessageEventArgs> MessageDeleted; | |||
| public event EventHandler<MessageEventArgs> MessageUpdated; | |||
| public event EventHandler<MessageEventArgs> MessageReadRemotely; | |||
| private readonly DiscordClient _client; | |||
| private readonly string _name, _id; | |||
| @@ -105,167 +76,38 @@ namespace Discord.Modules | |||
| if (_allowAll || _useServerWhitelist) //Server-only events | |||
| { | |||
| client.ChannelCreated += (s, e) => | |||
| { | |||
| if (_channelCreated.Any && HasServer(e.Server)) | |||
| return _channelCreated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.ChannelCreated += (s, e) => { if (ChannelCreated != null && HasServer(e.Server)) ChannelCreated(s, e); }; | |||
| client.UserVoiceStateUpdated += (s, e) => | |||
| { | |||
| if (_userVoiceStateUpdated.Any && HasServer(e.Server)) | |||
| return _userVoiceStateUpdated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.UserIsSpeakingUpdated += (s, e) => | |||
| { | |||
| if (_userIsSpeakingUpdated.Any && HasServer(e.Server)) | |||
| return _userIsSpeakingUpdated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.UserVoiceStateUpdated += (s, e) => { if (UserVoiceStateUpdated != null && HasServer(e.Server)) UserVoiceStateUpdated(s, e); }; | |||
| client.UserIsSpeakingUpdated += (s, e) => { if (UserIsSpeakingUpdated != null && HasServer(e.Server)) UserIsSpeakingUpdated(s, e); }; | |||
| } | |||
| client.ChannelDestroyed += (s, e) => | |||
| { | |||
| if (_channelDestroyed.Any && HasChannel(e.Channel)) | |||
| return _channelDestroyed.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.ChannelUpdated += (s, e) => | |||
| { | |||
| if (_channelUpdated.Any && HasChannel(e.Channel)) | |||
| return _channelUpdated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.ChannelDestroyed += (s, e) => { if (ChannelDestroyed != null && HasChannel(e.Channel)) ChannelDestroyed(s, e); }; | |||
| client.ChannelUpdated += (s, e) => { if (ChannelUpdated != null && HasChannel(e.Channel)) ChannelUpdated(s, e); }; | |||
| client.MessageReceived += (s, e) => | |||
| { | |||
| if (_messageReceived.Any && HasChannel(e.Channel)) | |||
| return _messageReceived.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.MessageSent += (s, e) => | |||
| { | |||
| if (_messageSent.Any && HasChannel(e.Channel)) | |||
| return _messageSent.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.MessageDeleted += (s, e) => | |||
| { | |||
| if (_messageDeleted.Any && HasChannel(e.Channel)) | |||
| return _messageDeleted.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.MessageUpdated += (s, e) => | |||
| { | |||
| if (_messageUpdated.Any && HasChannel(e.Channel)) | |||
| return _messageUpdated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.MessageReadRemotely += (s, e) => | |||
| { | |||
| if (_messageReadRemotely.Any && HasChannel(e.Channel)) | |||
| return _messageReadRemotely.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.MessageReceived += (s, e) => { if (MessageReceived != null && HasChannel(e.Channel)) MessageReceived(s, e); }; | |||
| client.MessageSent += (s, e) => { if (MessageSent != null && HasChannel(e.Channel)) MessageSent(s, e); }; | |||
| client.MessageDeleted += (s, e) => { if (MessageDeleted != null && HasChannel(e.Channel)) MessageDeleted(s, e); }; | |||
| client.MessageUpdated += (s, e) => { if (MessageUpdated != null && HasChannel(e.Channel)) MessageUpdated(s, e); }; | |||
| client.MessageReadRemotely += (s, e) => { if (MessageReadRemotely != null && HasChannel(e.Channel)) MessageReadRemotely(s, e); }; | |||
| client.RoleCreated += (s, e) => | |||
| { | |||
| if (_roleCreated.Any && HasIndirectServer(e.Server)) | |||
| return _roleCreated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.RoleUpdated += (s, e) => | |||
| { | |||
| if (_roleUpdated.Any && HasIndirectServer(e.Server)) | |||
| return _roleUpdated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.RoleDeleted += (s, e) => | |||
| { | |||
| if (_roleDeleted.Any && HasIndirectServer(e.Server)) | |||
| return _roleDeleted.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.RoleCreated += (s, e) => { if (RoleCreated != null && HasIndirectServer(e.Server)) RoleCreated(s, e); }; | |||
| client.RoleUpdated += (s, e) => { if (RoleUpdated != null && HasIndirectServer(e.Server)) RoleUpdated(s, e); }; | |||
| client.RoleDeleted += (s, e) => { if (RoleDeleted != null && HasIndirectServer(e.Server)) RoleDeleted(s, e); }; | |||
| client.LeftServer += (s, e) => | |||
| { | |||
| if (_leftServer.Any && HasIndirectServer(e.Server)) | |||
| { | |||
| DisableServer(e.Server); | |||
| return _leftServer.Invoke(s, e); | |||
| } | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.ServerUpdated += (s, e) => | |||
| { | |||
| if (_serverUpdated.Any && HasIndirectServer(e.Server)) | |||
| return _serverUpdated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.ServerUnavailable += (s, e) => | |||
| { | |||
| if (_serverUnavailable.Any && HasIndirectServer(e.Server)) | |||
| return _serverUnavailable.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.ServerAvailable += (s, e) => | |||
| { | |||
| if (_serverAvailable.Any && HasIndirectServer(e.Server)) | |||
| return _serverAvailable.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.LeftServer += (s, e) => { if (LeftServer != null && HasIndirectServer(e.Server)) { DisableServer(e.Server); LeftServer(s, e); } }; | |||
| client.ServerUpdated += (s, e) => { if (ServerUpdated != null && HasIndirectServer(e.Server)) ServerUpdated(s, e); }; | |||
| client.ServerUnavailable += (s, e) => { if (ServerUnavailable != null && HasIndirectServer(e.Server)) ServerUnavailable(s, e); }; | |||
| client.ServerAvailable += (s, e) => { if (ServerAvailable != null && HasIndirectServer(e.Server)) ServerAvailable(s, e); }; | |||
| client.UserJoined += (s, e) => | |||
| { | |||
| if (_userJoined.Any && HasIndirectServer(e.Server)) | |||
| return _userJoined.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.UserLeft += (s, e) => | |||
| { | |||
| if (_userLeft.Any && HasIndirectServer(e.Server)) | |||
| return _userLeft.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.UserUpdated += (s, e) => | |||
| { | |||
| if (_userUpdated.Any && HasIndirectServer(e.Server)) | |||
| return _userUpdated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.UserIsTypingUpdated += (s, e) => | |||
| { | |||
| if (_userIsTypingUpdated.Any && HasChannel(e.Channel)) | |||
| return _userIsTypingUpdated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.UserIsSpeakingUpdated += (s, e) => | |||
| { | |||
| if (_userIsSpeakingUpdated.Any && HasChannel(e.Channel)) | |||
| return _userIsSpeakingUpdated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.UserJoined += (s, e) => { if (UserJoined != null && HasIndirectServer(e.Server)) UserJoined(s, e); }; | |||
| client.UserLeft += (s, e) => { if (UserLeft != null && HasIndirectServer(e.Server)) UserLeft(s, e); }; | |||
| client.UserUpdated += (s, e) => { if (UserUpdated != null && HasIndirectServer(e.Server)) UserUpdated(s, e); }; | |||
| client.UserIsTypingUpdated += (s, e) => { if (UserIsSpeakingUpdated != null && HasChannel(e.Channel)) UserIsTypingUpdated(s, e); }; | |||
| //TODO: We aren't getting events from UserPresence if AllowPrivate is enabled, but the server we know that user through isn't on the whitelist | |||
| client.UserPresenceUpdated += (s, e) => | |||
| { | |||
| if (_userPresenceUpdated.Any && HasIndirectServer(e.Server)) | |||
| return _userPresenceUpdated.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.UserBanned += (s, e) => | |||
| { | |||
| if (_userBanned.Any && HasIndirectServer(e.Server)) | |||
| return _userBanned.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.UserUnbanned += (s, e) => | |||
| { | |||
| if (_userUnbanned.Any && HasIndirectServer(e.Server)) | |||
| return _userUnbanned.Invoke(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| client.UserPresenceUpdated += (s, e) => { if (UserPresenceUpdated != null && HasIndirectServer(e.Server)) UserPresenceUpdated(s, e); }; | |||
| client.UserBanned += (s, e) => { if (UserBanned != null && HasIndirectServer(e.Server)) UserBanned(s, e); }; | |||
| client.UserUnbanned += (s, e) => { if (UserUnbanned != null && HasIndirectServer(e.Server)) UserUnbanned(s, e); }; | |||
| } | |||
| public void CreateCommands(string prefix, Action<CommandGroupBuilder> config) | |||
| @@ -285,62 +127,26 @@ namespace Discord.Modules | |||
| if (!_useServerWhitelist) throw new InvalidOperationException("This module is not configured to use a server whitelist."); | |||
| lock (this) | |||
| return _enabledServers.TryAdd(server.Id, server); | |||
| } | |||
| public bool EnableServers(IEnumerable<Server> servers) | |||
| return EnableServerInternal(server); | |||
| } | |||
| public void EnableServers(IEnumerable<Server> servers) | |||
| { | |||
| if (servers == null) throw new ArgumentNullException(nameof(servers)); | |||
| if (servers.Contains(null)) throw new ArgumentException("Collection cannot contain null.", nameof(servers)); | |||
| if (!_useServerWhitelist) throw new InvalidOperationException("This module is not configured to use a server whitelist."); | |||
| bool result = false; | |||
| lock (this) | |||
| { | |||
| foreach (var server in servers) | |||
| { | |||
| if (_enabledServers.TryAdd(server.Id, server)) | |||
| result |= true; | |||
| } | |||
| EnableServerInternal(server); | |||
| } | |||
| return result; | |||
| } | |||
| public bool EnableChannel(Channel channel) | |||
| { | |||
| if (channel == null) throw new ArgumentNullException(nameof(channel)); | |||
| if (!_useChannelWhitelist) throw new InvalidOperationException("This module is not configured to use a channel whitelist."); | |||
| lock (this) | |||
| return EnableChannelInternal(channel); | |||
| } | |||
| public bool EnableChannels(IEnumerable<Channel> channels) | |||
| { | |||
| if (channels == null) throw new ArgumentNullException(nameof(channels)); | |||
| if (channels.Contains(null)) throw new ArgumentException("Collection cannot contain null.", nameof(channels)); | |||
| if (!_useChannelWhitelist) throw new InvalidOperationException("This module is not configured to use a channel whitelist."); | |||
| bool result = false; | |||
| lock (this) | |||
| { | |||
| foreach (var channel in channels) | |||
| { | |||
| if (EnableChannelInternal(channel)) | |||
| result |= true; | |||
| } | |||
| } | |||
| return result; | |||
| } | |||
| private bool EnableChannelInternal(Channel channel) | |||
| private bool EnableServerInternal(Server server) | |||
| { | |||
| if (_enabledChannels.TryAdd(channel.Id, channel)) | |||
| if (_enabledServers.TryAdd(server.Id, server)) | |||
| { | |||
| var server = channel.Server; | |||
| if (server != null) | |||
| { | |||
| int value = 0; | |||
| _indirectServers.TryGetValue(server.Id, out value); | |||
| value++; | |||
| _indirectServers[server.Id] = value; | |||
| } | |||
| if (ServerEnabled != null) | |||
| ServerEnabled(this, new ServerEventArgs(server)); | |||
| return true; | |||
| } | |||
| return false; | |||
| @@ -352,85 +158,123 @@ namespace Discord.Modules | |||
| if (!_useServerWhitelist) throw new InvalidOperationException("This module is not configured to use a server whitelist."); | |||
| lock (this) | |||
| return _enabledServers.TryRemove(server.Id, out server); | |||
| { | |||
| if (_enabledServers.TryRemove(server.Id, out server)) | |||
| { | |||
| if (ServerDisabled != null) | |||
| ServerDisabled(this, new ServerEventArgs(server)); | |||
| return true; | |||
| } | |||
| return false; | |||
| } | |||
| } | |||
| public bool DisableServers(IEnumerable<Server> servers) | |||
| public void DisableAllServers() | |||
| { | |||
| if (servers == null) throw new ArgumentNullException(nameof(servers)); | |||
| if (servers.Contains(null)) throw new ArgumentException("Collection cannot contain null.", nameof(servers)); | |||
| if (!_useServerWhitelist) throw new InvalidOperationException("This module is not configured to use a server whitelist."); | |||
| bool result = false; | |||
| lock (this) | |||
| { | |||
| foreach (var server in servers) | |||
| if (ServerDisabled != null) | |||
| { | |||
| Server ignored; | |||
| if (_enabledServers.TryRemove(server.Id, out ignored)) | |||
| result |= true; | |||
| } | |||
| foreach (var server in _enabledServers) | |||
| ServerDisabled(this, new ServerEventArgs(server.Value)); | |||
| } | |||
| _enabledServers.Clear(); | |||
| } | |||
| return result; | |||
| } | |||
| public bool DisableChannel(Channel channel) | |||
| public bool EnableChannel(Channel channel) | |||
| { | |||
| if (channel == null) throw new ArgumentNullException(nameof(channel)); | |||
| if (!_useChannelWhitelist) throw new InvalidOperationException("This module is not configured to use a channel whitelist."); | |||
| if (!_useChannelWhitelist) throw new InvalidOperationException("This module is not configured to use a channel whitelist."); | |||
| lock (this) | |||
| return DisableChannelInternal(channel); | |||
| return EnableChannelInternal(channel); | |||
| } | |||
| public bool DisableChannels(IEnumerable<Channel> channels) | |||
| public void EnableChannels(IEnumerable<Channel> channels) | |||
| { | |||
| if (channels == null) throw new ArgumentNullException(nameof(channels)); | |||
| if (channels.Contains(null)) throw new ArgumentException("Collection cannot contain null.", nameof(channels)); | |||
| if (!_useChannelWhitelist) throw new InvalidOperationException("This module is not configured to use a channel whitelist."); | |||
| bool result = false; | |||
| lock (this) | |||
| { | |||
| foreach (var channel in channels) | |||
| result |= DisableChannelInternal(channel); | |||
| } | |||
| return result; | |||
| EnableChannelInternal(channel); | |||
| } | |||
| } | |||
| private bool DisableChannelInternal(Channel channel) | |||
| private bool EnableChannelInternal(Channel channel) | |||
| { | |||
| Channel ignored; | |||
| if (_enabledChannels.TryRemove(channel.Id, out ignored)) | |||
| if (_enabledChannels.TryAdd(channel.Id, channel)) | |||
| { | |||
| var server = channel.Server; | |||
| if (server != null) | |||
| { | |||
| int value = 0; | |||
| if (_indirectServers.TryGetValue(server.Id, out value)) | |||
| _indirectServers.TryGetValue(server.Id, out value); | |||
| value++; | |||
| _indirectServers[server.Id] = value; | |||
| } | |||
| if (ChannelEnabled != null) | |||
| ChannelEnabled(this, new ChannelEventArgs(channel)); | |||
| return true; | |||
| } | |||
| return false; | |||
| } | |||
| public bool DisableChannel(Channel channel) | |||
| { | |||
| if (channel == null) throw new ArgumentNullException(nameof(channel)); | |||
| if (!_useChannelWhitelist) throw new InvalidOperationException("This module is not configured to use a channel whitelist."); | |||
| lock (this) | |||
| { | |||
| Channel ignored; | |||
| if (_enabledChannels.TryRemove(channel.Id, out ignored)) | |||
| { | |||
| var server = channel.Server; | |||
| if (server != null) | |||
| { | |||
| int value = 0; | |||
| _indirectServers.TryGetValue(server.Id, out value); | |||
| value--; | |||
| if (value <= 0) | |||
| _indirectServers.TryRemove(server.Id, out value); | |||
| else | |||
| _indirectServers[server.Id] = value; | |||
| } | |||
| if (ChannelDisabled != null) | |||
| ChannelDisabled(this, new ChannelEventArgs(channel)); | |||
| return true; | |||
| } | |||
| return true; | |||
| return false; | |||
| } | |||
| return false; | |||
| } | |||
| public bool DisableAll() | |||
| public void DisableAllChannels() | |||
| { | |||
| bool result = false; | |||
| if (_useServerWhitelist) | |||
| { | |||
| result |= _enabledServers.Count != 0; | |||
| _enabledServers.Clear(); | |||
| } | |||
| if (_useChannelWhitelist) | |||
| if (!_useChannelWhitelist) throw new InvalidOperationException("This module is not configured to use a channel whitelist."); | |||
| lock (this) | |||
| { | |||
| result |= _enabledServers.Count != 0; | |||
| if (ChannelDisabled != null) | |||
| { | |||
| foreach (var channel in _enabledChannels) | |||
| ChannelDisabled(this, new ChannelEventArgs(channel.Value)); | |||
| } | |||
| _enabledChannels.Clear(); | |||
| _indirectServers.Clear(); | |||
| } | |||
| return result; | |||
| } | |||
| } | |||
| public void DisableAll() | |||
| { | |||
| if (_useServerWhitelist) | |||
| DisableAllServers(); | |||
| if (_useChannelWhitelist) | |||
| DisableAllChannels(); | |||
| } | |||
| internal bool HasServer(Server server) => | |||
| _allowAll || | |||
| @@ -206,9 +206,6 @@ | |||
| <Compile Include="..\Discord.Net\Helpers\AsyncCollection.cs"> | |||
| <Link>Helpers\AsyncCollection.cs</Link> | |||
| </Compile> | |||
| <Compile Include="..\Discord.Net\Helpers\AsyncEvent.cs"> | |||
| <Link>Helpers\AsyncEvent.cs</Link> | |||
| </Compile> | |||
| <Compile Include="..\Discord.Net\Helpers\BitHelper.cs"> | |||
| <Link>Helpers\BitHelper.cs</Link> | |||
| </Compile> | |||
| @@ -46,20 +46,24 @@ namespace Discord | |||
| public partial class DiscordClient | |||
| { | |||
| public event AsyncEventHandler<ChannelEventArgs> ChannelCreated { add { _channelCreated.Add(value); } remove { _channelCreated.Remove(value); } } | |||
| private readonly AsyncEvent<ChannelEventArgs> _channelCreated = new AsyncEvent<ChannelEventArgs>(nameof(ChannelCreated)); | |||
| private Task RaiseChannelCreated(Channel channel) | |||
| => RaiseEvent(_channelCreated, new ChannelEventArgs(channel)); | |||
| public event AsyncEventHandler<ChannelEventArgs> ChannelDestroyed { add { _channelDestroyed.Add(value); } remove { _channelDestroyed.Remove(value); } } | |||
| private readonly AsyncEvent<ChannelEventArgs> _channelDestroyed = new AsyncEvent<ChannelEventArgs>(nameof(ChannelDestroyed)); | |||
| private Task RaiseChannelDestroyed(Channel channel) | |||
| => RaiseEvent(_channelDestroyed, new ChannelEventArgs(channel)); | |||
| public event AsyncEventHandler<ChannelEventArgs> ChannelUpdated { add { _channelUpdated.Add(value); } remove { _channelUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<ChannelEventArgs> _channelUpdated = new AsyncEvent<ChannelEventArgs>(nameof(ChannelUpdated)); | |||
| private Task RaiseChannelUpdated(Channel channel) | |||
| => RaiseEvent(_channelUpdated, new ChannelEventArgs(channel)); | |||
| public event EventHandler<ChannelEventArgs> ChannelCreated; | |||
| private void RaiseChannelCreated(Channel channel) | |||
| { | |||
| if (ChannelCreated != null) | |||
| RaiseEvent(nameof(ChannelCreated), () => ChannelCreated(this, new ChannelEventArgs(channel))); | |||
| } | |||
| public event EventHandler<ChannelEventArgs> ChannelDestroyed; | |||
| private void RaiseChannelDestroyed(Channel channel) | |||
| { | |||
| if (ChannelDestroyed != null) | |||
| RaiseEvent(nameof(ChannelDestroyed), () => ChannelDestroyed(this, new ChannelEventArgs(channel))); | |||
| } | |||
| public event EventHandler<ChannelEventArgs> ChannelUpdated; | |||
| private void RaiseChannelUpdated(Channel channel) | |||
| { | |||
| if (ChannelUpdated != null) | |||
| RaiseEvent(nameof(ChannelUpdated), () => ChannelUpdated(this, new ChannelEventArgs(channel))); | |||
| } | |||
| /// <summary> Returns a collection of all servers this client is a member of. </summary> | |||
| public IEnumerable<Channel> PrivateChannels { get { CheckReady(); return _channels.PrivateChannels; } } | |||
| @@ -50,30 +50,36 @@ namespace Discord | |||
| { | |||
| public const int MaxMessageSize = 2000; | |||
| public event AsyncEventHandler<MessageEventArgs> MessageReceived { add { _messageReceived.Add(value); } remove { _messageReceived.Remove(value); } } | |||
| private readonly AsyncEvent<MessageEventArgs> _messageReceived = new AsyncEvent<MessageEventArgs>(nameof(MessageReceived)); | |||
| private Task RaiseMessageReceived(Message msg) | |||
| => RaiseEvent(_messageReceived, new MessageEventArgs(msg)); | |||
| public event AsyncEventHandler<MessageEventArgs> MessageSent { add { _messageSent.Add(value); } remove { _messageSent.Remove(value); } } | |||
| private readonly AsyncEvent<MessageEventArgs> _messageSent = new AsyncEvent<MessageEventArgs>(nameof(MessageSent)); | |||
| private Task RaiseMessageSent(Message msg) | |||
| => RaiseEvent(_messageSent, new MessageEventArgs(msg)); | |||
| public event AsyncEventHandler<MessageEventArgs> MessageDeleted { add { _messageDeleted.Add(value); } remove { _messageDeleted.Remove(value); } } | |||
| private readonly AsyncEvent<MessageEventArgs> _messageDeleted = new AsyncEvent<MessageEventArgs>(nameof(MessageDeleted)); | |||
| private Task RaiseMessageDeleted(Message msg) | |||
| => RaiseEvent(_messageDeleted, new MessageEventArgs(msg)); | |||
| public event AsyncEventHandler<MessageEventArgs> MessageUpdated { add { _messageUpdated.Add(value); } remove { _messageUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<MessageEventArgs> _messageUpdated = new AsyncEvent<MessageEventArgs>(nameof(MessageUpdated)); | |||
| private Task RaiseMessageUpdated(Message msg) | |||
| => RaiseEvent(_messageUpdated, new MessageEventArgs(msg)); | |||
| public event AsyncEventHandler<MessageEventArgs> MessageReadRemotely { add { _messageReadRemotely.Add(value); } remove { _messageReadRemotely.Remove(value); } } | |||
| private readonly AsyncEvent<MessageEventArgs> _messageReadRemotely = new AsyncEvent<MessageEventArgs>(nameof(MessageReadRemotely)); | |||
| private Task RaiseMessageReadRemotely(Message msg) | |||
| => RaiseEvent(_messageReadRemotely, new MessageEventArgs(msg)); | |||
| public event EventHandler<MessageEventArgs> MessageReceived; | |||
| private void RaiseMessageReceived(Message msg) | |||
| { | |||
| if (MessageReceived != null) | |||
| RaiseEvent(nameof(MessageReceived), () => MessageReceived(this, new MessageEventArgs(msg))); | |||
| } | |||
| public event EventHandler<MessageEventArgs> MessageSent; | |||
| private void RaiseMessageSent(Message msg) | |||
| { | |||
| if (MessageSent != null) | |||
| RaiseEvent(nameof(MessageSent), () => MessageSent(this, new MessageEventArgs(msg))); | |||
| } | |||
| public event EventHandler<MessageEventArgs> MessageDeleted; | |||
| private void RaiseMessageDeleted(Message msg) | |||
| { | |||
| if (MessageDeleted != null) | |||
| RaiseEvent(nameof(MessageDeleted), () => MessageDeleted(this, new MessageEventArgs(msg))); | |||
| } | |||
| public event EventHandler<MessageEventArgs> MessageUpdated; | |||
| private void RaiseMessageUpdated(Message msg) | |||
| { | |||
| if (MessageUpdated != null) | |||
| RaiseEvent(nameof(MessageUpdated), () => MessageUpdated(this, new MessageEventArgs(msg))); | |||
| } | |||
| public event EventHandler<MessageEventArgs> MessageReadRemotely; | |||
| private void RaiseMessageReadRemotely(Message msg) | |||
| { | |||
| if (MessageReadRemotely != null) | |||
| RaiseEvent(nameof(MessageReadRemotely), () => MessageReadRemotely(this, new MessageEventArgs(msg))); | |||
| } | |||
| internal Messages Messages => _messages; | |||
| private readonly Messages _messages; | |||
| @@ -152,7 +158,7 @@ namespace Discord | |||
| var model = await _api.SendMessage(channel.Id, text, mentionedUsers.Select(x => x.Id), null, isTextToSpeech).ConfigureAwait(false); | |||
| msg = _messages.GetOrAdd(model.Id, channel.Id, model.Author.Id); | |||
| msg.Update(model); | |||
| await RaiseMessageSent(msg); | |||
| RaiseMessageSent(msg); | |||
| } | |||
| return msg; | |||
| } | |||
| @@ -325,7 +331,7 @@ namespace Discord | |||
| } | |||
| else | |||
| msg.State = MessageState.Failed; | |||
| await RaiseMessageSent(msg); | |||
| RaiseMessageSent(msg); | |||
| } | |||
| await Task.Delay(interval).ConfigureAwait(false); | |||
| } | |||
| @@ -25,20 +25,24 @@ namespace Discord | |||
| public partial class DiscordClient | |||
| { | |||
| public event AsyncEventHandler<RoleEventArgs> RoleCreated { add { _roleCreated.Add(value); } remove { _roleCreated.Remove(value); } } | |||
| private readonly AsyncEvent<RoleEventArgs> _roleCreated = new AsyncEvent<RoleEventArgs>(nameof(RoleCreated)); | |||
| private Task RaiseRoleCreated(Role role) | |||
| => RaiseEvent(_roleCreated, new RoleEventArgs(role)); | |||
| public event AsyncEventHandler<RoleEventArgs> RoleUpdated { add { _roleUpdated.Add(value); } remove { _roleUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<RoleEventArgs> _roleUpdated = new AsyncEvent<RoleEventArgs>(nameof(RoleUpdated)); | |||
| private Task RaiseRoleUpdated(Role role) | |||
| => RaiseEvent(_roleUpdated, new RoleEventArgs(role)); | |||
| public event AsyncEventHandler<RoleEventArgs> RoleDeleted { add { _roleDeleted.Add(value); } remove { _roleDeleted.Remove(value); } } | |||
| private readonly AsyncEvent<RoleEventArgs> _roleDeleted = new AsyncEvent<RoleEventArgs>(nameof(RoleDeleted)); | |||
| private Task RaiseRoleDeleted(Role role) | |||
| => RaiseEvent(_roleDeleted, new RoleEventArgs(role)); | |||
| public event EventHandler<RoleEventArgs> RoleCreated; | |||
| private void RaiseRoleCreated(Role role) | |||
| { | |||
| if (RoleCreated != null) | |||
| RaiseEvent(nameof(RoleCreated), () => RoleCreated(this, new RoleEventArgs(role))); | |||
| } | |||
| public event EventHandler<RoleEventArgs> RoleUpdated; | |||
| private void RaiseRoleDeleted(Role role) | |||
| { | |||
| if (RoleDeleted != null) | |||
| RaiseEvent(nameof(RoleDeleted), () => RoleDeleted(this, new RoleEventArgs(role))); | |||
| } | |||
| public event EventHandler<RoleEventArgs> RoleDeleted; | |||
| private void RaiseRoleUpdated(Role role) | |||
| { | |||
| if (RoleUpdated != null) | |||
| RaiseEvent(nameof(RoleUpdated), () => RoleUpdated(this, new RoleEventArgs(role))); | |||
| } | |||
| internal Roles Roles => _roles; | |||
| private readonly Roles _roles; | |||
| @@ -25,31 +25,36 @@ namespace Discord | |||
| public partial class DiscordClient | |||
| { | |||
| public event AsyncEventHandler<ServerEventArgs> JoinedServer { add { _joinedServer.Add(value); } remove { _joinedServer.Remove(value); } } | |||
| private readonly AsyncEvent<ServerEventArgs> _joinedServer = new AsyncEvent<ServerEventArgs>(nameof(JoinedServer)); | |||
| private Task RaiseJoinedServer(Server server) | |||
| => RaiseEvent(_joinedServer, new ServerEventArgs(server)); | |||
| public event AsyncEventHandler<ServerEventArgs> LeftServer { add { _leftServer.Add(value); } remove { _leftServer.Remove(value); } } | |||
| private readonly AsyncEvent<ServerEventArgs> _leftServer = new AsyncEvent<ServerEventArgs>(nameof(LeftServer)); | |||
| private Task RaiseLeftServer(Server server) | |||
| => RaiseEvent(_leftServer, new ServerEventArgs(server)); | |||
| public event AsyncEventHandler<ServerEventArgs> ServerUpdated { add { _serverUpdated.Add(value); } remove { _serverUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<ServerEventArgs> _serverUpdated = new AsyncEvent<ServerEventArgs>(nameof(ServerUpdated)); | |||
| private Task RaiseServerUpdated(Server server) | |||
| => RaiseEvent(_serverUpdated, new ServerEventArgs(server)); | |||
| public event AsyncEventHandler<ServerEventArgs> ServerAvailable { add { _serverAvailable.Add(value); } remove { _serverAvailable.Remove(value); } } | |||
| private readonly AsyncEvent<ServerEventArgs> _serverAvailable = new AsyncEvent<ServerEventArgs>(nameof(ServerAvailable)); | |||
| private Task RaiseServerAvailable(Server server) | |||
| => RaiseEvent(_serverAvailable, new ServerEventArgs(server)); | |||
| public event AsyncEventHandler<ServerEventArgs> ServerUnavailable { add { _serverUnavailable.Add(value); } remove { _serverUnavailable.Remove(value); } } | |||
| private readonly AsyncEvent<ServerEventArgs> _serverUnavailable = new AsyncEvent<ServerEventArgs>(nameof(ServerUnavailable)); | |||
| private Task RaiseServerUnavailable(Server server) | |||
| => RaiseEvent(_serverUnavailable, new ServerEventArgs(server)); | |||
| public event EventHandler<ServerEventArgs> JoinedServer; | |||
| private void RaiseJoinedServer(Server server) | |||
| { | |||
| if (JoinedServer != null) | |||
| RaiseEvent(nameof(JoinedServer), () => JoinedServer(this, new ServerEventArgs(server))); | |||
| } | |||
| public event EventHandler<ServerEventArgs> LeftServer; | |||
| private void RaiseLeftServer(Server server) | |||
| { | |||
| if (LeftServer != null) | |||
| RaiseEvent(nameof(LeftServer), () => LeftServer(this, new ServerEventArgs(server))); | |||
| } | |||
| public event EventHandler<ServerEventArgs> ServerUpdated; | |||
| private void RaiseServerUpdated(Server server) | |||
| { | |||
| if (ServerUpdated != null) | |||
| RaiseEvent(nameof(ServerUpdated), () => ServerUpdated(this, new ServerEventArgs(server))); | |||
| } | |||
| public event EventHandler<ServerEventArgs> ServerUnavailable; | |||
| private void RaiseServerUnavailable(Server server) | |||
| { | |||
| if (ServerUnavailable != null) | |||
| RaiseEvent(nameof(ServerUnavailable), () => ServerUnavailable(this, new ServerEventArgs(server))); | |||
| } | |||
| public event EventHandler<ServerEventArgs> ServerAvailable; | |||
| private void RaiseServerAvailable(Server server) | |||
| { | |||
| if (ServerAvailable != null) | |||
| RaiseEvent(nameof(ServerAvailable), () => ServerAvailable(this, new ServerEventArgs(server))); | |||
| } | |||
| /// <summary> Returns a collection of all servers this client is a member of. </summary> | |||
| public IEnumerable<Server> AllServers { get { CheckReady(); return _servers; } } | |||
| @@ -45,16 +45,6 @@ namespace Discord | |||
| Channel = channel; | |||
| } | |||
| } | |||
| public class UserIsTypingEventArgs : UserChannelEventArgs | |||
| { | |||
| public bool IsTyping { get; } | |||
| public UserIsTypingEventArgs(User user, Channel channel, bool isTyping) | |||
| : base(user, channel) | |||
| { | |||
| IsTyping = isTyping; | |||
| } | |||
| } | |||
| public class UserIsSpeakingEventArgs : UserChannelEventArgs | |||
| { | |||
| public bool IsSpeaking { get; } | |||
| @@ -79,55 +69,66 @@ namespace Discord | |||
| public partial class DiscordClient | |||
| { | |||
| public event AsyncEventHandler<UserEventArgs> UserJoined { add { _userJoined.Add(value); } remove { _userJoined.Remove(value); } } | |||
| private readonly AsyncEvent<UserEventArgs> _userJoined = new AsyncEvent<UserEventArgs>(nameof(UserJoined)); | |||
| private Task RaiseUserJoined(User user) | |||
| => RaiseEvent(_userJoined, new UserEventArgs(user)); | |||
| public event AsyncEventHandler<UserEventArgs> UserLeft { add { _userLeft.Add(value); } remove { _userLeft.Remove(value); } } | |||
| private readonly AsyncEvent<UserEventArgs> _userLeft = new AsyncEvent<UserEventArgs>(nameof(UserLeft)); | |||
| private Task RaiseUserLeft(User user) | |||
| => RaiseEvent(_userLeft, new UserEventArgs(user)); | |||
| public event AsyncEventHandler<UserEventArgs> UserUpdated { add { _userUpdated.Add(value); } remove { _userUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<UserEventArgs> _userUpdated = new AsyncEvent<UserEventArgs>(nameof(UserUpdated)); | |||
| private Task RaiseUserUpdated(User user) | |||
| => RaiseEvent(_userUpdated, new UserEventArgs(user)); | |||
| public event AsyncEventHandler<UserEventArgs> UserPresenceUpdated { add { _userPresenceUpdated.Add(value); } remove { _userPresenceUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<UserEventArgs> _userPresenceUpdated = new AsyncEvent<UserEventArgs>(nameof(UserPresenceUpdated)); | |||
| private Task RaiseUserPresenceUpdated(User user) | |||
| => RaiseEvent(_userPresenceUpdated, new UserEventArgs(user)); | |||
| public event AsyncEventHandler<UserEventArgs> UserVoiceStateUpdated { add { _userVoiceStateUpdated.Add(value); } remove { _userVoiceStateUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<UserEventArgs> _userVoiceStateUpdated = new AsyncEvent<UserEventArgs>(nameof(UserVoiceStateUpdated)); | |||
| private Task RaiseUserVoiceStateUpdated(User user) | |||
| => RaiseEvent(_userVoiceStateUpdated, new UserEventArgs(user)); | |||
| public event AsyncEventHandler<UserIsTypingEventArgs> UserIsTypingUpdated { add { _userIsTypingUpdated.Add(value); } remove { _userIsTypingUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<UserIsTypingEventArgs> _userIsTypingUpdated = new AsyncEvent<UserIsTypingEventArgs>(nameof(UserIsTypingUpdated)); | |||
| private Task RaiseUserIsTypingUpdated(User user, Channel channel, bool isTyping) | |||
| => RaiseEvent(_userIsTypingUpdated, new UserIsTypingEventArgs(user, channel, isTyping)); | |||
| public event AsyncEventHandler<UserIsSpeakingEventArgs> UserIsSpeakingUpdated { add { _userIsSpeakingUpdated.Add(value); } remove { _userIsSpeakingUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<UserIsSpeakingEventArgs> _userIsSpeakingUpdated = new AsyncEvent<UserIsSpeakingEventArgs>(nameof(UserIsSpeakingUpdated)); | |||
| private Task RaiseUserIsSpeakingUpdated(User user, Channel channel, bool isSpeaking) | |||
| => RaiseEvent(_userIsSpeakingUpdated, new UserIsSpeakingEventArgs(user, channel, isSpeaking)); | |||
| public event AsyncEventHandler<EventArgs> ProfileUpdated { add { _profileUpdated.Add(value); } remove { _profileUpdated.Remove(value); } } | |||
| private readonly AsyncEvent<EventArgs> _profileUpdated = new AsyncEvent<EventArgs>(nameof(ProfileUpdated)); | |||
| private Task RaiseProfileUpdated() | |||
| => RaiseEvent(_profileUpdated, EventArgs.Empty); | |||
| public event AsyncEventHandler<BanEventArgs> UserBanned { add { _userBanned.Add(value); } remove { _userBanned.Remove(value); } } | |||
| private readonly AsyncEvent<BanEventArgs> _userBanned = new AsyncEvent<BanEventArgs>(nameof(UserBanned)); | |||
| private Task RaiseUserBanned(long userId, Server server) | |||
| => RaiseEvent(_userBanned, new BanEventArgs(userId, server)); | |||
| public event AsyncEventHandler<BanEventArgs> UserUnbanned { add { _userUnbanned.Add(value); } remove { _userUnbanned.Remove(value); } } | |||
| private readonly AsyncEvent<BanEventArgs> _userUnbanned = new AsyncEvent<BanEventArgs>(nameof(UserUnbanned)); | |||
| private Task RaiseUserUnbanned(long userId, Server server) | |||
| => RaiseEvent(_userUnbanned, new BanEventArgs(userId, server)); | |||
| public event EventHandler<UserEventArgs> UserJoined; | |||
| private void RaiseUserJoined(User user) | |||
| { | |||
| if (UserJoined != null) | |||
| RaiseEvent(nameof(UserJoined), () => UserJoined(this, new UserEventArgs(user))); | |||
| } | |||
| public event EventHandler<UserEventArgs> UserLeft; | |||
| private void RaiseUserLeft(User user) | |||
| { | |||
| if (UserLeft != null) | |||
| RaiseEvent(nameof(UserLeft), () => UserLeft(this, new UserEventArgs(user))); | |||
| } | |||
| public event EventHandler<UserEventArgs> UserUpdated; | |||
| private void RaiseUserUpdated(User user) | |||
| { | |||
| if (UserUpdated != null) | |||
| RaiseEvent(nameof(UserUpdated), () => UserUpdated(this, new UserEventArgs(user))); | |||
| } | |||
| public event EventHandler<UserEventArgs> UserPresenceUpdated; | |||
| private void RaiseUserPresenceUpdated(User user) | |||
| { | |||
| if (UserPresenceUpdated != null) | |||
| RaiseEvent(nameof(UserPresenceUpdated), () => UserPresenceUpdated(this, new UserEventArgs(user))); | |||
| } | |||
| public event EventHandler<UserEventArgs> UserVoiceStateUpdated; | |||
| private void RaiseUserVoiceStateUpdated(User user) | |||
| { | |||
| if (UserVoiceStateUpdated != null) | |||
| RaiseEvent(nameof(UserVoiceStateUpdated), () => UserVoiceStateUpdated(this, new UserEventArgs(user))); | |||
| } | |||
| public event EventHandler<UserChannelEventArgs> UserIsTypingUpdated; | |||
| private void RaiseUserIsTyping(User user, Channel channel) | |||
| { | |||
| if (UserIsTypingUpdated != null) | |||
| RaiseEvent(nameof(UserIsTypingUpdated), () => UserIsTypingUpdated(this, new UserChannelEventArgs(user, channel))); | |||
| } | |||
| public event EventHandler<UserIsSpeakingEventArgs> UserIsSpeakingUpdated; | |||
| private void RaiseUserIsSpeaking(User user, Channel channel, bool isSpeaking) | |||
| { | |||
| if (UserIsSpeakingUpdated != null) | |||
| RaiseEvent(nameof(UserIsSpeakingUpdated), () => UserIsSpeakingUpdated(this, new UserIsSpeakingEventArgs(user, channel, isSpeaking))); | |||
| } | |||
| public event EventHandler ProfileUpdated; | |||
| private void RaiseProfileUpdated() | |||
| { | |||
| if (ProfileUpdated != null) | |||
| RaiseEvent(nameof(ProfileUpdated), () => ProfileUpdated(this, EventArgs.Empty)); | |||
| } | |||
| public event EventHandler<BanEventArgs> UserBanned; | |||
| private void RaiseUserBanned(long userId, Server server) | |||
| { | |||
| if (UserBanned != null) | |||
| RaiseEvent(nameof(UserBanned), () => UserBanned(this, new BanEventArgs(userId, server))); | |||
| } | |||
| public event EventHandler<BanEventArgs> UserUnbanned; | |||
| private void RaiseUserUnbanned(long userId, Server server) | |||
| { | |||
| if (UserUnbanned != null) | |||
| RaiseEvent(nameof(UserUnbanned), () => UserUnbanned(this, new BanEventArgs(userId, server))); | |||
| } | |||
| /// <summary> Returns the current logged-in user in a private channel. </summary> | |||
| internal User PrivateUser => _privateUser; | |||
| @@ -43,8 +43,7 @@ namespace Discord | |||
| client.LogMessage += (s, e) => | |||
| { | |||
| if (e.Source != LogMessageSource.DataWebSocket) | |||
| return RaiseLogMessage(e.Severity, e.Source, $"(#{client.Config.VoiceClientId}) {e.Message}", e.Exception); | |||
| return TaskHelper.CompletedTask; | |||
| RaiseOnLog(e.Severity, e.Source, $"(#{client.Config.VoiceClientId}) {e.Message}", e.Exception); | |||
| }; | |||
| await client.Connect(_gateway, _token).ConfigureAwait(false); | |||
| return client; | |||
| @@ -60,7 +60,7 @@ namespace Discord | |||
| await SendStatus().ConfigureAwait(false); | |||
| }; | |||
| VoiceDisconnected += async (s, e) => | |||
| VoiceDisconnected += (s, e) => | |||
| { | |||
| var server = _servers[e.ServerId]; | |||
| if (server != null) | |||
| @@ -70,7 +70,7 @@ namespace Discord | |||
| if (member.IsSpeaking) | |||
| { | |||
| member.IsSpeaking = false; | |||
| await RaiseUserIsSpeakingUpdated(member, _channels[_voiceSocket.CurrentChannelId], false); | |||
| RaiseUserIsSpeaking(member, _channels[_voiceSocket.CurrentChannelId], false); | |||
| } | |||
| } | |||
| } | |||
| @@ -78,89 +78,89 @@ namespace Discord | |||
| if (_config.LogLevel >= LogMessageSeverity.Info) | |||
| { | |||
| JoinedServer += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| JoinedServer += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Server Created: {e.Server?.Name ?? "[Private]"}"); | |||
| LeftServer += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| LeftServer += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Server Destroyed: {e.Server?.Name ?? "[Private]"}"); | |||
| ServerUpdated += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| ServerUpdated += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Server Updated: {e.Server?.Name ?? "[Private]"}"); | |||
| ServerAvailable += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| ServerAvailable += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Server Available: {e.Server?.Name ?? "[Private]"}"); | |||
| ServerUnavailable += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| ServerUnavailable += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Server Unavailable: {e.Server?.Name ?? "[Private]"}"); | |||
| ChannelCreated += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| ChannelCreated += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Channel Created: {e.Server?.Name ?? "[Private]"}/{e.Channel?.Name}"); | |||
| ChannelDestroyed += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| ChannelDestroyed += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Channel Destroyed: {e.Server?.Name ?? "[Private]"}/{e.Channel?.Name}"); | |||
| ChannelUpdated += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| ChannelUpdated += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Channel Updated: {e.Server?.Name ?? "[Private]"}/{e.Channel?.Name}"); | |||
| MessageReceived += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| MessageReceived += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Message Received: {e.Server?.Name ?? "[Private]"}/{e.Channel?.Name}/{e.Message?.Id}"); | |||
| MessageDeleted += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| MessageDeleted += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Message Deleted: {e.Server?.Name ?? "[Private]"}/{e.Channel?.Name}/{e.Message?.Id}"); | |||
| MessageUpdated += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| MessageUpdated += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Message Update: {e.Server?.Name ?? "[Private]"}/{e.Channel?.Name}/{e.Message?.Id}"); | |||
| RoleCreated += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| RoleCreated += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Role Created: {e.Server?.Name ?? "[Private]"}/{e.Role?.Name}"); | |||
| RoleUpdated += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| RoleUpdated += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Role Updated: {e.Server?.Name ?? "[Private]"}/{e.Role?.Name}"); | |||
| RoleDeleted += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| RoleDeleted += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Role Deleted: {e.Server?.Name ?? "[Private]"}/{e.Role?.Name}"); | |||
| UserBanned += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| UserBanned += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Banned User: {e.Server?.Name ?? "[Private]" }/{e.UserId}"); | |||
| UserUnbanned += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| UserUnbanned += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"Unbanned User: {e.Server?.Name ?? "[Private]"}/{e.UserId}"); | |||
| UserJoined += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| UserJoined += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"User Joined: {e.Server?.Name ?? "[Private]"}/{e.User.Name}"); | |||
| UserLeft += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| UserLeft += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"User Left: {e.Server?.Name ?? "[Private]"}/{e.User.Name}"); | |||
| UserUpdated += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| UserUpdated += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"User Updated: {e.Server?.Name ?? "[Private]"}/{e.User.Name}"); | |||
| UserVoiceStateUpdated += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| UserVoiceStateUpdated += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| $"User Updated (Voice State): {e.Server?.Name ?? "[Private]"}/{e.User.Name}"); | |||
| ProfileUpdated += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| ProfileUpdated += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.Client, | |||
| "Profile Updated"); | |||
| } | |||
| if (_config.LogLevel >= LogMessageSeverity.Verbose) | |||
| { | |||
| UserIsTypingUpdated += (s, e) => RaiseLogMessage(LogMessageSeverity.Verbose, LogMessageSource.Client, | |||
| UserIsTypingUpdated += (s, e) => RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Client, | |||
| $"User Updated (Is Typing): {e.Server?.Name ?? "[Private]"}/{e.Channel?.Name}/{e.User?.Name}"); | |||
| MessageReadRemotely += (s, e) => RaiseLogMessage(LogMessageSeverity.Verbose, LogMessageSource.Client, | |||
| MessageReadRemotely += (s, e) => RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Client, | |||
| $"Read Message (Remotely): {e.Server?.Name ?? "[Private]"}/{e.Channel?.Name}/{e.Message?.Id}"); | |||
| MessageSent += (s, e) => RaiseLogMessage(LogMessageSeverity.Verbose, LogMessageSource.Client, | |||
| MessageSent += (s, e) => RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Client, | |||
| $"Sent Message: {e.Server?.Name ?? "[Private]"}/{e.Channel?.Name}/{e.Message?.Id}"); | |||
| UserPresenceUpdated += (s, e) => RaiseLogMessage(LogMessageSeverity.Verbose, LogMessageSource.Client, | |||
| UserPresenceUpdated += (s, e) => RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Client, | |||
| $"User Updated (Presence): {e.Server?.Name ?? "[Private]"}/{e.User?.Name}"); | |||
| _api.RestClient.OnRequest += (s, e) => | |||
| { | |||
| if (e.Payload != null) | |||
| RaiseLogMessage(LogMessageSeverity.Verbose, LogMessageSource.Rest, $"{e.Method} {e.Path}: {Math.Round(e.ElapsedMilliseconds, 2)} ms ({e.Payload})"); | |||
| RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Rest, $"{e.Method} {e.Path}: {Math.Round(e.ElapsedMilliseconds, 2)} ms ({e.Payload})"); | |||
| else | |||
| RaiseLogMessage(LogMessageSeverity.Verbose, LogMessageSource.Rest, $"{e.Method} {e.Path}: {Math.Round(e.ElapsedMilliseconds, 2)} ms"); | |||
| RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Rest, $"{e.Method} {e.Path}: {Math.Round(e.ElapsedMilliseconds, 2)} ms"); | |||
| }; | |||
| } | |||
| if (_config.LogLevel >= LogMessageSeverity.Debug) | |||
| { | |||
| _channels.ItemCreated += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created Channel {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _channels.ItemDestroyed += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed Channel {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _channels.Cleared += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Channels"); | |||
| _users.ItemCreated += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created User {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _users.ItemDestroyed += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed User {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _users.Cleared += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Users"); | |||
| _messages.ItemCreated += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created Message {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Channel.Id}/{e.Item.Id}"); | |||
| _messages.ItemDestroyed += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed Message {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Channel.Id}/{e.Item.Id}"); | |||
| _messages.ItemRemapped += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Remapped Message {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Channel.Id}/[{e.OldId} -> {e.NewId}]"); | |||
| _messages.Cleared += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Messages"); | |||
| _roles.ItemCreated += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created Role {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _roles.ItemDestroyed += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed Role {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _roles.Cleared += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Roles"); | |||
| _servers.ItemCreated += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created Server {e.Item.Id}"); | |||
| _servers.ItemDestroyed += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed Server {e.Item.Id}"); | |||
| _servers.Cleared += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Servers"); | |||
| _globalUsers.ItemCreated += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created User {e.Item.Id}"); | |||
| _globalUsers.ItemDestroyed += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed User {e.Item.Id}"); | |||
| _globalUsers.Cleared += (s, e) => RaiseLogMessage(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Users"); | |||
| _channels.ItemCreated += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created Channel {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _channels.ItemDestroyed += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed Channel {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _channels.Cleared += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Channels"); | |||
| _users.ItemCreated += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created User {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _users.ItemDestroyed += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed User {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _users.Cleared += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Users"); | |||
| _messages.ItemCreated += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created Message {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Channel.Id}/{e.Item.Id}"); | |||
| _messages.ItemDestroyed += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed Message {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Channel.Id}/{e.Item.Id}"); | |||
| _messages.ItemRemapped += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Remapped Message {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Channel.Id}/[{e.OldId} -> {e.NewId}]"); | |||
| _messages.Cleared += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Messages"); | |||
| _roles.ItemCreated += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created Role {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _roles.ItemDestroyed += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed Role {IdConvert.ToString(e.Item.Server?.Id) ?? "[Private]"}/{e.Item.Id}"); | |||
| _roles.Cleared += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Roles"); | |||
| _servers.ItemCreated += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created Server {e.Item.Id}"); | |||
| _servers.ItemDestroyed += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed Server {e.Item.Id}"); | |||
| _servers.Cleared += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Servers"); | |||
| _globalUsers.ItemCreated += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Created User {e.Item.Id}"); | |||
| _globalUsers.ItemDestroyed += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Destroyed User {e.Item.Id}"); | |||
| _globalUsers.Cleared += (s, e) => RaiseOnLog(LogMessageSeverity.Debug, LogMessageSource.Cache, $"Cleared Users"); | |||
| } | |||
| if (Config.UseMessageQueue) | |||
| @@ -172,7 +172,7 @@ namespace Discord | |||
| internal override VoiceWebSocket CreateVoiceSocket() | |||
| { | |||
| var socket = base.CreateVoiceSocket(); | |||
| socket.IsSpeaking += async (s, e) => | |||
| socket.IsSpeaking += (s, e) => | |||
| { | |||
| if (_voiceSocket.State == WebSocketState.Connected) | |||
| { | |||
| @@ -182,7 +182,7 @@ namespace Discord | |||
| { | |||
| user.IsSpeaking = value; | |||
| var channel = _channels[_voiceSocket.CurrentChannelId]; | |||
| await RaiseUserIsSpeakingUpdated(user, channel, value); | |||
| RaiseUserIsSpeaking(user, channel, value); | |||
| if (Config.TrackActivity) | |||
| user.UpdateActivity(); | |||
| } | |||
| @@ -209,7 +209,7 @@ namespace Discord | |||
| .ConfigureAwait(false); | |||
| token = response.Token; | |||
| if (_config.LogLevel >= LogMessageSeverity.Verbose) | |||
| await RaiseLogMessage(LogMessageSeverity.Verbose, LogMessageSource.Client, "Login successful, got token."); | |||
| RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Client, "Login successful, got token."); | |||
| } | |||
| catch (TaskCanceledException) { throw new TimeoutException(); } | |||
| @@ -231,7 +231,7 @@ namespace Discord | |||
| .ConfigureAwait(false) | |||
| ).Url; | |||
| if (_config.LogLevel >= LogMessageSeverity.Verbose) | |||
| await RaiseLogMessage(LogMessageSeverity.Verbose, LogMessageSource.Client, $"Websocket endpoint: {gateway}"); | |||
| RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Client, $"Websocket endpoint: {gateway}"); | |||
| await base.Connect(gateway, token) | |||
| .Timeout(_config.ConnectionTimeout) | |||
| @@ -350,9 +350,9 @@ namespace Discord | |||
| var server = _servers.GetOrAdd(data.Id); | |||
| server.Update(data); | |||
| if (data.Unavailable == false) | |||
| await RaiseServerAvailable(server); | |||
| RaiseServerAvailable(server); | |||
| else | |||
| await RaiseJoinedServer(server); | |||
| RaiseJoinedServer(server); | |||
| } | |||
| } | |||
| break; | |||
| @@ -363,7 +363,7 @@ namespace Discord | |||
| if (server != null) | |||
| { | |||
| server.Update(data); | |||
| await RaiseServerUpdated(server); | |||
| RaiseServerUpdated(server); | |||
| } | |||
| } | |||
| break; | |||
| @@ -374,9 +374,9 @@ namespace Discord | |||
| if (server != null) | |||
| { | |||
| if (data.Unavailable == true) | |||
| await RaiseServerUnavailable(server); | |||
| RaiseServerUnavailable(server); | |||
| else | |||
| await RaiseLeftServer(server); | |||
| RaiseLeftServer(server); | |||
| } | |||
| } | |||
| break; | |||
| @@ -395,7 +395,7 @@ namespace Discord | |||
| else | |||
| channel = _channels.GetOrAdd(data.Id, data.GuildId, null); | |||
| channel.Update(data); | |||
| await RaiseChannelCreated(channel); | |||
| RaiseChannelCreated(channel); | |||
| } | |||
| break; | |||
| case "CHANNEL_UPDATE": | |||
| @@ -405,7 +405,7 @@ namespace Discord | |||
| if (channel != null) | |||
| { | |||
| channel.Update(data); | |||
| await RaiseChannelUpdated(channel); | |||
| RaiseChannelUpdated(channel); | |||
| } | |||
| } | |||
| break; | |||
| @@ -414,7 +414,7 @@ namespace Discord | |||
| var data = e.Payload.ToObject<ChannelDeleteEvent>(_dataSocketSerializer); | |||
| var channel = _channels.TryRemove(data.Id); | |||
| if (channel != null) | |||
| await RaiseChannelDestroyed(channel); | |||
| RaiseChannelDestroyed(channel); | |||
| } | |||
| break; | |||
| @@ -426,7 +426,7 @@ namespace Discord | |||
| user.Update(data); | |||
| if (Config.TrackActivity) | |||
| user.UpdateActivity(); | |||
| await RaiseUserJoined(user); | |||
| RaiseUserJoined(user); | |||
| } | |||
| break; | |||
| case "GUILD_MEMBER_UPDATE": | |||
| @@ -436,7 +436,7 @@ namespace Discord | |||
| if (user != null) | |||
| { | |||
| user.Update(data); | |||
| await RaiseUserUpdated(user); | |||
| RaiseUserUpdated(user); | |||
| } | |||
| } | |||
| break; | |||
| @@ -445,7 +445,7 @@ namespace Discord | |||
| var data = e.Payload.ToObject<MemberRemoveEvent>(_dataSocketSerializer); | |||
| var user = _users.TryRemove(data.UserId, data.GuildId); | |||
| if (user != null) | |||
| await RaiseUserLeft(user); | |||
| RaiseUserLeft(user); | |||
| } | |||
| break; | |||
| case "GUILD_MEMBERS_CHUNK": | |||
| @@ -455,7 +455,7 @@ namespace Discord | |||
| { | |||
| var user = _users.GetOrAdd(memberData.User.Id, memberData.GuildId); | |||
| user.Update(memberData); | |||
| //await RaiseUserAdded(user); | |||
| //RaiseUserAdded(user); | |||
| } | |||
| } | |||
| break; | |||
| @@ -469,7 +469,7 @@ namespace Discord | |||
| var server = _servers[data.GuildId]; | |||
| if (server != null) | |||
| server.AddRole(role); | |||
| await RaiseRoleUpdated(role); | |||
| RaiseRoleUpdated(role); | |||
| } | |||
| break; | |||
| case "GUILD_ROLE_UPDATE": | |||
| @@ -479,7 +479,7 @@ namespace Discord | |||
| if (role != null) | |||
| { | |||
| role.Update(data.Data); | |||
| await RaiseRoleUpdated(role); | |||
| RaiseRoleUpdated(role); | |||
| } | |||
| } | |||
| break; | |||
| @@ -489,7 +489,7 @@ namespace Discord | |||
| var role = _roles.TryRemove(data.RoleId); | |||
| if (role != null) | |||
| { | |||
| await RaiseRoleDeleted(role); | |||
| RaiseRoleDeleted(role); | |||
| var server = _servers[data.GuildId]; | |||
| if (server != null) | |||
| server.RemoveRole(role); | |||
| @@ -506,7 +506,7 @@ namespace Discord | |||
| { | |||
| var id = data.User?.Id ?? data.UserId; | |||
| server.AddBan(id); | |||
| await RaiseUserBanned(id, server); | |||
| RaiseUserBanned(id, server); | |||
| } | |||
| } | |||
| break; | |||
| @@ -518,7 +518,7 @@ namespace Discord | |||
| { | |||
| var id = data.User?.Id ?? data.UserId; | |||
| if (server.RemoveBan(id)) | |||
| await RaiseUserUnbanned(id, server); | |||
| RaiseUserUnbanned(id, server); | |||
| } | |||
| } | |||
| break; | |||
| @@ -545,7 +545,7 @@ namespace Discord | |||
| } | |||
| } | |||
| await RaiseMessageReceived(msg); | |||
| RaiseMessageReceived(msg); | |||
| if (Config.AckMessages && !isAuthor) | |||
| await _api.AckMessage(data.Id, data.ChannelId).ConfigureAwait(false); | |||
| @@ -558,7 +558,7 @@ namespace Discord | |||
| if (msg != null) | |||
| { | |||
| msg.Update(data); | |||
| await RaiseMessageUpdated(msg); | |||
| RaiseMessageUpdated(msg); | |||
| } | |||
| } | |||
| break; | |||
| @@ -567,7 +567,7 @@ namespace Discord | |||
| var data = e.Payload.ToObject<MessageDeleteEvent>(_dataSocketSerializer); | |||
| var msg = _messages.TryRemove(data.Id); | |||
| if (msg != null) | |||
| await RaiseMessageDeleted(msg); | |||
| RaiseMessageDeleted(msg); | |||
| } | |||
| break; | |||
| case "MESSAGE_ACK": | |||
| @@ -575,7 +575,7 @@ namespace Discord | |||
| var data = e.Payload.ToObject<MessageAckEvent>(_dataSocketSerializer); | |||
| var msg = GetMessage(data.MessageId); | |||
| if (msg != null) | |||
| await RaiseMessageReadRemotely(msg); | |||
| RaiseMessageReadRemotely(msg); | |||
| } | |||
| break; | |||
| @@ -587,7 +587,7 @@ namespace Discord | |||
| if (user != null) | |||
| { | |||
| user.Update(data); | |||
| await RaiseUserPresenceUpdated(user); | |||
| RaiseUserPresenceUpdated(user); | |||
| } | |||
| } | |||
| break; | |||
| @@ -601,7 +601,7 @@ namespace Discord | |||
| if (user != null) | |||
| { | |||
| if (channel != null) | |||
| await RaiseUserIsTypingUpdated(user, channel, true); | |||
| RaiseUserIsTyping(user, channel); | |||
| } | |||
| if (Config.TrackActivity) | |||
| @@ -627,10 +627,10 @@ namespace Discord | |||
| if (voiceChannel != null && data.ChannelId != voiceChannel.Id && user.IsSpeaking) | |||
| { | |||
| user.IsSpeaking = false; | |||
| await RaiseUserIsSpeakingUpdated(user, _channels[voiceChannel.Id], false); | |||
| RaiseUserIsSpeaking(user, _channels[voiceChannel.Id], false); | |||
| } | |||
| user.Update(data); | |||
| await RaiseUserVoiceStateUpdated(user); | |||
| RaiseUserVoiceStateUpdated(user); | |||
| } | |||
| } | |||
| break; | |||
| @@ -643,7 +643,7 @@ namespace Discord | |||
| if (user != null) | |||
| { | |||
| user.Update(data); | |||
| await RaiseProfileUpdated(); | |||
| RaiseProfileUpdated(); | |||
| } | |||
| } | |||
| break; | |||
| @@ -664,20 +664,20 @@ namespace Discord | |||
| //Others | |||
| default: | |||
| await RaiseLogMessage(LogMessageSeverity.Warning, LogMessageSource.DataWebSocket, $"Unknown message type: {e.Type}"); | |||
| RaiseOnLog(LogMessageSeverity.Warning, LogMessageSource.DataWebSocket, $"Unknown message type: {e.Type}"); | |||
| break; | |||
| } | |||
| } | |||
| catch (Exception ex) | |||
| { | |||
| await RaiseLogMessage(LogMessageSeverity.Error, LogMessageSource.Client, $"Error handling {e.Type} event: {ex.GetBaseException().Message}"); | |||
| RaiseOnLog(LogMessageSeverity.Error, LogMessageSource.Client, $"Error handling {e.Type} event: {ex.GetBaseException().Message}"); | |||
| } | |||
| } | |||
| private void SendInitialLog() | |||
| { | |||
| if (_config.LogLevel >= LogMessageSeverity.Verbose) | |||
| RaiseLogMessage(LogMessageSeverity.Verbose, LogMessageSource.Client, $"Config: {JsonConvert.SerializeObject(_config)}"); | |||
| RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Client, $"Config: {JsonConvert.SerializeObject(_config)}"); | |||
| _sentInitialLog = true; | |||
| } | |||
| @@ -1,5 +1,4 @@ | |||
| using System; | |||
| using System.Threading.Tasks; | |||
| namespace Discord | |||
| { | |||
| @@ -33,15 +32,6 @@ namespace Discord | |||
| Error = error; | |||
| } | |||
| } | |||
| public class VoiceConnectedEventArgs : EventArgs | |||
| { | |||
| public readonly long ServerId; | |||
| internal VoiceConnectedEventArgs(long serverId) | |||
| { | |||
| ServerId = serverId; | |||
| } | |||
| } | |||
| public class VoiceDisconnectedEventArgs : DisconnectedEventArgs | |||
| { | |||
| public readonly long ServerId; | |||
| @@ -68,7 +58,7 @@ namespace Discord | |||
| } | |||
| } | |||
| public sealed class VoicePacketEventArgs : EventArgs | |||
| public sealed class VoicePacketEventArgs | |||
| { | |||
| public long UserId { get; } | |||
| public long ChannelId { get; } | |||
| @@ -87,34 +77,43 @@ namespace Discord | |||
| public partial class DiscordWSClient | |||
| { | |||
| public event AsyncEventHandler<EventArgs> Connected { add { _connected.Add(value); } remove { _connected.Remove(value); } } | |||
| private readonly AsyncEvent<EventArgs> _connected = new AsyncEvent<EventArgs>(nameof(Connected)); | |||
| protected Task RaiseConnected() | |||
| => RaiseEvent(_connected, EventArgs.Empty); | |||
| public event AsyncEventHandler<DisconnectedEventArgs> Disconnected { add { _disconnected.Add(value); } remove { _disconnected.Remove(value); } } | |||
| private readonly AsyncEvent<DisconnectedEventArgs> _disconnected = new AsyncEvent<DisconnectedEventArgs>(nameof(Disconnected)); | |||
| protected Task RaiseDisconnected(DisconnectedEventArgs e) | |||
| => RaiseEvent(_disconnected, e); | |||
| public event AsyncEventHandler<VoiceConnectedEventArgs> VoiceConnected { add { _voiceConnected.Add(value); } remove { _voiceConnected.Remove(value); } } | |||
| private readonly AsyncEvent<VoiceConnectedEventArgs> _voiceConnected = new AsyncEvent<VoiceConnectedEventArgs>(nameof(VoiceConnected)); | |||
| protected Task RaiseVoiceConnected(long serverId) | |||
| => RaiseEvent(_voiceConnected, new VoiceConnectedEventArgs(serverId)); | |||
| public event AsyncEventHandler<VoiceDisconnectedEventArgs> VoiceDisconnected { add { _voiceDisconnected.Add(value); } remove { _voiceDisconnected.Remove(value); } } | |||
| private readonly AsyncEvent<VoiceDisconnectedEventArgs> _voiceDisconnected = new AsyncEvent<VoiceDisconnectedEventArgs>(nameof(VoiceDisconnected)); | |||
| protected Task RaiseVoiceDisconnected(long serverId, DisconnectedEventArgs e) | |||
| => RaiseEvent(_voiceDisconnected, new VoiceDisconnectedEventArgs(serverId, e)); | |||
| public event EventHandler Connected; | |||
| private void RaiseConnected() | |||
| { | |||
| if (Connected != null) | |||
| RaiseEvent(nameof(Connected), () => Connected(this, EventArgs.Empty)); | |||
| } | |||
| public event EventHandler<DisconnectedEventArgs> Disconnected; | |||
| private void RaiseDisconnected(DisconnectedEventArgs e) | |||
| { | |||
| if (Disconnected != null) | |||
| RaiseEvent(nameof(Disconnected), () => Disconnected(this, e)); | |||
| } | |||
| public event EventHandler<LogMessageEventArgs> LogMessage; | |||
| internal void RaiseOnLog(LogMessageSeverity severity, LogMessageSource source, string message, Exception exception = null) | |||
| { | |||
| if (LogMessage != null) | |||
| RaiseEvent(nameof(LogMessage), () => LogMessage(this, new LogMessageEventArgs(severity, source, message, exception))); | |||
| } | |||
| public event AsyncEventHandler<LogMessageEventArgs> LogMessage { add { _logMessage.Add(value); } remove { _logMessage.Remove(value); } } | |||
| private readonly AsyncEvent<LogMessageEventArgs> _logMessage = new AsyncEvent<LogMessageEventArgs>(nameof(LogMessage)); | |||
| protected Task RaiseLogMessage(LogMessageSeverity severity, LogMessageSource source, string message, Exception exception = null) | |||
| => RaiseEvent(_logMessage, new LogMessageEventArgs(severity, source, message, exception)); | |||
| public event EventHandler VoiceConnected; | |||
| private void RaiseVoiceConnected() | |||
| { | |||
| if (VoiceConnected != null) | |||
| RaiseEvent(nameof(VoiceConnected), () => VoiceConnected(this, EventArgs.Empty)); | |||
| } | |||
| public event EventHandler<VoiceDisconnectedEventArgs> VoiceDisconnected; | |||
| private void RaiseVoiceDisconnected(long serverId, DisconnectedEventArgs e) | |||
| { | |||
| if (VoiceDisconnected != null) | |||
| RaiseEvent(nameof(VoiceDisconnected), () => VoiceDisconnected(this, new VoiceDisconnectedEventArgs(serverId, e))); | |||
| } | |||
| public event AsyncEventHandler<VoicePacketEventArgs> VoiceReceived { add { _voiceReceived.Add(value); } remove { _voiceReceived.Remove(value); } } | |||
| private readonly AsyncEvent<VoicePacketEventArgs> _voiceReceived = new AsyncEvent<VoicePacketEventArgs>(nameof(VoiceReceived)); | |||
| protected Task RaiseVoiceReceived(long userId, long channelId, byte[] buffer, int offset, int count) | |||
| => RaiseEvent(_voiceReceived, new VoicePacketEventArgs(userId, channelId, buffer, offset, count)); | |||
| public event EventHandler<VoicePacketEventArgs> OnVoicePacket; | |||
| internal void RaiseOnVoicePacket(VoicePacketEventArgs e) | |||
| { | |||
| if (OnVoicePacket != null) | |||
| OnVoicePacket(this, e); | |||
| } | |||
| } | |||
| } | |||
| @@ -68,7 +68,7 @@ namespace Discord | |||
| _dataSocketSerializer.Error += (s, e) => | |||
| { | |||
| e.ErrorContext.Handled = true; | |||
| RaiseLogMessage(LogMessageSeverity.Error, LogMessageSource.DataWebSocket, "Serialization Failed", e.ErrorContext.Error); | |||
| RaiseOnLog(LogMessageSeverity.Error, LogMessageSource.DataWebSocket, "Serialization Failed", e.ErrorContext.Error); | |||
| }; | |||
| #endif | |||
| @@ -81,7 +81,7 @@ namespace Discord | |||
| _voiceSocketSerializer.Error += (s, e) => | |||
| { | |||
| e.ErrorContext.Handled = true; | |||
| RaiseLogMessage(LogMessageSeverity.Error, LogMessageSource.VoiceWebSocket, "Serialization Failed", e.ErrorContext.Error); | |||
| RaiseOnLog(LogMessageSeverity.Error, LogMessageSource.VoiceWebSocket, "Serialization Failed", e.ErrorContext.Error); | |||
| }; | |||
| #endif | |||
| @@ -105,16 +105,16 @@ namespace Discord | |||
| ; | |||
| socket.Disconnected += async (s, e) => | |||
| { | |||
| await RaiseDisconnected(e); | |||
| RaiseDisconnected(e); | |||
| if (e.WasUnexpected) | |||
| await socket.Reconnect(_token).ConfigureAwait(false); | |||
| }; | |||
| socket.LogMessage += (s, e) => RaiseLogMessage(e.Severity, LogMessageSource.DataWebSocket, e.Message, e.Exception); | |||
| socket.LogMessage += (s, e) => RaiseOnLog(e.Severity, LogMessageSource.DataWebSocket, e.Message, e.Exception); | |||
| if (_config.LogLevel >= LogMessageSeverity.Info) | |||
| { | |||
| socket.Connected += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.DataWebSocket, "Connected"); | |||
| socket.Disconnected += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.DataWebSocket, "Disconnected"); | |||
| socket.Connected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.DataWebSocket, "Connected"); | |||
| socket.Disconnected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.DataWebSocket, "Disconnected"); | |||
| } | |||
| socket.ReceivedEvent += async (s, e) => await OnReceivedEvent(e).ConfigureAwait(false); | |||
| @@ -123,19 +123,19 @@ namespace Discord | |||
| internal virtual VoiceWebSocket CreateVoiceSocket() | |||
| { | |||
| var socket = new VoiceWebSocket(this); | |||
| socket.Connected += (s, e) => RaiseVoiceConnected(socket.CurrentServerId.Value); | |||
| socket.Connected += (s, e) => RaiseVoiceConnected(); | |||
| socket.Disconnected += async (s, e) => | |||
| { | |||
| await RaiseVoiceDisconnected(socket.CurrentServerId.Value, e); | |||
| RaiseVoiceDisconnected(socket.CurrentServerId.Value, e); | |||
| if (e.WasUnexpected) | |||
| await socket.Reconnect().ConfigureAwait(false); | |||
| }; | |||
| socket.LogMessage += (s, e) => RaiseLogMessage(e.Severity, LogMessageSource.VoiceWebSocket, e.Message, e.Exception); | |||
| socket.LogMessage += (s, e) => RaiseOnLog(e.Severity, LogMessageSource.VoiceWebSocket, e.Message, e.Exception); | |||
| if (_config.LogLevel >= LogMessageSeverity.Info) | |||
| { | |||
| socket.Connected += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.VoiceWebSocket, "Connected"); | |||
| socket.Disconnected += (s, e) => RaiseLogMessage(LogMessageSeverity.Info, LogMessageSource.VoiceWebSocket, "Disconnected"); | |||
| socket.Connected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.VoiceWebSocket, "Connected"); | |||
| socket.Disconnected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.VoiceWebSocket, "Disconnected"); | |||
| } | |||
| return socket; | |||
| @@ -314,15 +314,14 @@ namespace Discord | |||
| if (checkVoice && _config.VoiceMode == DiscordVoiceMode.Disabled) | |||
| throw new InvalidOperationException("Voice is not enabled for this client."); | |||
| } | |||
| internal async Task RaiseEvent<T>(AsyncEvent<T> eventHandler, T eventArgs) | |||
| where T : EventArgs | |||
| protected void RaiseEvent(string name, Action action) | |||
| { | |||
| try { await eventHandler.Invoke(this, eventArgs); } | |||
| try { action(); } | |||
| catch (Exception ex) | |||
| { | |||
| var ex2 = ex.GetBaseException(); | |||
| await RaiseLogMessage(LogMessageSeverity.Error, LogMessageSource.Client, | |||
| $"{eventHandler.Name}'s handler raised {ex2.GetType().Name}: ${ex2.Message}", ex); | |||
| RaiseOnLog(LogMessageSeverity.Error, LogMessageSource.Client, | |||
| $"{name}'s handler raised {ex2.GetType().Name}: ${ex2.Message}", ex); | |||
| } | |||
| } | |||
| @@ -351,7 +350,7 @@ namespace Discord | |||
| } | |||
| catch (Exception ex) | |||
| { | |||
| await RaiseLogMessage(LogMessageSeverity.Error, LogMessageSource.Client, $"Error handling {e.Type} event: {ex.GetBaseException().Message}"); | |||
| RaiseOnLog(LogMessageSeverity.Error, LogMessageSource.Client, $"Error handling {e.Type} event: {ex.GetBaseException().Message}"); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,85 +0,0 @@ | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Threading.Tasks; | |||
| namespace Discord | |||
| { | |||
| public delegate Task AsyncEventHandler<T>(object sender, T eventArgs); | |||
| public class AsyncEvent<T> | |||
| where T : EventArgs | |||
| { | |||
| private List<AsyncEventHandler<T>> _handlers; | |||
| private Dictionary<EventHandler<T>, AsyncEventHandler<T>> _mapping; | |||
| public bool Any { get; private set; } | |||
| public string Name { get; } | |||
| public AsyncEvent(string name) | |||
| { | |||
| Name = name; | |||
| _handlers = new List<AsyncEventHandler<T>>(); | |||
| _mapping = new Dictionary<EventHandler<T>, AsyncEventHandler<T>>(); | |||
| } | |||
| public void Add(AsyncEventHandler<T> handler) | |||
| { | |||
| lock (_handlers) | |||
| { | |||
| _handlers.Add(handler); | |||
| Any = true; | |||
| } | |||
| } | |||
| public void Add(EventHandler<T> handler) | |||
| { | |||
| lock (_handlers) | |||
| { | |||
| AsyncEventHandler<T> func = (s, e) => | |||
| { | |||
| handler(s, e); | |||
| return TaskHelper.CompletedTask; | |||
| }; | |||
| _mapping[handler] = func; | |||
| _handlers.Add(func); | |||
| Any = true; | |||
| } | |||
| } | |||
| public void Remove(AsyncEventHandler<T> handler) | |||
| { | |||
| lock (_handlers) | |||
| { | |||
| _handlers.Remove(handler); | |||
| Any = _handlers.Count != 0; | |||
| } | |||
| } | |||
| public void Remove(EventHandler<T> handler) | |||
| { | |||
| lock (_handlers) | |||
| { | |||
| AsyncEventHandler<T> func; | |||
| if (_mapping.TryGetValue(handler, out func)) | |||
| { | |||
| _handlers.Remove(func); | |||
| _mapping.Remove(handler); | |||
| } | |||
| Any = _handlers.Count != 0; | |||
| } | |||
| } | |||
| public void Clear() | |||
| { | |||
| lock (_handlers) | |||
| { | |||
| _handlers.Clear(); | |||
| _mapping.Clear(); | |||
| Any = false; | |||
| } | |||
| } | |||
| public Task Invoke(object sender, T args) | |||
| { | |||
| return Task.WhenAll(_handlers.Select(x => x(sender, args)).ToArray()); | |||
| } | |||
| } | |||
| } | |||
| @@ -7,11 +7,7 @@ namespace Discord | |||
| public static Task CompletedTask { get; } | |||
| static TaskHelper() | |||
| { | |||
| #if DOTNET5_4 | |||
| CompletedTask = Task.CompletedTask; | |||
| #else | |||
| CompletedTask = Task.Delay(0); | |||
| #endif | |||
| } | |||
| } | |||
| } | |||
| @@ -72,7 +72,7 @@ namespace Discord.Net.WebSockets | |||
| catch (OperationCanceledException) { throw; } | |||
| catch (Exception ex) | |||
| { | |||
| RaiseLogMessage(LogMessageSeverity.Error, $"Reconnect failed: {ex.GetBaseException().Message}"); | |||
| RaiseOnLog(LogMessageSeverity.Error, $"Reconnect failed: {ex.GetBaseException().Message}"); | |||
| //Net is down? We can keep trying to reconnect until the user runs Disconnect() | |||
| await Task.Delay(_client.Config.FailedReconnectDelay, cancelToken).ConfigureAwait(false); | |||
| } | |||
| @@ -117,14 +117,14 @@ namespace Discord.Net.WebSockets | |||
| { | |||
| Host = payload.Url; | |||
| if (_logLevel >= LogMessageSeverity.Info) | |||
| RaiseLogMessage(LogMessageSeverity.Info, "Redirected to " + payload.Url); | |||
| RaiseOnLog(LogMessageSeverity.Info, "Redirected to " + payload.Url); | |||
| await Redirect(payload.Url).ConfigureAwait(false); | |||
| } | |||
| } | |||
| break; | |||
| default: | |||
| if (_logLevel >= LogMessageSeverity.Warning) | |||
| RaiseLogMessage(LogMessageSeverity.Warning, $"Unknown Opcode: {opCode}"); | |||
| RaiseOnLog(LogMessageSeverity.Warning, $"Unknown Opcode: {opCode}"); | |||
| break; | |||
| } | |||
| } | |||
| @@ -107,7 +107,7 @@ namespace Discord.Net.WebSockets | |||
| catch (OperationCanceledException) { throw; } | |||
| catch (Exception ex) | |||
| { | |||
| RaiseLogMessage(LogMessageSeverity.Error, $"Reconnect failed: {ex.GetBaseException().Message}"); | |||
| RaiseOnLog(LogMessageSeverity.Error, $"Reconnect failed: {ex.GetBaseException().Message}"); | |||
| //Net is down? We can keep trying to reconnect until the user runs Disconnect() | |||
| await Task.Delay(_client.Config.FailedReconnectDelay, cancelToken).ConfigureAwait(false); | |||
| } | |||
| @@ -282,7 +282,7 @@ namespace Discord.Net.WebSockets | |||
| } | |||
| /*if (_logLevel >= LogMessageSeverity.Debug) | |||
| RaiseLogMessage(LogMessageSeverity.Debug, $"Received {buffer.Length - 12} bytes.");*/ | |||
| RaiseOnLog(LogMessageSeverity.Debug, $"Received {buffer.Length - 12} bytes.");*/ | |||
| long userId; | |||
| if (_ssrcMapping.TryGetValue(ssrc, out userId)) | |||
| @@ -404,7 +404,7 @@ namespace Discord.Net.WebSockets | |||
| } | |||
| catch (SocketException ex) | |||
| { | |||
| RaiseLogMessage(LogMessageSeverity.Error, "Failed to send UDP packet.", ex); | |||
| RaiseOnLog(LogMessageSeverity.Error, "Failed to send UDP packet.", ex); | |||
| } | |||
| hasFrame = false; | |||
| } | |||
| @@ -511,7 +511,7 @@ namespace Discord.Net.WebSockets | |||
| break; | |||
| default: | |||
| if (_logLevel >= LogMessageSeverity.Warning) | |||
| RaiseLogMessage(LogMessageSeverity.Warning, $"Unknown Opcode: {opCode}"); | |||
| RaiseOnLog(LogMessageSeverity.Warning, $"Unknown Opcode: {opCode}"); | |||
| break; | |||
| } | |||
| } | |||
| @@ -18,7 +18,7 @@ namespace Discord.Net.WebSockets | |||
| } | |||
| public event EventHandler<LogMessageEventArgs> LogMessage; | |||
| internal void RaiseLogMessage(LogMessageSeverity severity, string message, Exception exception = null) | |||
| internal void RaiseOnLog(LogMessageSeverity severity, string message, Exception exception = null) | |||
| { | |||
| if (LogMessage != null) | |||
| LogMessage(this, new LogMessageEventArgs(severity, LogMessageSource.Unknown, message, exception)); | |||
| @@ -204,7 +204,7 @@ namespace Discord.Net.WebSockets | |||
| protected virtual Task ProcessMessage(string json) | |||
| { | |||
| if (_logLevel >= LogMessageSeverity.Debug) | |||
| RaiseLogMessage(LogMessageSeverity.Debug, $"In: {json}"); | |||
| RaiseOnLog(LogMessageSeverity.Debug, $"In: {json}"); | |||
| return TaskHelper.CompletedTask; | |||
| } | |||
| protected abstract object GetKeepAlive(); | |||
| @@ -213,7 +213,7 @@ namespace Discord.Net.WebSockets | |||
| { | |||
| string json = JsonConvert.SerializeObject(message); | |||
| if (_logLevel >= LogMessageSeverity.Debug) | |||
| RaiseLogMessage(LogMessageSeverity.Debug, $"Out: " + json); | |||
| RaiseOnLog(LogMessageSeverity.Debug, $"Out: " + json); | |||
| _engine.QueueMessage(json); | |||
| } | |||
| @@ -51,7 +51,7 @@ namespace Discord.Net.WebSockets | |||
| }; | |||
| _webSocket.OnError += async (s, e) => | |||
| { | |||
| _parent.RaiseLogMessage(LogMessageSeverity.Error, e.Exception?.GetBaseException()?.Message ?? e.Message); | |||
| _parent.RaiseOnLog(LogMessageSeverity.Error, e.Exception?.GetBaseException()?.Message ?? e.Message); | |||
| await _parent.DisconnectInternal(e.Exception, skipAwait: true).ConfigureAwait(false); | |||
| }; | |||
| _webSocket.OnClose += async (s, e) => | |||
| @@ -9,7 +9,7 @@ namespace Discord.Tests | |||
| { | |||
| //TODO: Tests are massively incomplete and out of date, needing a full rewrite | |||
| /*[TestClass] | |||
| [TestClass] | |||
| public class Tests | |||
| { | |||
| private const int EventTimeout = 5000; //Max time in milliseconds to wait for an event response from our test actions | |||
| @@ -167,5 +167,5 @@ namespace Discord.Tests | |||
| { | |||
| Task.WaitAll(tasks.Where(x => x != null).SelectMany(x => x).ToArray()); | |||
| } | |||
| }*/ | |||
| } | |||
| } | |||