Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 3x 3x 3x 3x 1x 1x 1x 7x 7x 6x 6x 5x 5x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x | // Command registration script for Discord bot (TypeScript)
// ESLint/Prettier rules applied
import {
Client,
ApplicationCommandData,
Snowflake,
Collection,
GatewayIntentBits,
ApplicationCommand,
} from 'discord.js';
import { readFileSync } from 'fs';
// Inlined test environment check
const isTestEnv = !!process.env.JEST_WORKER_ID;
// .env loader (skipped in test environment)
if (!isTestEnv) {
try {
const env = readFileSync('.env', 'utf-8');
env.split('\n').forEach((line) => {
// Skip empty lines and comments
const trimmedLine = line.trim();
if (!trimmedLine || trimmedLine.startsWith('#')) return;
const match = line.match(/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
if (match) {
const [, key, value] = match;
Eif (!process.env[key]) {
process.env[key] = value.replace(/^['"]|['"]$/g, '');
}
}
});
} catch {
// Do nothing
}
}
// Read token from environment variable; skip validation in test environment
const token = process.env.DISCORD_TOKEN;
// Throw error if DISCORD_TOKEN is not set when script is executed directly
Iif (require.main === module && !token) {
throw new Error('DISCORD_TOKEN is not set in environment variables.');
}
async function register(
client: Client,
commands: ApplicationCommandData[],
guildID?: Snowflake
): Promise<Collection<string, ApplicationCommand>> {
if (!client.application) throw new Error('Client application is not ready.');
if (!guildID) {
return client.application.commands.set(commands);
}
const guild = await client.guilds.fetch(guildID);
return guild.commands.set(commands);
}
const playCommand: ApplicationCommandData = {
name: 'play',
description: 'Play TopazChat music',
options: [
{
name: 'streamkey',
type: 3, // STRING
description: 'The StreamKey of the music to play',
required: true,
},
],
};
const resyncCommand: ApplicationCommandData = {
name: 'resync',
description: 'Resync TopazChat music',
};
const stopCommand: ApplicationCommandData = {
name: 'stop',
description: 'Stop TopazChat music',
};
const commands: ApplicationCommandData[] = [playCommand, resyncCommand, stopCommand];
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
async function main() {
await client.login(token);
if (!client.application) throw new Error('Client application is not ready after login.');
await client.application.fetch();
await register(client, commands, process.argv[2]);
console.log('registration succeed!');
process.exit(0);
}
export { commands, register };
Iif (require.main === module) {
main().catch((err) => {
console.error(err);
process.exit(1);
});
}
|