@@ -20,6 +20,7 @@ public class DiscordChatClient : IChatClient
2020 private TaskCompletionSource < bool > _connectionCompletionTask = new TaskCompletionSource < bool > ( ) ;
2121 private TaskCompletionSource < bool > _disconnectionCompletionTask = new TaskCompletionSource < bool > ( ) ;
2222 private SocketGuild _Guild ;
23+ private readonly List < ulong > _GuildChannelIds = new List < ulong > ( ) ;
2324 private ISocketMessageChannel _TextChannel ;
2425 private bool _isReady ;
2526
@@ -37,13 +38,15 @@ public DiscordChatClient(DiscordClientSettings settings)
3738 private async Task DiscordClientGuildAvailable ( SocketGuild arg )
3839 {
3940 _Guild = arg ;
41+ _GuildChannelIds . AddRange ( _Guild . Channels . Select ( channel => channel . Id ) ) ;
4042 _TextChannel = _Guild . Channels . FirstOrDefault ( channel => channel . Id == _settings . DiscordTextChannelId ) as ISocketMessageChannel ;
4143 _isReady = true ;
4244 }
4345
4446 private async Task DiscordClientGuildUnavailable ( SocketGuild arg )
4547 {
4648 _Guild = null ;
49+ _GuildChannelIds . Clear ( ) ;
4750 _isReady = false ;
4851 }
4952
@@ -58,23 +61,34 @@ private async Task DiscordClientMessageReceived(SocketMessage arg)
5861 int commandStartIndex = 0 ;
5962 if ( message . HasCharPrefix ( _settings . CommandPrefix , ref commandStartIndex ) )
6063 {
61- if ( arg . Author is SocketGuildUser guildUser )
64+ if ( _GuildChannelIds . Contains ( message . Channel . Id ) )
6265 {
63- GuildMessageReceived ( guildUser , commandStartIndex , arg . Content ) ;
66+ if ( arg . Author is IGuildUser guildUser )
67+ {
68+ GuildCommandReceived ( guildUser , commandStartIndex , arg . Content ) ;
69+ }
70+ }
71+ else
72+ {
73+ DirectCommandReceieved ( arg . Author , commandStartIndex , arg . Content ) ;
6474 }
65-
66- // TODO: arg.Author could be of type SocketGlobalUser (but the type is internal...) which means we got a direct message.
67- // I'm not sure how else I can detect the difference I'm not seeing anything obvious in the API
6875 }
6976 }
7077
71- private void GuildMessageReceived ( SocketGuildUser user , int commandStartIndex , string message )
78+ private void GuildCommandReceived ( IGuildUser user , int commandStartIndex , string message )
7279 {
7380 var commandInfo = CommandParser . Parse ( message , commandStartIndex ) ;
74- if ( ! string . IsNullOrWhiteSpace ( commandInfo . commandWord ) )
75- {
76- RaiseOnCommandReceived ( user , commandInfo . commandWord , commandInfo . arguments ) ;
77- }
81+ if ( string . IsNullOrWhiteSpace ( commandInfo . commandWord ) ) return ;
82+
83+ RaiseOnCommandReceived ( user , commandInfo . commandWord , commandInfo . arguments ) ;
84+ }
85+
86+ private void DirectCommandReceieved ( IUser user , int commandStartIndex , string message )
87+ {
88+ var commandInfo = CommandParser . Parse ( message , commandStartIndex ) ;
89+ if ( string . IsNullOrWhiteSpace ( commandInfo . commandWord ) ) return ;
90+
91+ // TODO: Do we want to handle direct message commands?
7892 }
7993
8094 public async Task Connect ( )
@@ -140,7 +154,7 @@ public void SendMessage(string message)
140154 _TextChannel . SendMessageAsync ( $ "`{ message } `") . Wait ( ) ;
141155 }
142156
143- private void RaiseOnCommandReceived ( SocketGuildUser user , string commandWord , List < string > arguments )
157+ private void RaiseOnCommandReceived ( IGuildUser user , string commandWord , List < string > arguments )
144158 {
145159 var eventArgs = new CommandReceivedEventArgs
146160 {
0 commit comments