-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
155 lines (129 loc) · 5.43 KB
/
index.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Require the necessary discord.js classes
const fs = require('node:fs');
const path = require('node:path');
require("dotenv").config();
const { Client, Collection, Events, GatewayIntentBits} = require('discord.js');
const { token, channelId } = require('./config.json');
const axios = require('axios');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const { Player } = require("discord-player");
const { updateLocalTaskLists, getLocalTaskLists } = require('./helperFunctions/retrieve_task_lists');
const { fetchSheetTitles } = require('./helperFunctions/google_sheet_helpers');
const DISCORD_WEBHOOK_USER_ID = "1221912941255721211";
// Create a new client instance
const client = new Client({
intents:
[GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
});
// When the client is ready, run this code (only once).
// The distinction between `client: Client<boolean>` and `readyClient: Client<true>` is important for TypeScript developers.
// It makes some properties non-nullable.
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
// stores a copy of task lists locally
updateLocalTaskLists();
client.commands = new Collection();
//command handling, loading all command files and saving command paths in commandFiles
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
// ping role when meeting starts
client.on('messageCreate', async (message) => {
// if a message is coming from google cal webhook
if (message.author.id === DISCORD_WEBHOOK_USER_ID) {
try {
// get the role name from the message param
let roleName = message.embeds[0].data.fields[2].value;
// get the role id from the server roles list
let roleId;
message.guild.roles.cache.forEach(role => {
if (role.name == roleName) {
roleId = role.id;
}
});
// if no role id found
if (!roleId) {
// reply w error msg
await message.reply("Role could not be found")
} else {
// else, ping the role
await message.reply(`<@&${roleId}>`);
}
} catch (error) {
console.error(error);
await message.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
})
//event listener
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
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!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
if (interaction.isAutocomplete() && interaction.commandName === 'createtask') {
const focusedOption = interaction.options.getFocused(true);
if (focusedOption.name === 'tasklistname') {
const localTaskLists = await getLocalTaskLists()
let filteredTaskLists = localTaskLists.filter(tasklist =>
tasklist.name.toLowerCase().includes(focusedOption.value.toLowerCase())
);
// sort tasklist options alphabetically
filteredTaskLists = filteredTaskLists.sort((a, b) => {
return a.name.localeCompare(b.name);
});
await interaction.respond(
// max autocomplete options supported by discord is 25
filteredTaskLists.slice(0, 25).map(tasklist => ({ name: tasklist.name, value: tasklist.name }))
);
}
}
else if (interaction.isAutocomplete() && (interaction.commandName === 'addworkorder' || interaction.commandName === 'sendworkorder' || interaction.commandName === 'viewworkorder')){
const focusedOption = interaction.options.getFocused(true);
if (focusedOption.name === 'title' || focusedOption.name === 'title2' || focusedOption.name === 'title3' || focusedOption.name === 'title4' || focusedOption.name === 'title5') {
const titles = await fetchSheetTitles(); // Fetches titles of the Google Sheets in the Work Order folder
let filteredTitles = titles.filter(title =>
title.toLowerCase().includes(focusedOption.value.toLowerCase())
);
filteredTitles = filteredTitles.sort((a, b) => {
return a.localeCompare(b);
});
// Respond with up to 25 choices that match the user's input
await interaction.respond(
filteredTitles.slice(0, 25).map(title => ({ name: title, value: title }))
);
}
}
});
// Log in to Discord with your client's token
client.login(token);
// Music player
client.player = new Player(client, {});