Archy-Bots commited on
Commit
d49fca9
·
verified ·
1 Parent(s): 8b204eb

Delete src/commands/leave-background.js

Browse files
Files changed (1) hide show
  1. src/commands/leave-background.js +0 -223
src/commands/leave-background.js DELETED
@@ -1,223 +0,0 @@
1
- import { readJSON, writeJSON, writeBinary, deleteBucketFile } from '../storage/hfClient.js';
2
- import { invalidateLeaveBgCache } from '../canvas/leaveCard.js';
3
- import { Canvas, loadImage } from 'skia-canvas';
4
- import { patchWebhook } from '../utils/proxy.js';
5
-
6
- const EMBED_COLOR = 0x5865F2;
7
- const MANAGE_GUILD = 32n;
8
- const SLOT_NAME_REGEX = /^[a-z0-9-]{1,20}$/i;
9
- const MAX_CUSTOM_SLOTS = 6;
10
- const DEFAULT_BACKGROUNDS = ['default1', 'default2', 'default3'];
11
-
12
- function createEmbed(title, description, fields = [], color = EMBED_COLOR) {
13
- return {
14
- embeds: [{ title, description, fields, color, timestamp: new Date().toISOString() }],
15
- };
16
- }
17
-
18
- function hasManageGuild(interaction) {
19
- return !!(BigInt(interaction.member.permissions) & MANAGE_GUILD);
20
- }
21
-
22
- // Center-crop and scale any image to exactly 800x200 (banner dimensions)
23
- async function autoCropToBanner(buffer) {
24
- const img = await loadImage(buffer);
25
- const canvas = new Canvas(800, 200);
26
- const ctx = canvas.getContext('2d');
27
-
28
- const scale = Math.max(800 / img.width, 200 / img.height);
29
- const x = (800 - img.width * scale) / 2;
30
- const y = (200 - img.height * scale) / 2;
31
-
32
- ctx.drawImage(img, x, y, img.width * scale, img.height * scale);
33
- return canvas.toBuffer('png');
34
- }
35
-
36
- async function getConfig(guildId) {
37
- const defaults = {
38
- guildId,
39
- welcomeChannelId: null,
40
- leaveChannelId: null,
41
- autoRoles: [],
42
- welcomeMessage: '{username} just joined {server}!',
43
- leaveMessage: '{username} has left {server}.',
44
- dmMessage: 'Welcome to {server}!',
45
- dmEnabled: false,
46
- welcomeBackground: 'default1',
47
- leaveBackground: 'default1',
48
- welcomeCustomBackgrounds: [],
49
- leaveCustomBackgrounds: [],
50
- cardTextColor: '#ffffff',
51
- cardAccentColor: '#5865F2',
52
- createdAt: new Date().toISOString(),
53
- };
54
-
55
- try {
56
- const data = await readJSON(`guilds/${guildId}/config.json`);
57
- return data ? { ...defaults, ...data } : defaults;
58
- } catch {
59
- return defaults;
60
- }
61
- }
62
-
63
- export default async function handleLeaveBackground(interaction) {
64
- const { guild_id: guildId, data, token } = interaction;
65
- const clientId = process.env.DISCORD_CLIENT_ID;
66
-
67
- if (!hasManageGuild(interaction)) {
68
- await patchWebhook(clientId, token, createEmbed(
69
- 'Permission Denied',
70
- 'You need the **Manage Server** permission to use this command.'
71
- ));
72
- return;
73
- }
74
-
75
- const subcommand = data.options?.[0]?.name;
76
- const options = data.options?.[0]?.options || [];
77
- const config = await getConfig(guildId);
78
-
79
- switch (subcommand) {
80
- case 'default': {
81
- const style = options.find(o => o.name === 'style')?.value;
82
- if (!style || !DEFAULT_BACKGROUNDS.includes(style)) {
83
- await patchWebhook(clientId, token, createEmbed('Invalid Style', 'Choose: `default1`, `default2`, or `default3`'));
84
- return;
85
- }
86
- config.leaveBackground = style;
87
- await writeJSON(`guilds/${guildId}/config.json`, config);
88
- invalidateLeaveBgCache(guildId);
89
- await patchWebhook(clientId, token, createEmbed('Background Updated', `Leave card background set to **${style}**.`));
90
- break;
91
- }
92
-
93
- case 'upload': {
94
- const name = options.find(o => o.name === 'name')?.value?.toLowerCase();
95
- const attachmentId = options.find(o => o.name === 'file')?.value;
96
-
97
- if (!name || !SLOT_NAME_REGEX.test(name)) {
98
- await patchWebhook(clientId, token, createEmbed('Invalid Slot Name', 'Slot name must be 1-20 characters, alphanumeric and dashes only.'));
99
- return;
100
- }
101
- if (!attachmentId) {
102
- await patchWebhook(clientId, token, createEmbed('No File Provided', 'Please attach an image file.'));
103
- return;
104
- }
105
-
106
- const attachment = (interaction.data.resolved?.attachments || {})[attachmentId];
107
- if (!attachment) {
108
- await patchWebhook(clientId, token, createEmbed('Invalid Attachment', 'Could not locate attachment metadata.'));
109
- return;
110
- }
111
- if (!attachment.content_type?.startsWith('image/')) {
112
- await patchWebhook(clientId, token, createEmbed('Invalid File Type', 'Please upload a valid image file (PNG, JPG, etc.).'));
113
- return;
114
- }
115
- if (config.leaveCustomBackgrounds.length >= MAX_CUSTOM_SLOTS && !config.leaveCustomBackgrounds.includes(name)) {
116
- await patchWebhook(clientId, token, createEmbed(
117
- 'Max Slots Reached',
118
- `You can only have ${MAX_CUSTOM_SLOTS} custom backgrounds.`,
119
- [{ name: 'Current Slots', value: config.leaveCustomBackgrounds.join(', ') || 'None', inline: false }]
120
- ));
121
- return;
122
- }
123
-
124
- try {
125
- const raw = Buffer.from(await (await fetch(attachment.url)).arrayBuffer());
126
- const processed = await autoCropToBanner(raw);
127
- const bucketPath = `guilds/${guildId}/assets/backgrounds/leave/${name}.png`;
128
-
129
- await writeBinary(bucketPath, processed, 'image/png');
130
-
131
- if (!config.leaveCustomBackgrounds.includes(name)) config.leaveCustomBackgrounds.push(name);
132
- config.leaveBackground = name;
133
-
134
- await writeJSON(`guilds/${guildId}/config.json`, config);
135
- invalidateLeaveBgCache(guildId, name);
136
-
137
- await patchWebhook(clientId, token, createEmbed(
138
- 'Background Uploaded',
139
- `Custom background **${name}** uploaded, cropped to 800×200, and set as active.`
140
- ));
141
- } catch (err) {
142
- console.error('[leave-background] Upload error:', err);
143
- await patchWebhook(clientId, token, createEmbed('Upload Failed', err.message));
144
- }
145
- break;
146
- }
147
-
148
- case 'switch': {
149
- const name = options.find(o => o.name === 'name')?.value?.toLowerCase();
150
- if (!name) {
151
- await patchWebhook(clientId, token, createEmbed('Missing Slot Name', 'Please provide a slot name to switch to.'));
152
- return;
153
- }
154
-
155
- if (DEFAULT_BACKGROUNDS.includes(name) || config.leaveCustomBackgrounds.includes(name)) {
156
- config.leaveBackground = name;
157
- await writeJSON(`guilds/${guildId}/config.json`, config);
158
- invalidateLeaveBgCache(guildId, name);
159
- await patchWebhook(clientId, token, createEmbed('Background Switched', `Leave background set to **${name}**.`));
160
- return;
161
- }
162
-
163
- await patchWebhook(clientId, token, createEmbed(
164
- 'Slot Not Found',
165
- `Background slot **${name}** not found.`,
166
- [
167
- { name: 'Available Defaults', value: DEFAULT_BACKGROUNDS.join(', '), inline: true },
168
- { name: 'Your Custom Slots', value: config.leaveCustomBackgrounds.join(', ') || 'None', inline: true },
169
- ]
170
- ));
171
- break;
172
- }
173
-
174
- case 'delete': {
175
- const name = options.find(o => o.name === 'name')?.value?.toLowerCase();
176
- if (!name) {
177
- await patchWebhook(clientId, token, createEmbed('Missing Slot Name', 'Please provide a slot name to delete.'));
178
- return;
179
- }
180
- if (DEFAULT_BACKGROUNDS.includes(name)) {
181
- await patchWebhook(clientId, token, createEmbed('Cannot Delete Default', 'Default backgrounds cannot be deleted.'));
182
- return;
183
- }
184
- if (!config.leaveCustomBackgrounds.includes(name)) {
185
- await patchWebhook(clientId, token, createEmbed('Slot Not Found', `Custom background **${name}** not found in your slots.`));
186
- return;
187
- }
188
-
189
- try {
190
- await deleteBucketFile(`guilds/${guildId}/assets/backgrounds/leave/${name}.png`);
191
-
192
- config.leaveCustomBackgrounds = config.leaveCustomBackgrounds.filter(s => s !== name);
193
- if (config.leaveBackground === name) config.leaveBackground = 'default1';
194
-
195
- await writeJSON(`guilds/${guildId}/config.json`, config);
196
- invalidateLeaveBgCache(guildId, name);
197
-
198
- await patchWebhook(clientId, token, createEmbed('Background Deleted', `Custom background **${name}** has been deleted.`));
199
- } catch (err) {
200
- console.error('[leave-background] Delete error:', err);
201
- await patchWebhook(clientId, token, createEmbed('Delete Failed', err.message));
202
- }
203
- break;
204
- }
205
-
206
- case 'list': {
207
- await patchWebhook(clientId, token, createEmbed(
208
- 'Leave Backgrounds',
209
- 'Your configured leave backgrounds:',
210
- [
211
- { name: 'Active Background', value: `\`${config.leaveBackground}\``, inline: true },
212
- { name: 'Slots Used', value: `${config.leaveCustomBackgrounds.length}/${MAX_CUSTOM_SLOTS}`, inline: true },
213
- { name: 'Defaults Available', value: DEFAULT_BACKGROUNDS.join(', '), inline: false },
214
- { name: 'Your Custom Slots', value: config.leaveCustomBackgrounds.join(', ') || 'None', inline: false },
215
- ]
216
- ));
217
- break;
218
- }
219
-
220
- default:
221
- await patchWebhook(clientId, token, createEmbed('Unknown Subcommand', 'Available: `default`, `upload`, `switch`, `delete`, `list`'));
222
- }
223
- }