Skip to content

Commit ac1341b

Browse files
committed
Better autocomplete
1 parent e3535e2 commit ac1341b

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

src/commands/moderation/cancelMatch.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ import {
66
PermissionFlagsBits,
77
} from 'discord.js'
88
import { COMMAND_HANDLERS } from '../../command-handlers'
9-
import {
10-
getActiveMatches,
11-
getQueueIdFromMatch,
12-
getQueueSettings,
13-
} from '../../utils/queryDB'
9+
import { getQueueIdFromMatch, getQueueSettings } from '../../utils/queryDB'
10+
import { getMatchesForAutocomplete } from '../../utils/Autocompletions'
1411

1512
export default {
1613
data: new SlashCommandBuilder()
@@ -71,11 +68,11 @@ export default {
7168

7269
async autocomplete(interaction: AutocompleteInteraction) {
7370
try {
74-
const focusedValue = interaction.options.getFocused().toLowerCase()
75-
const activeMatches = await getActiveMatches()
71+
const focusedValue = interaction.options.getFocused()
72+
const matches = await getMatchesForAutocomplete(focusedValue)
7673

7774
const choices = await Promise.all(
78-
activeMatches.map(async (match) => {
75+
matches.map(async (match) => {
7976
const queueId = await getQueueIdFromMatch(match.id)
8077
const queueSettings = await getQueueSettings(queueId, ['queue_name'])
8178
return {
@@ -85,11 +82,7 @@ export default {
8582
}),
8683
)
8784

88-
const filtered = choices.filter((choice) =>
89-
choice.name.toLowerCase().includes(focusedValue),
90-
)
91-
92-
await interaction.respond(filtered.slice(0, 25))
85+
await interaction.respond(choices)
9386
} catch (err) {
9487
console.error('Error in cancel-match autocomplete:', err)
9588
await interaction.respond([])

src/commands/moderation/giveWin.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {
66
SlashCommandBuilder,
77
} from 'discord.js'
88
import { getUsersInMatch, getUserTeam } from '../../utils/queryDB'
9-
import { pool } from '../../db'
109
import { endMatch } from '../../utils/matchHelpers'
10+
import { getMatchesForAutocomplete } from '../../utils/Autocompletions'
11+
import { pool } from '../../db'
1112

1213
export default {
1314
data: new SlashCommandBuilder()
@@ -92,22 +93,20 @@ export default {
9293
)
9394
await interaction.respond(filtered.slice(0, 25))
9495
} else if (currentValue.name === 'match-id') {
95-
const input = currentValue.value
96-
const matches = await pool.query(
97-
'SELECT id FROM matches ORDER BY id DESC',
98-
)
99-
if (!matches.rows || matches.rows.length === 0) {
96+
const input = currentValue.value ?? ''
97+
const matches = await getMatchesForAutocomplete(input)
98+
99+
if (!matches || matches.length === 0) {
100100
await interaction.respond([])
101101
return
102102
}
103-
const matchIds = matches.rows.map((match: any) => ({
104-
name: (interaction.channelId = match.id.toString())
105-
? `${match.id.toString()}`
106-
: match.id.toString(),
103+
104+
const matchIds = matches.map((match) => ({
105+
name: match.id.toString(),
107106
value: match.id.toString(),
108107
}))
109-
const filtered = matchIds.filter((match) => match.name.includes(input))
110-
await interaction.respond(filtered.slice(0, 25))
108+
109+
await interaction.respond(matchIds)
111110
}
112111
},
113112
}

src/utils/Autocompletions.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AutocompleteInteraction, User } from 'discord.js'
22
import { getAllOpenRooms, strikeUtils } from './queryDB'
33
import { client } from '../client'
4+
import { pool } from '../db'
45

56
const userCache = new Map<string, User>()
67

@@ -186,3 +187,34 @@ export async function roomDeleteAutoCompletion(
186187
await interaction.respond(choices)
187188
return
188189
}
190+
191+
// Show all active matches for autocomplete using prefix
192+
export async function getMatchesForAutocomplete(
193+
input: string,
194+
): Promise<{ id: number }[]> {
195+
let query: string
196+
let params: any[]
197+
198+
if (input.length > 0) {
199+
query = `
200+
SELECT id FROM matches
201+
WHERE open = true
202+
AND id::text ILIKE $1
203+
ORDER BY id DESC
204+
LIMIT 25
205+
`
206+
params = [`${input}%`]
207+
} else {
208+
// Show 25 matches with no input
209+
query = `
210+
SELECT id FROM matches
211+
WHERE open = true
212+
ORDER BY id DESC
213+
LIMIT 25
214+
`
215+
params = []
216+
}
217+
218+
const result = await pool.query(query, params)
219+
return result.rows
220+
}

0 commit comments

Comments
 (0)