-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.mjs
74 lines (61 loc) · 2.12 KB
/
index.mjs
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
import { Client, GatewayIntentBits, REST, Routes } from 'discord.js';
import fetch from 'node-fetch';
const token = process.env.DISCORD_BOT_TOKEN;
const id = process.env.DISCORD_SERVER_ID;
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
// Create the command and add a string parameter for the question
const commands = [
{
name: 'magic',
description: 'Asks the magic conch shell a yes/no question.',
options: [
{
name: 'question',
type: 3,
description: 'The question you want to ask.',
required: true,
},
],
},
];
const rest = new REST({ version: '10' }).setToken(token);
try {
console.log('Started refreshing application (/) commands.');
// Replace <Your_Guild_ID> with the actual ID of your Discord server
await rest.put(
Routes.applicationGuildCommands(client.user.id, id),
{ body: commands }
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'magic') {
const question = interaction.options.getString('question');
fetch('https://yesno.wtf/api')
.then((response) => response.json())
.then((response) => {
interaction.reply({
content: `Question: ${question}\nAnswer: ${response.answer}`,
files: [response.image]
});
})
.catch((error) => {
console.error(error);
interaction.reply('There was an error processing your request.');
});
}
});
client.login(token);