-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.js
More file actions
158 lines (137 loc) · 4.19 KB
/
bot.js
File metadata and controls
158 lines (137 loc) · 4.19 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
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
156
157
158
const Discord = require("discord.js");
const fs = require("fs");
const path = require("path");
const auth = require("./auth.json");
const {
cleanupFiles,
createClient,
deletePtnFile,
getGameData,
handleMove,
isGameOngoing,
sendMessage,
setTimer,
validPly,
} = require("./util");
const { themes } = require("./TPS-Ninja/src/themes");
const themeIDs = Object.values(themes).map(({ id }) => id);
const client = createClient();
// Load slash commands
client.commands = new Discord.Collection();
const commandsPath = path.join(__dirname, "commands");
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);
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.`
);
}
}
// Main code
client.on(Discord.Events.ClientReady, () => {
console.log(`Logged in as ${client.user.tag}!`);
// Restore timers
const channelsDir = path.join(__dirname, "data");
if (!fs.existsSync(channelsDir)) {
fs.mkdirSync(channelsDir);
return;
}
const channels = fs.readdirSync(channelsDir);
channels.forEach((channelId) => {
const timersDir = path.join(channelsDir, channelId, "timers");
if (!fs.existsSync(timersDir)) {
return;
}
const timerFiles = fs.readdirSync(timersDir);
timerFiles.forEach((timerFilename) => {
const timerPath = path.join(timersDir, timerFilename);
const timer = require(timerPath);
if (timer && timer.timestamp && timer.type) {
setTimer(timer, channelId);
} else {
console.log("Invalid timer:", timerPath);
}
});
});
});
client.on(Discord.Events.InteractionCreate, async (interaction) => {
if (interaction.isAutocomplete()) {
// Handle autocomplete
const focusedOption = interaction.options.getFocused(true);
if (focusedOption.name === "theme") {
const focusedValue = focusedOption.value.trim().toLowerCase();
return interaction.respond(
themeIDs
.filter((choice) => choice.startsWith(focusedValue))
.map((choice) => ({ name: choice, value: choice }))
);
}
} else if (interaction.isChatInputCommand()) {
// Handle commands
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, client);
} 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,
});
}
}
}
});
client.on(Discord.Events.MessageCreate, (msg) => {
let message = msg.content.trim();
if (
message.length >= 4 &&
["!tak", "!rng"].includes(message.substring(0, 4).toLowerCase())
) {
sendMessage(msg, "Please use my new slash commands!");
} else if (validPly(message)) {
return handleMove(msg, message);
}
});
const removeChannelFolder = function (channel) {
const gameData = getGameData({ channel });
const isOngoing = isGameOngoing({ channel });
if (gameData) {
cleanupFiles(channel.id, true);
if (isOngoing) {
deletePtnFile(gameData);
}
}
};
client.on(Discord.Events.ChannelDelete, removeChannelFolder);
client.on(Discord.Events.ThreadDelete, removeChannelFolder);
client.on(Discord.Events.Error, (error) => {
console.log(`ERROR: ${error}`);
});
client.on(Discord.Events.Warn, (warning) => {
console.log(`WARNING: ${warning}`);
});
client.on(Discord.Events.RateLimit, (info) => {
console.log(`RATE_LIMIT: ${info}`);
});
process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection:", error);
});
client.login(auth.token);