Skip to content

Commit 3c6f5f6

Browse files
authored
Merge pull request #321 from cubedhuang/main
feat(commands): add regular expression prefixes
2 parents eebd9c7 + 1fb6f07 commit 3c6f5f6

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/commands/client.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import {
1212
import { parseArgs } from '../utils/command.ts'
1313
import { Extension, ExtensionsManager } from './extension.ts'
1414

15-
type PrefixReturnType = string | string[] | Promise<string | string[]>
15+
type PrefixType = string | RegExp | Array<string | RegExp>
16+
type PrefixReturnType = PrefixType | Promise<PrefixType>
1617

1718
/** Command Client options extending Client Options to provide a lot of Commands-related customizations */
1819
export interface CommandClientOptions extends ClientOptions {
19-
/** Global prefix(s) of the bot. */
20-
prefix: string | string[]
20+
/** Global prefix(s) of the bot. Can be a string, a regular expression, or an array including either. */
21+
prefix: PrefixType
2122
/** Whether to enable mention prefix or not. */
2223
mentionPrefix?: boolean
2324
/** Method to get a Guild's custom prefix(s). */
@@ -57,7 +58,7 @@ export type CommandContextMiddlewareNext = () => unknown | Promise<unknown>
5758
* See InteractionsClient (`Client#slash`) for more info about Slash Commands.
5859
*/
5960
export class CommandClient extends Client implements CommandClientOptions {
60-
prefix: string | string[]
61+
prefix: PrefixType
6162
mentionPrefix: boolean
6263

6364
getGuildPrefix: (guildID: string) => PrefixReturnType
@@ -167,20 +168,23 @@ export class CommandClient extends Client implements CommandClientOptions {
167168
if (isGuildBlacklisted) return
168169
}
169170

170-
let prefix: string | string[] = []
171-
if (typeof this.prefix === 'string') prefix = [...prefix, this.prefix]
171+
let prefix: PrefixType = []
172+
if (typeof this.prefix === 'string' || this.prefix instanceof RegExp)
173+
prefix = [...prefix, this.prefix]
172174
else prefix = [...prefix, ...this.prefix]
173175

174176
const userPrefix = await this.getUserPrefix(msg.author.id)
175177
if (userPrefix !== undefined) {
176-
if (typeof userPrefix === 'string') prefix = [...prefix, userPrefix]
178+
if (typeof userPrefix === 'string' || userPrefix instanceof RegExp)
179+
prefix = [...prefix, userPrefix]
177180
else prefix = [...prefix, ...userPrefix]
178181
}
179182

180183
if (msg.guild !== undefined) {
181184
const guildPrefix = await this.getGuildPrefix(msg.guild.id)
182185
if (guildPrefix !== undefined) {
183-
if (typeof guildPrefix === 'string') prefix = [...prefix, guildPrefix]
186+
if (typeof guildPrefix === 'string' || guildPrefix instanceof RegExp)
187+
prefix = [...prefix, guildPrefix]
184188
else prefix = [...prefix, ...guildPrefix]
185189
}
186190
}
@@ -189,9 +193,22 @@ export class CommandClient extends Client implements CommandClientOptions {
189193

190194
let mentionPrefix = false
191195

192-
let usedPrefix = prefix
193-
.filter((v) => msg.content.startsWith(v))
194-
.sort((b, a) => a.length - b.length)[0]
196+
const usedPrefixes = []
197+
for (const p of prefix) {
198+
if (typeof p === 'string') {
199+
if (msg.content.startsWith(p)) {
200+
usedPrefixes.push(p)
201+
}
202+
} else {
203+
const match = msg.content.match(p)
204+
// The regex matches and is at the start of the message
205+
if (match !== null && match.index === 0) {
206+
usedPrefixes.push(match[0])
207+
}
208+
}
209+
}
210+
211+
let usedPrefix = usedPrefixes.sort((b, a) => a.length - b.length)[0]
195212
if (usedPrefix === undefined && this.mentionPrefix) mentionPrefix = true
196213

197214
if (mentionPrefix) {

test/class.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { TOKEN } from './config.ts'
1212
class MyClient extends CommandClient {
1313
constructor() {
1414
super({
15-
prefix: ['!', '!!'],
15+
// /^!+/ is a regular expression that matches any amount of exclamation marks
16+
prefix: ['!', '!!', /!+/],
1617
caseSensitive: false
1718
})
1819
}

0 commit comments

Comments
 (0)