Skip to content

Commit

Permalink
Query official database for player name search when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian8337 committed Nov 12, 2024
1 parent 7d7b0f8 commit 790c492
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { DatabaseManager } from "@database/DatabaseManager";
import { AutocompleteHandler } from "@structures/core/AutocompleteHandler";
import { DroidHelper } from "@utils/helpers/DroidHelper";

export const run: AutocompleteHandler["run"] = async (_, interaction) => {
const focusedValue: string = interaction.options.getFocused();

interaction.respond(
await DatabaseManager.elainaDb.collections.userBind.searchPlayersForAutocomplete(
focusedValue,
await DroidHelper.searchPlayersForAutocomplete(
interaction.options.getFocused(),
),
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { DatabaseManager } from "@database/DatabaseManager";
import { AutocompleteHandler } from "@structures/core/AutocompleteHandler";
import { DroidHelper } from "@utils/helpers/DroidHelper";

export const run: AutocompleteHandler["run"] = async (_, interaction) => {
const focusedValue: string = interaction.options.getFocused();

interaction.respond(
await DatabaseManager.elainaDb.collections.userBind.searchPlayersForAutocomplete(
focusedValue,
await DroidHelper.searchPlayersForAutocomplete(
interaction.options.getFocused(),
),
);
};
Expand Down
5 changes: 2 additions & 3 deletions src/interactions/autocomplete/osu! and osu!droid/pp/pp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DatabaseManager } from "@database/DatabaseManager";
import { AutocompleteHandler } from "@structures/core/AutocompleteHandler";
import { DroidHelper } from "@utils/helpers/DroidHelper";

export const run: AutocompleteHandler["run"] = async (_, interaction) => {
// Lots of autocomplete options for this command, but they're all usernames.
Expand Down Expand Up @@ -28,9 +29,7 @@ export const run: AutocompleteHandler["run"] = async (_, interaction) => {
}
} else {
interaction.respond(
await DatabaseManager.elainaDb.collections.userBind.searchPlayersForAutocomplete(
focused.value,
),
await DroidHelper.searchPlayersForAutocomplete(focused.value),
);
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { DatabaseManager } from "@database/DatabaseManager";
import { AutocompleteHandler } from "@structures/core/AutocompleteHandler";
import { DroidHelper } from "@utils/helpers/DroidHelper";

export const run: AutocompleteHandler["run"] = async (_, interaction) => {
const focusedValue: string = interaction.options.getFocused();

interaction.respond(
await DatabaseManager.elainaDb.collections.userBind.searchPlayersForAutocomplete(
focusedValue,
await DroidHelper.searchPlayersForAutocomplete(
interaction.options.getFocused(),
),
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { DatabaseManager } from "@database/DatabaseManager";
import { AutocompleteHandler } from "@structures/core/AutocompleteHandler";
import { DroidHelper } from "@utils/helpers/DroidHelper";

export const run: AutocompleteHandler["run"] = async (_, interaction) => {
const focused = interaction.options.getFocused(true);

switch (focused.name) {
case "username":
interaction.respond(
await DatabaseManager.elainaDb.collections.userBind.searchPlayersForAutocomplete(
focused.value,
),
await DroidHelper.searchPlayersForAutocomplete(focused.value),
);
break;
case "reworktype":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { DatabaseManager } from "@database/DatabaseManager";
import { AutocompleteHandler } from "@structures/core/AutocompleteHandler";
import { DroidHelper } from "@utils/helpers/DroidHelper";

export const run: AutocompleteHandler["run"] = async (_, interaction) => {
const focusedValue: string = interaction.options.getFocused();

interaction.respond(
await DatabaseManager.elainaDb.collections.userBind.searchPlayersForAutocomplete(
focusedValue,
await DroidHelper.searchPlayersForAutocomplete(
interaction.options.getFocused(),
),
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { DatabaseManager } from "@database/DatabaseManager";
import { AutocompleteHandler } from "@structures/core/AutocompleteHandler";
import { DroidHelper } from "@utils/helpers/DroidHelper";

export const run: AutocompleteHandler["run"] = async (_, interaction) => {
const focusedValue: string = interaction.options.getFocused();

interaction.respond(
await DatabaseManager.elainaDb.collections.userBind.searchPlayersForAutocomplete(
focusedValue,
await DroidHelper.searchPlayersForAutocomplete(
interaction.options.getFocused(),
),
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { DatabaseManager } from "@database/DatabaseManager";
import { AutocompleteHandler } from "@structures/core/AutocompleteHandler";
import { DroidHelper } from "@utils/helpers/DroidHelper";

export const run: AutocompleteHandler["run"] = async (_, interaction) => {
const focusedValue: string = interaction.options.getFocused();

interaction.respond(
await DatabaseManager.elainaDb.collections.userBind.searchPlayersForAutocomplete(
focusedValue,
await DroidHelper.searchPlayersForAutocomplete(
interaction.options.getFocused(),
),
);
};
Expand Down
49 changes: 49 additions & 0 deletions src/utils/helpers/DroidHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { APIScore, Player, Score } from "@rian8337/osu-droid-utilities";
import { RowDataPacket } from "mysql2";
import { OnlinePlayerRank } from "@structures/utils/OnlinePlayerRank";
import { readFile, stat } from "fs/promises";
import { ApplicationCommandOptionChoiceData } from "discord.js";
import { DatabaseManager } from "@database/DatabaseManager";

/**
* A helper for osu!droid related requests.
Expand Down Expand Up @@ -582,6 +584,53 @@ export abstract class DroidHelper {
return readFile(avatarPath).catch(() => null);
}

/**
* Searches for player names.
*
* In debug mode, this queries the database instead.
*
* @param name The name to search for.
* @param amount The amount of player names to retrieve. Defaults to 25.
* @returns The player names, `null` if in debug mode or the database cannot be queried.
*/
static async searchPlayersForAutocomplete(
name: string,
amount = 25,
): Promise<ApplicationCommandOptionChoiceData<string>[]> {
name = name.trim();

// Usernames only allow:
// - 2 to 20 characters.
// - alphanumeric characters and underscores.
if (
name.length < 2 ||
name.length > 20 ||
!/^[a-zA-Z0-9_]*$/.test(name)
) {
return [];
}

if (Config.isDebug) {
return DatabaseManager.elainaDb.collections.userBind.searchPlayersForAutocomplete(
name,
amount,
);
}

const playerQuery = await officialPool
.query<
RowDataPacket[]
>(`SELECT username FROM ${constructOfficialDatabaseTable(OfficialDatabaseTables.user)} WHERE username LIKE ? LIMIT ${amount};`, [`${name}%`])
.then((res) => res[0] as Pick<OfficialDatabaseUser, "username">[]);

return playerQuery.map((v) => {
return {
name: v.username,
value: v.username,
};
});
}

/**
* Cleans up filenames received from the score table to a proper title.
*
Expand Down

0 comments on commit 790c492

Please sign in to comment.