1- // This file is part of the DSharpPlus project.
2- //
3- // Copyright (c) 2015 Mike Santiago
4- // Copyright (c) 2016-2022 DSharpPlus Contributors
5- //
6- // Permission is hereby granted, free of charge, to any person obtaining a copy
7- // of this software and associated documentation files (the "Software"), to deal
8- // in the Software without restriction, including without limitation the rights
9- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10- // copies of the Software, and to permit persons to whom the Software is
11- // furnished to do so, subject to the following conditions:
12- //
13- // The above copyright notice and this permission notice shall be included in all
14- // copies or substantial portions of the Software.
15- //
16- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22- // SOFTWARE.
23-
1+ // This file is part of the DSharpPlus project.
2+ //
3+ // Copyright (c) 2015 Mike Santiago
4+ // Copyright (c) 2016-2022 DSharpPlus Contributors
5+ //
6+ // Permission is hereby granted, free of charge, to any person obtaining a copy
7+ // of this software and associated documentation files (the "Software"), to deal
8+ // in the Software without restriction, including without limitation the rights
9+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ // copies of the Software, and to permit persons to whom the Software is
11+ // furnished to do so, subject to the following conditions:
12+ //
13+ // The above copyright notice and this permission notice shall be included in all
14+ // copies or substantial portions of the Software.
15+ //
16+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ // SOFTWARE.
23+
2424using System ;
2525using System . Threading . Tasks ;
2626using DSharpPlus ;
@@ -43,13 +43,13 @@ public class CommandHandler
4343 private DiscordClient _client ;
4444 private MainHandler _mainHandler ;
4545 private IServiceProvider _services ;
46- private readonly MemoryCache cache = new MemoryCache ( new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan . FromMinutes ( 3 ) } ) ;
46+ private readonly MemoryCache cache = new ( new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan . FromMinutes ( 3 ) } ) ;
4747
4848 public Task InitializeAsync ( MainHandler mainHandler )
4949 {
5050 _mainHandler = mainHandler ;
5151 _client = mainHandler . Client ;
52- ServiceCollection services = new ServiceCollection ( ) ;
52+ ServiceCollection services = new ( ) ;
5353 services . AddSingleton ( mainHandler ) ;
5454 services . AddSingleton ( new PaginationService ( _client ) ) ;
5555 _services = services . BuildServiceProvider ( ) ;
@@ -66,42 +66,42 @@ public Task InitializeAsync(MainHandler mainHandler)
6666 _commands . RegisterCommands < GeneralCommands > ( ) ;
6767
6868 _client . MessageUpdated += HandleUpdate ;
69- _commands . CommandErrored += _commands_CommandErrored ;
69+ _commands . CommandErrored += CommandErroredAsync ;
7070
7171 return Task . CompletedTask ;
7272 }
7373
74- private async Task _commands_CommandErrored ( CommandsNextExtension commandsNext , CommandErrorEventArgs e )
74+ private async Task CommandErroredAsync ( CommandsNextExtension commandsNext , CommandErrorEventArgs e )
7575 {
7676 if ( e . Exception is CommandNotFoundException )
7777 {
78- ( string , DiscordEmbedBuilder , PaginatedMessage ) reply = await BuildReply ( e . Context . Message , e . Context . Message . Content . Substring ( e . Context . Message . GetMentionPrefixLength ( _client . CurrentUser ) ) ) ;
79- if ( reply . Item1 == null && reply . Item2 == null && reply . Item3 == null )
80- {
81- return ;
82- }
83-
84- DiscordMessage message = reply . Item3 != null
85- ? await e . Context . Services . GetService < PaginationService > ( ) . SendPaginatedMessageAsync ( e . Context . Channel , reply . Item3 )
78+ ( string , DiscordEmbedBuilder , PaginatedMessage ) reply = await BuildReplyAsync ( e . Context . Message , e . Context . Message . Content [ e . Context . Message . GetMentionPrefixLength ( _client . CurrentUser ) .. ] ) ;
79+ if ( reply . Item1 == null && reply . Item2 == null && reply . Item3 == null )
80+ {
81+ return ;
82+ }
83+
84+ DiscordMessage message = reply . Item3 != null
85+ ? await e . Context . Services . GetService < PaginationService > ( ) . SendPaginatedMessageAsync ( e . Context . Channel , reply . Item3 )
8686 : await e . Context . RespondAsync ( reply . Item1 , embed : reply . Item2 ) ;
8787 AddCache ( e . Context . Message . Id , message . Id ) ;
8888 }
89- else
90- {
91- Console . WriteLine ( e . Exception ) ;
92- }
89+ else
90+ {
91+ Console . WriteLine ( e . Exception ) ;
92+ }
9393 }
9494
9595 public Task < int > HandleCommand ( DiscordMessage msg )
9696 {
97- if ( ! msg . Channel . IsPrivate && msg . Channel . Guild . Id == 81384788765712384 )
98- {
99- if ( msg . Channel . Name != "dotnet_dsharpplus" && msg . Channel . Name != "testing" && msg . Channel . Name != "playground" )
100- {
101- return Task . FromResult ( - 1 ) ;
102- }
103- }
104-
97+ if ( ! msg . Channel . IsPrivate && msg . Channel . Guild . Id == 81384788765712384 )
98+ {
99+ if ( msg . Channel . Name is not "dotnet_dsharpplus" and not "testing" and not "playground" )
100+ {
101+ return Task . FromResult ( - 1 ) ;
102+ }
103+ }
104+
105105 return Task . FromResult ( msg . GetMentionPrefixLength ( _client . CurrentUser ) ) ;
106106 }
107107
@@ -113,36 +113,36 @@ private Task HandleUpdate(DiscordClient client, MessageUpdateEventArgs e)
113113 if ( ( id = GetOurMessageIdFromCache ( e . Message . Id ) ) != null )
114114 {
115115 DiscordMessage botMessage = await e . Channel . GetMessageAsync ( id . Value ) ;
116- if ( botMessage == null )
117- {
118- return ;
119- }
120-
116+ if ( botMessage == null )
117+ {
118+ return ;
119+ }
120+
121121 int argPos = 0 ;
122- if ( ( argPos = await HandleCommand ( e . Message ) ) == - 1 )
123- {
124- return ;
125- }
126-
127- ( string , DiscordEmbedBuilder , PaginatedMessage ) reply = await BuildReply ( e . Message , e . Message . Content . Substring ( argPos ) ) ;
122+ if ( ( argPos = await HandleCommand ( e . Message ) ) == - 1 )
123+ {
124+ return ;
125+ }
126+
127+ ( string , DiscordEmbedBuilder , PaginatedMessage ) reply = await BuildReplyAsync ( e . Message , e . Message . Content [ argPos ..] ) ;
128+
129+ if ( reply . Item1 == null && reply . Item2 == null && reply . Item3 == null )
130+ {
131+ return ;
132+ }
128133
129- if ( reply . Item1 == null && reply . Item2 == null && reply . Item3 == null )
130- {
131- return ;
132- }
133-
134134 PaginationService pagination = _services . GetService < PaginationService > ( ) ;
135135 bool isPaginatedMessage = pagination . IsPaginatedMessage ( id . Value ) ;
136136 if ( reply . Item3 != null )
137137 {
138- if ( isPaginatedMessage )
139- {
140- await pagination . UpdatePaginatedMessageAsync ( botMessage , reply . Item3 ) ;
141- }
142- else
143- {
144- await pagination . EditMessageToPaginatedMessageAsync ( botMessage , reply . Item3 ) ;
145- }
138+ if ( isPaginatedMessage )
139+ {
140+ await pagination . UpdatePaginatedMessageAsync ( botMessage , reply . Item3 ) ;
141+ }
142+ else
143+ {
144+ await pagination . EditMessageToPaginatedMessageAsync ( botMessage , reply . Item3 ) ;
145+ }
146146 }
147147 else
148148 {
@@ -158,7 +158,7 @@ private Task HandleUpdate(DiscordClient client, MessageUpdateEventArgs e)
158158 return Task . CompletedTask ;
159159 }
160160
161- private async Task < ( string , DiscordEmbedBuilder , PaginatedMessage ) > BuildReply ( DiscordMessage msg , string message )
161+ private async Task < ( string , DiscordEmbedBuilder , PaginatedMessage ) > BuildReplyAsync ( DiscordMessage msg , string message )
162162 {
163163 if ( ! _mainHandler . QueryHandler . IsReady ( ) )
164164 {
@@ -171,13 +171,13 @@ private Task HandleUpdate(DiscordClient client, MessageUpdateEventArgs e)
171171 ( string , object ) tuple = await _mainHandler . QueryHandler . RunAsync ( message ) ;
172172 if ( tuple . Item2 is PaginatorBuilder pag )
173173 {
174- PaginatedMessage paginated = new PaginatedMessage ( pag . Pages , "Results" , user : msg . Author , options : new AppearanceOptions { Timeout = TimeSpan . FromMinutes ( 10 ) } ) ;
174+ PaginatedMessage paginated = new ( pag . Pages , "Results" , user : msg . Author , options : new AppearanceOptions { Timeout = TimeSpan . FromMinutes ( 10 ) } ) ;
175175 return ( null , null , paginated ) ;
176176 }
177- else
178- {
179- return ( tuple . Item1 , tuple . Item2 as DiscordEmbedBuilder , null ) ;
180- }
177+ else
178+ {
179+ return ( tuple . Item1 , tuple . Item2 as DiscordEmbedBuilder , null ) ;
180+ }
181181 }
182182 catch ( Exception e )
183183 {
0 commit comments