All files index.ts

64.1% Statements 25/39
55% Branches 11/20
33.33% Functions 2/6
66.66% Lines 22/33

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 646x 6x 6x     6x     6x         6x 6x 6x 6x 2x 9x 9x 8x 8x 6x 6x 6x 6x 6x                 6x       6x                 6x                              
import { Client, GatewayIntentBits, Interaction } from 'discord.js';
import { readFileSync } from 'fs';
import { handleInteraction } from './bot';
 
// Detect test environment
const isTestEnv = !!process.env.JEST_WORKER_ID;
 
// Lazy singleton client export (tests expect `client` named export)
export const client = new Client({
  intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates],
});
 
// .env loader (simple, tolerant)
(function loadEnv() {
  Iif (isTestEnv) return; // skip in test env for predictable tests
  try {
    const env = readFileSync('.env', 'utf-8');
    env.split('\n').forEach((line) => {
      const trimmed = line.trim();
      if (!trimmed || trimmed.startsWith('#')) return;
      const match = line.match(/^[A-Za-z_][A-Za-z0-9_]*\s*=\s*.*/);
      if (!match) return;
      const eqIdx = line.indexOf('=');
      const key = line.slice(0, eqIdx).trim();
      let value = line.slice(eqIdx + 1).trim();
      value = value.replace(/^['"]|['"]$/g, '');
      if (!process.env[key]) process.env[key] = value; // don't override existing
    });
  } catch {
    // Ignore missing .env
  }
})();
 
// Register basic events (tests check these listeners exist)
// v15 'clientReady' イベント
client.once('clientReady', () => {
  if (!isTestEnv) console.log('Bot ready');
});
 
client.on('interactionCreate', async (interaction: Interaction) => {
  try {
    await handleInteraction(interaction);
  } catch (e) {
    if (!isTestEnv) console.error('interaction error', e);
  }
});
 
// Only login when executed directly and not in test env
Iif (require.main === module && !isTestEnv) {
  const token = process.env.DISCORD_TOKEN;
  if (!token) {
    throw new Error('DISCORD_TOKEN is not set');
  }
  client
    .login(token)
    .then(() => {
      console.log('Logged in');
    })
    .catch((err) => {
      console.error('Login failed', err);
      process.exit(1);
    });
}