summaryrefslogtreecommitdiff
path: root/src/register.ts
blob: 94bb420c3f35c642b9da1942cfd91a6623692e17 (plain) (blame)
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
import {
  HOT_COMMAND,
  NSFW_COMMAND,
  ROLEPLAY_COMMAND,
  TOP_COMMAND,
  type DiscordCommand,
} from './commands.ts';
import dotenv from 'dotenv';
import process from 'node:process';

dotenv.config({ path: '.dev.vars' });

const token = process.env.DISCORD_TOKEN;
const applicationID = process.env.DISCORD_APPLICATION_ID;

if (!token)
  throw new Error('The DISCORD_TOKEN environment variable is required.');

if (!applicationID)
  throw new Error(
    'The DISCORD_APPLICATION_ID environment variable is required.',
  );

const url = `https://discord.com/api/v10/applications/${applicationID}/commands`;

const commands: DiscordCommand[] = [
  HOT_COMMAND,
  ROLEPLAY_COMMAND,
  NSFW_COMMAND,
  TOP_COMMAND,
];

const response = await fetch(url, {
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Bot ${token}`,
  },
  method: 'PUT',
  body: JSON.stringify(commands),
});

if (response.ok) {
  console.log('Registered all commands');

  const data = await response.json();

  console.log(JSON.stringify(data, null, 2));
} else {
  console.error('Error registering commands');

  let errorText = `Error registering commands \n ${response.url}: ${response.status} ${response.statusText}`;

  try {
    const error = await response.text();

    if (error) errorText = `${errorText} \n\n ${error}`;
  } catch (error) {
    console.error('Error reading body from request:', error);
  }

  console.error(errorText);
}