Skip to content

Commit 3a6e26c

Browse files
committed
webhook configuration fixes
1 parent c6b439b commit 3a6e26c

File tree

7 files changed

+212
-260
lines changed

7 files changed

+212
-260
lines changed

src/webpage/channel.ts

Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {Voice} from "./voice.js";
2424
import {User} from "./user.js";
2525
import {I18n} from "./i18n.js";
2626
import {mobile} from "./utils/utils.js";
27+
import {webhookMenu} from "./webhooks.js";
2728

2829
declare global {
2930
interface NotificationOptions {
@@ -268,128 +269,7 @@ class Channel extends SnowFlake {
268269
);
269270

270271
const webhooks = settings.addButton(I18n.webhooks.base());
271-
(async () => {
272-
const hooks = (await (
273-
await fetch(this.info.api + `/channels/${this.id}/webhooks`, {headers: this.headers})
274-
).json()) as webhookType[];
275-
webhooks.addButtonInput("", I18n.webhooks.newWebHook(), () => {
276-
const nameBox = new Dialog(I18n.webhooks.EnterWebhookName());
277-
const options = nameBox.float.options;
278-
options.addTextInput(I18n.webhooks.name(), async (name) => {
279-
const json = await (
280-
await fetch(`${this.info.api}/channels/${this.id}/webhooks/`, {
281-
method: "POST",
282-
headers: this.headers,
283-
body: JSON.stringify({name}),
284-
})
285-
).json();
286-
makeHook(json);
287-
});
288-
options.addButtonInput("", I18n.submit(), () => {
289-
options.submit();
290-
nameBox.hide();
291-
});
292-
nameBox.show();
293-
});
294-
295-
const makeHook = (hook: webhookType) => {
296-
const div = document.createElement("div");
297-
div.classList.add("flexltr", "webhookArea");
298-
const pfp = document.createElement("img");
299-
if (hook.avatar) {
300-
pfp.src = `${this.info.cdn}/avatars/${hook.id}/${hook.avatar}`;
301-
} else {
302-
const int = Number((BigInt(hook.id) >> 22n) % 6n);
303-
pfp.src = `${this.info.cdn}/embed/avatars/${int}.png`;
304-
}
305-
pfp.classList.add("webhookpfppreview");
306-
307-
const namePlate = document.createElement("div");
308-
namePlate.classList.add("flexttb");
309-
310-
const name = document.createElement("b");
311-
name.textContent = hook.name;
312-
313-
const createdAt = document.createElement("span");
314-
createdAt.textContent = I18n.webhooks.createdAt(
315-
new Intl.DateTimeFormat(I18n.lang).format(SnowFlake.stringToUnixTime(hook.id)),
316-
);
317-
318-
namePlate.append(name, createdAt);
319-
320-
const icon = document.createElement("span");
321-
icon.classList.add("svg-intoMenu", "svgicon");
322-
323-
div.append(pfp, namePlate, icon);
324-
325-
div.onclick = () => {
326-
const form = webhooks.addSubForm(
327-
hook.name,
328-
(e) => {
329-
console.log(e);
330-
},
331-
{traditionalSubmit: true},
332-
);
333-
form.addTextInput(I18n.webhooks.name(), "name", {initText: hook.name});
334-
form.addFileInput(I18n.webhooks.avatar(), "avatar", {clear: true});
335-
336-
const moveChannels = this.guild.channels.filter(
337-
(_) => _.hasPermission("MANAGE_WEBHOOKS") && _.type !== 4,
338-
);
339-
form.addSelect(
340-
I18n.webhooks.channel(),
341-
"channel_id",
342-
moveChannels.map((_) => _.name),
343-
{
344-
defaultIndex: moveChannels.findIndex((_) => _.id === this.id),
345-
},
346-
moveChannels.map((_) => _.id),
347-
);
348-
349-
form.addMDText(I18n.webhooks.token(hook.token));
350-
form.addMDText(I18n.webhooks.url(hook.url));
351-
form.addButtonInput("", I18n.webhooks.copyURL(), () => {
352-
navigator.clipboard.writeText(hook.url);
353-
});
354-
355-
form.addText(I18n.webhooks.createdBy());
356-
357-
try {
358-
const div = document.createElement("div");
359-
div.classList.add("flexltr", "createdWebhook");
360-
//TODO make sure this is something I can actually do here
361-
const user = new User(hook.user, this.localuser);
362-
const name = document.createElement("b");
363-
name.textContent = user.name;
364-
const nameBox = document.createElement("div");
365-
nameBox.classList.add("flexttb");
366-
nameBox.append(name);
367-
const pfp = user.buildpfp();
368-
div.append(pfp, nameBox);
369-
form.addHTMLArea(div);
370-
371-
Member.resolveMember(user, this.guild).then((_) => {
372-
if (_) {
373-
name.textContent = _.name;
374-
pfp.src = _.getpfpsrc();
375-
} else {
376-
const notFound = document.createElement("span");
377-
notFound.textContent = I18n.webhooks.notFound();
378-
nameBox.append(notFound);
379-
}
380-
});
381-
user.bind(div, this.guild);
382-
} catch {}
383-
};
384-
385-
console.log(hook);
386-
387-
webhooks.addHTMLArea(div);
388-
};
389-
for (const hook of hooks) {
390-
makeHook(hook);
391-
}
392-
})();
272+
webhookMenu(this.guild, this.info.api + `/channels/${this.id}/webhooks`, webhooks, this.id);
393273

394274
settings.show();
395275
}

src/webpage/guild.ts

Lines changed: 2 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import {
1414
rolesjson,
1515
emojipjson,
1616
extendedProperties,
17-
webhookType,
1817
} from "./jsontypes.js";
1918
import {User} from "./user.js";
2019
import {I18n} from "./i18n.js";
2120
import {Emoji} from "./emoji.js";
21+
import {webhookMenu} from "./webhooks.js";
2222

2323
class Guild extends SnowFlake {
2424
owner!: Localuser;
@@ -318,136 +318,7 @@ class Guild extends SnowFlake {
318318
emoji.addHTMLArea(containdiv);
319319
}
320320
const webhooks = settings.addButton(I18n.webhooks.base());
321-
(async () => {
322-
const moveChannels = this.channels.filter(
323-
(_) => _.hasPermission("MANAGE_WEBHOOKS") && _.type !== 4,
324-
);
325-
const hooks = (await (
326-
await fetch(this.info.api + `/guilds/${this.id}/webhooks`, {headers: this.headers})
327-
).json()) as webhookType[];
328-
webhooks.addButtonInput("", I18n.webhooks.newWebHook(), () => {
329-
const nameBox = new Dialog(I18n.webhooks.EnterWebhookName());
330-
const options = nameBox.float.options;
331-
options.addTextInput(I18n.webhooks.name(), async (name) => {
332-
const json = await (
333-
await fetch(`${this.info.api}/channels/${moveChannels[select.index].id}/webhooks/`, {
334-
method: "POST",
335-
headers: this.headers,
336-
body: JSON.stringify({name}),
337-
})
338-
).json();
339-
makeHook(json);
340-
});
341-
const select = options.addSelect(
342-
I18n.webhooks.channel(),
343-
() => {},
344-
moveChannels.map((_) => _.name),
345-
{
346-
defaultIndex: 0,
347-
},
348-
);
349-
options.addButtonInput("", I18n.submit(), () => {
350-
options.submit();
351-
nameBox.hide();
352-
});
353-
nameBox.show();
354-
});
355-
356-
const makeHook = (hook: webhookType) => {
357-
const div = document.createElement("div");
358-
div.classList.add("flexltr", "webhookArea");
359-
const pfp = document.createElement("img");
360-
if (hook.avatar) {
361-
pfp.src = `${this.info.cdn}/avatars/${hook.id}/${hook.avatar}`;
362-
} else {
363-
const int = Number((BigInt(hook.id) >> 22n) % 6n);
364-
pfp.src = `${this.info.cdn}/embed/avatars/${int}.png`;
365-
}
366-
pfp.classList.add("webhookpfppreview");
367-
368-
const namePlate = document.createElement("div");
369-
namePlate.classList.add("flexttb");
370-
371-
const name = document.createElement("b");
372-
name.textContent = hook.name;
373-
374-
const createdAt = document.createElement("span");
375-
createdAt.textContent = I18n.webhooks.createdAt(
376-
new Intl.DateTimeFormat(I18n.lang).format(SnowFlake.stringToUnixTime(hook.id)),
377-
);
378-
379-
namePlate.append(name, createdAt);
380-
381-
const icon = document.createElement("span");
382-
icon.classList.add("svg-intoMenu", "svgicon");
383-
384-
div.append(pfp, namePlate, icon);
385-
386-
div.onclick = () => {
387-
const form = webhooks.addSubForm(
388-
hook.name,
389-
(e) => {
390-
console.log(e);
391-
},
392-
{traditionalSubmit: true},
393-
);
394-
form.addTextInput(I18n.webhooks.name(), "name", {initText: hook.name});
395-
form.addFileInput(I18n.webhooks.avatar(), "avatar", {clear: true});
396-
397-
form.addSelect(
398-
I18n.webhooks.channel(),
399-
"channel_id",
400-
moveChannels.map((_) => _.name),
401-
{
402-
defaultIndex: moveChannels.findIndex((_) => _.id === hook.channel_id),
403-
},
404-
moveChannels.map((_) => _.id),
405-
);
406-
407-
form.addMDText(I18n.webhooks.token(hook.token));
408-
form.addMDText(I18n.webhooks.url(hook.url));
409-
form.addButtonInput("", I18n.webhooks.copyURL(), () => {
410-
navigator.clipboard.writeText(hook.url);
411-
});
412-
413-
form.addText(I18n.webhooks.createdBy());
414-
415-
try {
416-
const div = document.createElement("div");
417-
div.classList.add("flexltr", "createdWebhook");
418-
//TODO make sure this is something I can actually do here
419-
const user = new User(hook.user, this.localuser);
420-
const name = document.createElement("b");
421-
name.textContent = user.name;
422-
const nameBox = document.createElement("div");
423-
nameBox.classList.add("flexttb");
424-
nameBox.append(name);
425-
const pfp = user.buildpfp();
426-
div.append(pfp, nameBox);
427-
form.addHTMLArea(div);
428-
429-
Member.resolveMember(user, this).then((_) => {
430-
if (_) {
431-
name.textContent = _.name;
432-
pfp.src = _.getpfpsrc();
433-
} else {
434-
const notFound = document.createElement("span");
435-
notFound.textContent = I18n.webhooks.notFound();
436-
nameBox.append(notFound);
437-
}
438-
});
439-
user.bind(div, this);
440-
} catch {}
441-
};
442-
443-
console.log(hook);
444-
445-
webhooks.addHTMLArea(div);
446-
};
447-
for (const hook of hooks) {
448-
makeHook(hook);
449-
}
450-
})();
321+
webhookMenu(this, this.info.api + `/guilds/${this.id}/webhooks`, webhooks);
451322
settings.show();
452323
}
453324
makeInviteMenu(options: Options, valid: void | Channel[]) {

src/webpage/jsontypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ type webhookType = {
309309
guild_id: string;
310310
id: string;
311311
name: string;
312-
type: 1;
312+
type: 1 | 2 | 3;
313313
user: userjson;
314314
token: string;
315315
url: string;

src/webpage/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ class Options implements OptionsElement<void> {
664664
this.noSubmit = noSubmit;
665665
}
666666
removeAll() {
667+
this.returnFromSub();
667668
while (this.options.length) {
668669
this.options.pop();
669670
}

0 commit comments

Comments
 (0)