11
11
using DiscordBotBase . CommandHandlers ;
12
12
using DiscordBotBase . Reactables ;
13
13
using FreneticUtilities . FreneticToolkit ;
14
+ using FreneticUtilities . FreneticExtensions ;
14
15
15
16
namespace DiscordBotBase
16
17
{
@@ -41,6 +42,21 @@ public class DiscordBot
41
42
/// <summary>A cache of messages sent previously on Discord.</summary>
42
43
public DiscordMessageCache Cache ;
43
44
45
+ /// <summary>Lock object for config file saving/loading.</summary>
46
+ public static LockObject ConfigSaveLock = new ( ) ;
47
+
48
+ /// <summary>Signaled when the bot is stopped.</summary>
49
+ public ManualResetEvent StoppedEvent = new ( false ) ;
50
+
51
+ /// <summary>Monitor object to help restart the bot as needed.</summary>
52
+ public ConnectionMonitor BotMonitor ;
53
+
54
+ /// <summary>All valid user chat commands in a map of typable command name -> command method.</summary>
55
+ public readonly Dictionary < string , Action < CommandData > > ChatCommands = new ( 1024 ) ;
56
+
57
+ /// <summary>All valid slash commands in a map of typable command name -> command method.</summary>
58
+ public readonly Dictionary < string , Action < SocketSlashCommand > > SlashCommands = new ( 1024 ) ;
59
+
44
60
/// <summary>Bot command response handler.</summary>
45
61
/// <param name="message">The message received.</param>
46
62
/// <param name="outputUnknowns">Whether to output "unknown command" messages.</param>
@@ -84,7 +100,7 @@ public void Respond(IUserMessage message, bool outputUnknowns, bool wasMentioned
84
100
}
85
101
string fullMessageCleaned = resultBuilder . ToString ( ) ;
86
102
Console . WriteLine ( "Found input from: (" + message . Author . Username + "), in channel: " + message . Channel . Name + ": " + fullMessageCleaned ) ;
87
- string commandNameLowered = argsCleaned [ 0 ] . ToLowerInvariant ( ) ;
103
+ string commandNameLowered = argsCleaned [ 0 ] . ToLowerFast ( ) ;
88
104
argsCleaned . RemoveAt ( 0 ) ;
89
105
argsRaw . RemoveAt ( 0 ) ;
90
106
CommandData commandData = new ( ) { Message = message , CleanedArguments = argsCleaned . ToArray ( ) , RawArguments = argsRaw . ToArray ( ) , WasBotMention = wasMentioned } ;
@@ -107,9 +123,6 @@ public void Respond(IUserMessage message, bool outputUnknowns, bool wasMentioned
107
123
}
108
124
}
109
125
110
- /// <summary>All valid user commands in a map of typable command name -> command method.</summary>
111
- public readonly Dictionary < string , Action < CommandData > > ChatCommands = new ( 1024 ) ;
112
-
113
126
/// <summary>Saves the config file.</summary>
114
127
public void SaveConfig ( )
115
128
{
@@ -119,15 +132,21 @@ public void SaveConfig()
119
132
}
120
133
}
121
134
122
- /// <summary>Lock object for config file saving/loading.</summary>
123
- public static LockObject ConfigSaveLock = new ( ) ;
124
-
125
135
/// <summary>Registers a command to a name and any number of aliases.</summary>
126
136
public void RegisterCommand ( Action < CommandData > command , params string [ ] names )
127
137
{
128
138
foreach ( string name in names )
129
139
{
130
- ChatCommands . Add ( name , command ) ;
140
+ ChatCommands . Add ( name . ToLowerFast ( ) , command ) ;
141
+ }
142
+ }
143
+
144
+ /// <summary>Registers a slash command to a name and any number of aliases.</summary>
145
+ public void RegisterSlashCommand ( Action < SocketSlashCommand > command , params string [ ] names )
146
+ {
147
+ foreach ( string name in names )
148
+ {
149
+ SlashCommands . Add ( name . ToLowerFast ( ) , command ) ;
131
150
}
132
151
}
133
152
@@ -140,12 +159,6 @@ public void Shutdown()
140
159
StoppedEvent . Set ( ) ;
141
160
}
142
161
143
- /// <summary>Signaled when the bot is stopped.</summary>
144
- public ManualResetEvent StoppedEvent = new ( false ) ;
145
-
146
- /// <summary>Monitor object to help restart the bot as needed.</summary>
147
- public ConnectionMonitor BotMonitor ;
148
-
149
162
/// <summary>Initializes the bot object, connects, and runs the active loop.</summary>
150
163
public void InitAndRun ( string [ ] args )
151
164
{
@@ -304,6 +317,19 @@ public void InitAndRun(string[] args)
304
317
Cache . CacheMessage ( socketMessage ) ;
305
318
return Task . CompletedTask ;
306
319
} ;
320
+ Client . SlashCommandExecuted += ( command ) =>
321
+ {
322
+ if ( ! ClientConfig . AllowSlashCommandsInDM && command . Channel is not IGuildChannel )
323
+ {
324
+ command . RespondAsync ( "Commands don't work there." , ephemeral : true ) ;
325
+ return Task . CompletedTask ;
326
+ }
327
+ if ( ! SlashCommands . TryGetValue ( command . CommandName . ToLowerFast ( ) , out Action < SocketSlashCommand > cmd ) )
328
+ {
329
+ command . RespondAsync ( "Unknown command." , ephemeral : true ) ;
330
+ }
331
+ return Task . CompletedTask ;
332
+ } ;
307
333
Console . WriteLine ( "Logging in to Discord..." ) ;
308
334
Client . LoginAsync ( TokenType . Bot , TOKEN ) . Wait ( ) ;
309
335
Console . WriteLine ( "Connecting to Discord..." ) ;
0 commit comments