mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
Update edit_channel.ts
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import { eventHandlers } from "../../bot.ts";
|
import { eventHandlers } from "../../bot.ts";
|
||||||
import { cacheHandlers } from "../../cache.ts";
|
import { cacheHandlers } from "../../cache.ts";
|
||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
|
import type { DiscordenoChannel } from "../../structures/channel.ts";
|
||||||
|
import { structures } from "../../structures/mod.ts";
|
||||||
import type { Channel } from "../../types/channels/channel.ts";
|
import type { Channel } from "../../types/channels/channel.ts";
|
||||||
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
|
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
|
||||||
import type { ModifyChannel } from "../../types/channels/modify_channel.ts";
|
import type { ModifyChannel } from "../../types/channels/modify_channel.ts";
|
||||||
@@ -20,7 +22,7 @@ import { hasOwnProperty, snakelize } from "../../util/utils.ts";
|
|||||||
export async function editChannel(
|
export async function editChannel(
|
||||||
channelId: bigint,
|
channelId: bigint,
|
||||||
options: ModifyChannel | ModifyThread,
|
options: ModifyChannel | ModifyThread,
|
||||||
reason?: string,
|
reason?: string
|
||||||
) {
|
) {
|
||||||
const channel = await cacheHandlers.get("channels", channelId);
|
const channel = await cacheHandlers.get("channels", channelId);
|
||||||
|
|
||||||
@@ -50,14 +52,12 @@ export async function editChannel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
hasOwnProperty<ModifyChannel>(
|
hasOwnProperty<ModifyChannel>(options, "permissionOverwrites") &&
|
||||||
options,
|
Array.isArray(options.permissionOverwrites)
|
||||||
"permissionOverwrites",
|
|
||||||
) && Array.isArray(options.permissionOverwrites)
|
|
||||||
) {
|
) {
|
||||||
await requireOverwritePermissions(
|
await requireOverwritePermissions(
|
||||||
channel.guildId,
|
channel.guildId,
|
||||||
options.permissionOverwrites,
|
options.permissionOverwrites
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,38 +78,43 @@ export async function editChannel(
|
|||||||
request.amount = 2;
|
request.amount = 2;
|
||||||
request.timestamp = Date.now() + 600000;
|
request.timestamp = Date.now() + 600000;
|
||||||
} else {
|
} else {
|
||||||
// 2 have already been used add to queue
|
return new Promise<DiscordenoChannel>((resolve, reject) => {
|
||||||
request.items.push({ channelId, options });
|
// 2 have already been used add to queue
|
||||||
if (editChannelProcessing) return;
|
request.items.push({ channelId, options, resolve, reject });
|
||||||
editChannelProcessing = true;
|
if (editChannelProcessing) return;
|
||||||
processEditChannelQueue();
|
processEditChannelQueue();
|
||||||
return;
|
editChannelProcessing = true;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
...snakelize<Record<string, unknown>>(options),
|
...snakelize<Record<string, unknown>>(options),
|
||||||
// deno-lint-ignore camelcase
|
// deno-lint-ignore camelcase
|
||||||
permission_overwrites:
|
permission_overwrites: hasOwnProperty<ModifyChannel>(
|
||||||
hasOwnProperty<ModifyChannel>(options, "permissionOverwrites")
|
options,
|
||||||
? options.permissionOverwrites?.map((overwrite) => {
|
"permissionOverwrites"
|
||||||
|
)
|
||||||
|
? options.permissionOverwrites?.map((overwrite) => {
|
||||||
return {
|
return {
|
||||||
...overwrite,
|
...overwrite,
|
||||||
allow: calculateBits(overwrite.allow),
|
allow: calculateBits(overwrite.allow),
|
||||||
deny: calculateBits(overwrite.deny),
|
deny: calculateBits(overwrite.deny),
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
: undefined,
|
: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
return await rest.runMethod<Channel>(
|
const result = await rest.runMethod<Channel>(
|
||||||
"patch",
|
"patch",
|
||||||
endpoints.CHANNEL_BASE(channelId),
|
endpoints.CHANNEL_BASE(channelId),
|
||||||
{
|
{
|
||||||
...payload,
|
...payload,
|
||||||
reason,
|
reason,
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return await structures.createDiscordenoChannel(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EditChannelRequest {
|
interface EditChannelRequest {
|
||||||
@@ -119,6 +124,9 @@ interface EditChannelRequest {
|
|||||||
items: {
|
items: {
|
||||||
channelId: bigint;
|
channelId: bigint;
|
||||||
options: ModifyChannel;
|
options: ModifyChannel;
|
||||||
|
resolve: (channel: DiscordenoChannel) => void;
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
reject: (error: any) => void;
|
||||||
}[];
|
}[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,11 +137,8 @@ function processEditChannelQueue() {
|
|||||||
if (!editChannelProcessing) return;
|
if (!editChannelProcessing) return;
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
editChannelNameTopicQueue.forEach((request) => {
|
editChannelNameTopicQueue.forEach(async (request) => {
|
||||||
eventHandlers.debug?.(
|
eventHandlers.debug?.("loop", `Running forEach loop in edit_channel file.`);
|
||||||
"loop",
|
|
||||||
`Running forEach loop in edit_channel file.`,
|
|
||||||
);
|
|
||||||
if (now > request.timestamp) return;
|
if (now > request.timestamp) return;
|
||||||
// 10 minutes have passed so we can reset this channel again
|
// 10 minutes have passed so we can reset this channel again
|
||||||
if (!request.items.length) {
|
if (!request.items.length) {
|
||||||
@@ -145,21 +150,23 @@ function processEditChannelQueue() {
|
|||||||
|
|
||||||
if (!details) return;
|
if (!details) return;
|
||||||
|
|
||||||
editChannel(details.channelId, details.options);
|
await editChannel(details.channelId, details.options)
|
||||||
|
.then((result) => details.resolve(result))
|
||||||
|
.catch(details.reject);
|
||||||
const secondDetails = request.items.shift();
|
const secondDetails = request.items.shift();
|
||||||
if (!secondDetails) return;
|
if (!secondDetails) return;
|
||||||
|
|
||||||
return editChannel(secondDetails.channelId, secondDetails.options);
|
await editChannel(secondDetails.channelId, secondDetails.options)
|
||||||
|
.then((result) => secondDetails.resolve(result))
|
||||||
|
.catch(secondDetails.reject);
|
||||||
|
return;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (editChannelNameTopicQueue.size) {
|
if (editChannelNameTopicQueue.size) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
eventHandlers.debug?.(
|
eventHandlers.debug?.("loop", `Running setTimeout in EDIT_CHANNEL file.`);
|
||||||
"loop",
|
|
||||||
`Running setTimeout in EDIT_CHANNEL file.`,
|
|
||||||
);
|
|
||||||
processEditChannelQueue();
|
processEditChannelQueue();
|
||||||
}, 600000);
|
}, 60000);
|
||||||
} else {
|
} else {
|
||||||
editChannelProcessing = false;
|
editChannelProcessing = false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user