seyfert
Version:
The most advanced framework for discord bots
233 lines (232 loc) • 11.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChannelShorter = void 0;
const cache_1 = require("../../cache");
const transformers_1 = require("../../client/transformers");
const structures_1 = require("../../structures");
const Permissions_1 = require("../../structures/extra/Permissions");
const types_1 = require("../../types");
const utils_1 = require("../it/utils");
const base_1 = require("./base");
class ChannelShorter extends base_1.BaseShorter {
/**
* Fetches a channel by its ID.
* @param id The ID of the channel to fetch.
* @param force Whether to force fetching the channel from the API even if it exists in the cache.
* @returns A Promise that resolves to the fetched channel.
*/
async fetch(id, force) {
return (0, structures_1.channelFrom)(await this.raw(id, force), this.client);
}
async raw(id, force) {
if (!force) {
const channel = await this.client.cache.channels?.raw(id);
if (channel) {
const overwrites = await this.client.cache.overwrites?.raw(id);
if (overwrites)
channel.permission_overwrites = overwrites;
return channel;
}
}
const channel = await this.client.proxy.channels(id).get();
await this.client.cache.channels?.patch(cache_1.CacheFrom.Rest, id, 'guild_id' in channel && channel.guild_id ? channel.guild_id : '@me', channel);
if ('permission_overwrites' in channel && channel.permission_overwrites && channel.guild_id) {
await this.client.cache.overwrites?.set(cache_1.CacheFrom.Rest, id, channel.guild_id, channel.permission_overwrites);
}
return channel;
}
/**
* Deletes a channel by its ID.
* @param id The ID of the channel to delete.
* @param optional Optional parameters for the deletion.
* @returns A Promise that resolves to the deleted channel.
*/
async delete(id, optional = { guildId: '@me' }) {
const options = (0, utils_1.MergeOptions)({ guildId: '@me' }, optional);
const res = await this.client.proxy.channels(id).delete({ reason: options.reason });
await this.client.cache.channels?.removeIfNI(structures_1.BaseChannel.__intent__(options.guildId), res.id, options.guildId);
return (0, structures_1.channelFrom)(res, this.client);
}
/**
* Edits a channel by its ID.
* @param id The ID of the channel to edit.
* @param body The updated channel data.
* @param optional Optional parameters for the editing.
* @returns A Promise that resolves to the edited channel.
*/
async edit(id, body, optional = { guildId: '@me' }) {
const options = (0, utils_1.MergeOptions)({ guildId: '@me' }, optional);
const res = await this.client.proxy.channels(id).patch({ body, reason: options.reason });
await this.client.cache.channels?.setIfNI(cache_1.CacheFrom.Rest, structures_1.BaseChannel.__intent__(options.guildId), res.id, options.guildId, res);
if (body.permission_overwrites && 'permission_overwrites' in res && res.permission_overwrites)
await this.client.cache.overwrites?.setIfNI(cache_1.CacheFrom.Rest, structures_1.BaseChannel.__intent__(options.guildId), res.id, options.guildId, res.permission_overwrites);
return (0, structures_1.channelFrom)(res, this.client);
}
/**
* Edits or creates a permission overwrite for a channel.
* @param channelId The ID of the channel.
* @param overwriteId The ID of the role or member overwrite.
* @param body The overwrite payload.
* @param optional Optional parameters including guild id and reason.
*/
async editOverwrite(channelId, overwriteId, raw, optional = { guildId: '@me' }) {
const body = {
...raw,
allow: raw.allow ? Permissions_1.PermissionsBitField.resolve(raw.allow).toString() : '0',
deny: raw.deny ? Permissions_1.PermissionsBitField.resolve(raw.deny).toString() : '0',
};
const options = (0, utils_1.MergeOptions)({ guildId: '@me' }, optional);
await this.client.proxy.channels(channelId).permissions(overwriteId).put({
body,
reason: options.reason,
});
if (options.guildId === '@me')
return;
const overwrite = {
allow: body.allow,
deny: body.deny,
id: overwriteId,
type: body.type,
};
const cachedOverwrites = (await this.client.cache.overwrites?.raw(channelId)) ?? [];
const updatedOverwrites = [...cachedOverwrites.filter(current => current.id !== overwriteId), overwrite];
await this.client.cache.overwrites?.setIfNI(cache_1.CacheFrom.Rest, structures_1.BaseChannel.__intent__(options.guildId), channelId, options.guildId, updatedOverwrites);
}
/**
* Deletes a permission overwrite from a channel.
* @param channelId The ID of the channel.
* @param overwriteId The ID of the role or member overwrite.
* @param optional Optional parameters including guild id and reason.
*/
async deleteOverwrite(channelId, overwriteId, optional = { guildId: '@me' }) {
const options = (0, utils_1.MergeOptions)({ guildId: '@me' }, optional);
await this.client.proxy.channels(channelId).permissions(overwriteId).delete({ reason: options.reason });
if (options.guildId === '@me')
return;
const cachedOverwrites = await this.client.cache.overwrites?.raw(channelId);
if (!cachedOverwrites)
return;
const updatedOverwrites = cachedOverwrites.filter(current => current.id !== overwriteId);
if (!updatedOverwrites.length) {
await this.client.cache.overwrites?.removeIfNI(structures_1.BaseChannel.__intent__(options.guildId), channelId, options.guildId);
return;
}
await this.client.cache.overwrites?.setIfNI(cache_1.CacheFrom.Rest, structures_1.BaseChannel.__intent__(options.guildId), channelId, options.guildId, updatedOverwrites);
}
/**
* Sends a typing indicator to the channel.
* @param id The ID of the channel.
* @returns A Promise that resolves when the typing indicator is successfully sent.
*/
typing(id) {
return this.client.proxy.channels(id).typing.post();
}
async pins(channelId, query) {
const pins = await this.client.proxy.channels(channelId).messages.pins.get({ query });
await this.client.cache.messages?.patch(cache_1.CacheFrom.Rest, pins.items.map(x => {
return [x.message.id, x.message];
}), channelId);
return {
hasMore: pins.has_more,
items: pins.items.map(x => ({
pinnedAt: x.pinned_at,
message: transformers_1.Transformers.Message(this.client, x.message),
})),
};
}
/**
* Pins a message in the channel.
* @param messageId The ID of the message to pin.
* @param channelId The ID of the channel.
* @param reason The reason for pinning the message.
* @returns A Promise that resolves when the message is successfully pinned.
*/
setPin(messageId, channelId, reason) {
return this.client.proxy.channels(channelId).messages.pins(messageId).put({ reason });
}
/**
* Unpins a message in the channel.
* @param messageId The ID of the message to unpin.
* @param channelId The ID of the channel.
* @param reason The reason for unpinning the message.
* @returns A Promise that resolves when the message is successfully unpinned.
*/
deletePin(messageId, channelId, reason) {
return this.client.proxy.channels(channelId).messages.pins(messageId).delete({ reason });
}
/**
* Creates a new thread in the channel (only guild based channels).
* @param channelId The ID of the parent channel.
* @param reason The reason for unpinning the message.
* @returns A promise that resolves when the thread is succesfully created.
*/
async thread(channelId, body, reason) {
return this.client.threads.create(channelId, body, reason);
}
async memberPermissions(channelId, member, checkAdmin = true) {
const memberPermissions = await member.fetchPermissions();
if (checkAdmin && memberPermissions.has([types_1.PermissionFlagsBits.Administrator])) {
return new Permissions_1.PermissionsBitField(Permissions_1.PermissionsBitField.All);
}
const overwrites = await this.overwritesFor(channelId, member);
const permissions = new Permissions_1.PermissionsBitField(memberPermissions.bits);
permissions.remove([overwrites.everyone?.deny.bits ?? 0n]);
permissions.add([overwrites.everyone?.allow.bits ?? 0n]);
permissions.remove(overwrites.roles.length > 0 ? overwrites.roles.map(role => role.deny.bits) : [0n]);
permissions.add(overwrites.roles.length > 0 ? overwrites.roles.map(role => role.allow.bits) : [0n]);
permissions.remove([overwrites.member?.deny.bits ?? 0n]);
permissions.add([overwrites.member?.allow.bits ?? 0n]);
return permissions;
}
async overwritesFor(channelId, member) {
const roleOverwrites = [];
let memberOverwrites;
let everyoneOverwrites;
const channelOverwrites = (await this.client.cache.overwrites?.get(channelId)) ?? [];
for (const overwrite of channelOverwrites) {
if (overwrite.id === member.guildId) {
everyoneOverwrites = overwrite;
}
else if (member.roles.keys.includes(overwrite.id)) {
roleOverwrites.push(overwrite);
}
else if (overwrite.id === member.id) {
memberOverwrites = overwrite;
}
}
return {
everyone: everyoneOverwrites,
roles: roleOverwrites,
member: memberOverwrites,
};
}
async rolePermissions(channelId, role, checkAdmin = true) {
if (checkAdmin && role.permissions.has([types_1.PermissionFlagsBits.Administrator])) {
return new Permissions_1.PermissionsBitField(Permissions_1.PermissionsBitField.All);
}
const permissions = new Permissions_1.PermissionsBitField(role.permissions.bits);
const channelOverwrites = await this.client.cache.overwrites?.get(channelId);
if (!channelOverwrites)
return permissions;
const everyoneOverwrites = channelOverwrites.find(x => x.id === role.guildId);
const roleOverwrites = channelOverwrites.find(x => x.id === role.id);
permissions.remove([everyoneOverwrites?.deny.bits ?? 0n]);
permissions.add([everyoneOverwrites?.allow.bits ?? 0n]);
permissions.remove([roleOverwrites?.deny.bits ?? 0n]);
permissions.add([roleOverwrites?.allow.bits ?? 0n]);
return permissions;
}
async fetchMessages(channelId, query) {
const result = await this.client.proxy.channels(channelId).messages.get({
query,
});
await this.client.cache.messages?.patch(cache_1.CacheFrom.Rest, result.map(x => {
return [x.id, x];
}), channelId);
return result.map(message => transformers_1.Transformers.Message(this.client, message));
}
setVoiceStatus(channelId, status = null) {
return this.client.proxy.channels(channelId)['voice-status'].put({ body: { status } });
}
}
exports.ChannelShorter = ChannelShorter;