-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
244 lines (216 loc) · 7.03 KB
/
Copy pathindex.js
File metadata and controls
244 lines (216 loc) · 7.03 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const { EmbedBuilder } = require("discord.js");
const { createCustomCommandControl } = require("./commands/customcommand");
const customCommandSchema = require("./models/customCommand");
/**
* Replace template variables in the response content.
*/
function replaceVariables(template, { user, guild, args = [], timestamp, targetUser, targetMessage }) {
let result = template;
// Replace {user}
const userMention = user ? `<@${user.id}>` : "";
result = result.replace(/\{user\}/g, userMention);
// Replace {server}
const serverName = guild ? guild.name : "";
result = result.replace(/\{server\}/g, serverName);
// Replace {timestamp}
const tsStr = `<t:${Math.floor((timestamp || Date.now()) / 1000)}:f>`;
result = result.replace(/\{timestamp\}/g, tsStr);
// Replace {args:N}
const argsRegex = /\{args:(\d+)\}/g;
result = result.replace(argsRegex, (match, nStr) => {
const n = parseInt(nStr, 10);
if (args && args[n - 1] !== undefined) {
return args[n - 1];
}
// If targetUser exists (User Context Menu)
if (targetUser) {
if (n === 1) return `<@${targetUser.id}>`;
if (n === 2) return targetUser.username;
if (n === 3) return targetUser.id;
}
// If targetMessage exists (Message Context Menu)
if (targetMessage) {
if (n === 1) return `<@${targetMessage.author.id}>`;
if (n === 2) return targetMessage.content;
if (n === 3) return targetMessage.id;
if (n === 4) return targetMessage.author.username;
}
return "";
});
// Replace {args:all} as a convenience helper
result = result.replace(/\{args:all\}/g, args.join(" "));
return result;
}
/**
* Executes a custom slash or context menu command.
*/
async function executeCustomCommand(interaction, cmd, ctx) {
try {
let args = [];
let targetUser = null;
let targetMessage = null;
if (interaction.isChatInputCommand()) {
const argsStr = interaction.options.getString("args") || "";
args = argsStr.trim().split(/ +/).filter(Boolean);
} else if (interaction.isUserContextMenuCommand()) {
targetUser = interaction.targetUser;
} else if (interaction.isMessageContextMenuCommand()) {
targetMessage = interaction.targetMessage;
}
const processed = replaceVariables(cmd.response, {
user: interaction.user,
guild: interaction.guild,
args,
timestamp: Date.now(),
targetUser,
targetMessage,
});
if (cmd.embed) {
const embed = new EmbedBuilder()
.setDescription(processed)
.setColor(0x5865F2);
await interaction.reply({ embeds: [embed] });
} else {
await interaction.reply({ content: processed });
}
} catch (error) {
ctx.logger.error(`Error executing custom command ${cmd.name}:`, error);
try {
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: "❌ Failed to execute custom command.", ephemeral: true });
} else {
await interaction.reply({ content: "❌ Failed to execute custom command.", ephemeral: true });
}
} catch (e) {
ctx.logger.error("Failed to send error reply:", e);
}
}
}
/**
* Executes a custom text (prefix) command.
*/
async function executeCustomTextCommand(message, cmd, args, ctx) {
try {
const processed = replaceVariables(cmd.response, {
user: message.author,
guild: message.guild,
args,
timestamp: Date.now(),
});
if (cmd.embed) {
const embed = new EmbedBuilder()
.setDescription(processed)
.setColor(0x5865F2);
await message.reply({ embeds: [embed] });
} else {
await message.reply({ content: processed });
}
} catch (error) {
ctx.logger.error(`Error executing custom text command ${cmd.name}:`, error);
try {
await message.reply({ content: "❌ Failed to execute custom command." });
} catch (e) {
ctx.logger.error("Failed to send text error reply:", e);
}
}
}
/**
* Register a custom slash command executor in client.commands.
*/
function registerGlobalSlashExecutor(client, CustomCommandModel, name, ctx) {
if (!client.commands.has(name)) {
client.commands.set(name, {
data: {
name: name,
description: "Custom slash command",
options: [
{
name: "args",
type: 3, // STRING
description: "Arguments for the command",
required: false,
},
],
toJSON() {
return this;
},
},
async execute(interaction) {
const dbCmd = await CustomCommandModel.findOne({
guildId: interaction.guildId,
name: interaction.commandName,
type: "slash",
});
if (dbCmd) {
await executeCustomCommand(interaction, dbCmd, ctx);
} else {
await interaction.reply({ content: "❌ Custom command not found in this server.", ephemeral: true });
}
},
});
}
}
/**
* Every ADB plugin exports a single `load(ctx)` function. `ctx` is frozen
* and namespaced to this plugin.
*/
async function load(ctx) {
const CustomCommandModel = ctx.defineModel("customCommand", customCommandSchema);
// Register the /customcommand control command
ctx.registerCommand(
createCustomCommandControl(CustomCommandModel, ctx, executeCustomCommand, registerGlobalSlashExecutor),
);
// Load existing slash commands and register their executors in client.commands
try {
const slashCommands = await CustomCommandModel.find({ type: "slash" });
for (const cmd of slashCommands) {
registerGlobalSlashExecutor(ctx.client, CustomCommandModel, cmd.name, ctx);
}
} catch (err) {
ctx.logger.error("Failed to load and register existing custom slash commands:", err);
}
// Register listener for text prefix commands
ctx.registerEvent("messageCreate", async (message) => {
if (message.author.bot || !message.guild) return;
// Text-command prefix is configured from the dashboard (settings.prefix).
// Falls back to "!" when unset. Read per-message so live edits apply
// without a reload.
let prefix = "!";
try {
const cfg = await ctx.db.getPluginConfig(message.guild.id, "adb-plugin-custom-commands");
const p = cfg?.data?.prefix;
if (typeof p === "string" && p.length > 0) prefix = p;
} catch (err) {
ctx.logger.error("Failed to read custom-commands prefix config:", err);
}
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
if (!commandName) return;
const dbCmd = await CustomCommandModel.findOne({
guildId: message.guild.id,
name: commandName,
type: "text",
});
if (dbCmd) {
await executeCustomTextCommand(message, dbCmd, args, ctx);
}
});
// Register listener for user/message context menu commands
ctx.registerEvent("interactionCreate", async (interaction) => {
if (!interaction.guild) return;
if (interaction.isUserContextMenuCommand() || interaction.isMessageContextMenuCommand()) {
const type = interaction.isUserContextMenuCommand() ? "user" : "message";
const dbCmd = await CustomCommandModel.findOne({
guildId: interaction.guildId,
name: interaction.commandName,
type: type,
});
if (dbCmd) {
await executeCustomCommand(interaction, dbCmd, ctx);
}
}
});
ctx.logger.info("Custom Commands plugin loaded");
}
module.exports = { load, replaceVariables };