-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscordAPI.js
More file actions
110 lines (99 loc) · 3.07 KB
/
Copy pathdiscordAPI.js
File metadata and controls
110 lines (99 loc) · 3.07 KB
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
101
102
103
104
105
106
107
108
109
110
import {
Client,
Collection,
Events,
GatewayIntentBits,
MessageFlags,
SlashCommandBuilder,
} from "discord.js";
import {
joinVoiceChannel,
VoiceConnectionStatus,
getVoiceConnection,
} from "@discordjs/voice";
import "dotenv/config";
const discordAPI = {
client: null,
commands: new Collection(),
};
discordAPI.commands
.set("connect", {
data: new SlashCommandBuilder()
.setName("connect")
.setDescription("connect to the reactive-chat voice channel"),
execute: async (interaction) => {
await interaction.reply("connecting...");
let channel = await interaction.guild.channels.fetch(
process.env.discord_channel_id
);
let connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
connection.on(VoiceConnectionStatus.Ready, (oldState, newState) => {
console.log(
"Discord app connected to voice and is in the Ready state!"
);
});
/* TO DO Reconnect automatically
connection.on(
VoiceConnectionStatus.Disconnected,
(oldState, newState) => {
console.log(
"Discord app disconnected from voice, attempting to reconnect."
);
joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdaptorCreator,
});
}
); */
},
})
.set("disconnect", {
data: new SlashCommandBuilder()
.setName("disconnect")
.setDescription("disconnect from the reactive-chat voice channel"),
execute: async (interaction) => {
await interaction.reply("disconnecting...");
let connection = await getVoiceConnection(process.env.discord_guild_id);
connection.destroy();
},
})
.set("ping", {
data: new SlashCommandBuilder().setName("ping").setDescription("pong?"),
execute: async (interaction) => {
await interaction.reply("pong!");
},
});
discordAPI.connect = () => {
discordAPI.client = new Client({ intents: [GatewayIntentBits.Guilds] });
discordAPI.client.once(Events.ClientReady, (readyClient) => {
console.log(`Logged in to discord API as ${readyClient.user.tag}`);
});
discordAPI.client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = discordAPI.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: "There was an error while executing this command!",
flags: MessageFlags.Ephemeral,
});
} else {
await interaction.reply({
content: "There was an error while executing this command!",
flags: MessageFlags.Ephemeral,
});
}
}
});
discordAPI.client.login(process.env.discord_token);
};
export default discordAPI;