mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 02:23:31 +01:00
refactor(GuildManager): Better handling of creation code (#8974)
* refactor(GuildManager): tidy up guild creation * refactor: emit once only Co-authored-by: Aura Román <kyradiscord@gmail.com> * fix: allow `0`/`0n` * refactor: modify name Co-authored-by: Aura Román <kyradiscord@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -57,14 +57,14 @@ class GuildManager extends CachedManager {
|
|||||||
/**
|
/**
|
||||||
* Partial data for a Role.
|
* Partial data for a Role.
|
||||||
* @typedef {Object} PartialRoleData
|
* @typedef {Object} PartialRoleData
|
||||||
* @property {Snowflake|number} [id] The role's id, used to set channel overrides,
|
* @property {Snowflake|number} [id] The role's id, used to set channel overrides.
|
||||||
* this is a placeholder and will be replaced by the API after consumption
|
* This is a placeholder and will be replaced by the API after consumption
|
||||||
* @property {string} [name] The name of the role
|
* @property {string} [name] The name of the role
|
||||||
* @property {ColorResolvable} [color] The color of the role, either a hex string or a base 10 number
|
* @property {ColorResolvable} [color] The color of the role, either a hex string or a base 10 number
|
||||||
* @property {boolean} [hoist] Whether or not the role should be hoisted
|
* @property {boolean} [hoist] Whether the role should be hoisted
|
||||||
* @property {number} [position] The position of the role
|
* @property {number} [position] The position of the role
|
||||||
* @property {PermissionResolvable} [permissions] The permissions of the role
|
* @property {PermissionResolvable} [permissions] The permissions of the role
|
||||||
* @property {boolean} [mentionable] Whether or not the role should be mentionable
|
* @property {boolean} [mentionable] Whether the role should be mentionable
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -79,8 +79,8 @@ class GuildManager extends CachedManager {
|
|||||||
/**
|
/**
|
||||||
* Partial data for a Channel.
|
* Partial data for a Channel.
|
||||||
* @typedef {Object} PartialChannelData
|
* @typedef {Object} PartialChannelData
|
||||||
* @property {Snowflake|number} [id] The channel's id, used to set its parent,
|
* @property {Snowflake|number} [id] The channel's id, used to set its parent.
|
||||||
* this is a placeholder and will be replaced by the API after consumption
|
* This is a placeholder and will be replaced by the API after consumption
|
||||||
* @property {Snowflake|number} [parentId] The parent id for this channel
|
* @property {Snowflake|number} [parentId] The parent id for this channel
|
||||||
* @property {ChannelType.GuildText|ChannelType.GuildVoice|ChannelType.GuildCategory} [type] The type of the channel
|
* @property {ChannelType.GuildText|ChannelType.GuildVoice|ChannelType.GuildCategory} [type] The type of the channel
|
||||||
* @property {string} name The name of the channel
|
* @property {string} name The name of the channel
|
||||||
@@ -141,18 +141,18 @@ class GuildManager extends CachedManager {
|
|||||||
* Options used to create a guild.
|
* Options used to create a guild.
|
||||||
* @typedef {Object} GuildCreateOptions
|
* @typedef {Object} GuildCreateOptions
|
||||||
* @property {string} name The name of the guild
|
* @property {string} name The name of the guild
|
||||||
* @property {Snowflake|number} [afkChannelId] The AFK channel's id
|
* @property {?(BufferResolvable|Base64Resolvable)} [icon=null] The icon for the guild
|
||||||
* @property {number} [afkTimeout] The AFK timeout in seconds
|
* @property {GuildVerificationLevel} [verificationLevel] The verification level for the guild
|
||||||
* @property {PartialChannelData[]} [channels=[]] The channels for this guild
|
|
||||||
* @property {GuildDefaultMessageNotifications} [defaultMessageNotifications] The default message notifications
|
* @property {GuildDefaultMessageNotifications} [defaultMessageNotifications] The default message notifications
|
||||||
* for the guild
|
* for the guild
|
||||||
* @property {GuildExplicitContentFilter} [explicitContentFilter] The explicit content filter level for the guild
|
* @property {GuildExplicitContentFilter} [explicitContentFilter] The explicit content filter level for the guild
|
||||||
* @property {?(BufferResolvable|Base64Resolvable)} [icon=null] The icon for the guild
|
|
||||||
* @property {PartialRoleData[]} [roles=[]] The roles for this guild,
|
* @property {PartialRoleData[]} [roles=[]] The roles for this guild,
|
||||||
|
* @property {PartialChannelData[]} [channels=[]] The channels for this guild
|
||||||
|
* @property {Snowflake|number} [afkChannelId] The AFK channel's id
|
||||||
|
* @property {number} [afkTimeout] The AFK timeout in seconds
|
||||||
* the first element of this array is used to change properties of the guild's everyone role.
|
* the first element of this array is used to change properties of the guild's everyone role.
|
||||||
* @property {Snowflake|number} [systemChannelId] The system channel's id
|
* @property {Snowflake|number} [systemChannelId] The system channel's id
|
||||||
* @property {SystemChannelFlagsResolvable} [systemChannelFlags] The flags of the system channel
|
* @property {SystemChannelFlagsResolvable} [systemChannelFlags] The flags of the system channel
|
||||||
* @property {GuildVerificationLevel} [verificationLevel] The verification level for the guild
|
|
||||||
*/
|
*/
|
||||||
/* eslint-enable max-len */
|
/* eslint-enable max-len */
|
||||||
|
|
||||||
@@ -164,81 +164,83 @@ class GuildManager extends CachedManager {
|
|||||||
*/
|
*/
|
||||||
async create({
|
async create({
|
||||||
name,
|
name,
|
||||||
afkChannelId,
|
icon = null,
|
||||||
afkTimeout,
|
verificationLevel,
|
||||||
channels = [],
|
|
||||||
defaultMessageNotifications,
|
defaultMessageNotifications,
|
||||||
explicitContentFilter,
|
explicitContentFilter,
|
||||||
icon = null,
|
|
||||||
roles = [],
|
roles = [],
|
||||||
|
channels = [],
|
||||||
|
afkChannelId,
|
||||||
|
afkTimeout,
|
||||||
systemChannelId,
|
systemChannelId,
|
||||||
systemChannelFlags,
|
systemChannelFlags,
|
||||||
verificationLevel,
|
|
||||||
}) {
|
}) {
|
||||||
icon = await DataResolver.resolveImage(icon);
|
|
||||||
|
|
||||||
for (const channel of channels) {
|
|
||||||
channel.parent_id = channel.parentId;
|
|
||||||
delete channel.parentId;
|
|
||||||
channel.user_limit = channel.userLimit;
|
|
||||||
delete channel.userLimit;
|
|
||||||
channel.rate_limit_per_user = channel.rateLimitPerUser;
|
|
||||||
delete channel.rateLimitPerUser;
|
|
||||||
channel.rtc_region = channel.rtcRegion;
|
|
||||||
delete channel.rtcRegion;
|
|
||||||
channel.video_quality_mode = channel.videoQualityMode;
|
|
||||||
delete channel.videoQualityMode;
|
|
||||||
|
|
||||||
if (!channel.permissionOverwrites) continue;
|
|
||||||
for (const overwrite of channel.permissionOverwrites) {
|
|
||||||
overwrite.allow &&= PermissionsBitField.resolve(overwrite.allow).toString();
|
|
||||||
overwrite.deny &&= PermissionsBitField.resolve(overwrite.deny).toString();
|
|
||||||
}
|
|
||||||
channel.permission_overwrites = channel.permissionOverwrites;
|
|
||||||
delete channel.permissionOverwrites;
|
|
||||||
}
|
|
||||||
for (const role of roles) {
|
|
||||||
role.color &&= resolveColor(role.color);
|
|
||||||
role.permissions &&= PermissionsBitField.resolve(role.permissions).toString();
|
|
||||||
}
|
|
||||||
systemChannelFlags &&= SystemChannelFlagsBitField.resolve(systemChannelFlags);
|
|
||||||
|
|
||||||
const data = await this.client.rest.post(Routes.guilds(), {
|
const data = await this.client.rest.post(Routes.guilds(), {
|
||||||
body: {
|
body: {
|
||||||
name,
|
name,
|
||||||
icon,
|
icon: icon && (await DataResolver.resolveImage(icon)),
|
||||||
verification_level: verificationLevel,
|
verification_level: verificationLevel,
|
||||||
default_message_notifications: defaultMessageNotifications,
|
default_message_notifications: defaultMessageNotifications,
|
||||||
explicit_content_filter: explicitContentFilter,
|
explicit_content_filter: explicitContentFilter,
|
||||||
roles,
|
roles: roles.map(({ color, permissions, ...options }) => ({
|
||||||
channels,
|
...options,
|
||||||
|
color: color && resolveColor(color),
|
||||||
|
permissions:
|
||||||
|
typeof permissions === 'undefined' ? undefined : PermissionsBitField.resolve(permissions).toString(),
|
||||||
|
})),
|
||||||
|
channels: channels.map(
|
||||||
|
({
|
||||||
|
parentId,
|
||||||
|
userLimit,
|
||||||
|
rtcRegion,
|
||||||
|
videoQualityMode,
|
||||||
|
permissionOverwrites,
|
||||||
|
rateLimitPerUser,
|
||||||
|
...options
|
||||||
|
}) => ({
|
||||||
|
...options,
|
||||||
|
parent_id: parentId,
|
||||||
|
user_limit: userLimit,
|
||||||
|
rtc_region: rtcRegion,
|
||||||
|
video_quality_mode: videoQualityMode,
|
||||||
|
permission_overwrites: permissionOverwrites?.map(({ allow, deny, ...permissionOverwriteOptions }) => ({
|
||||||
|
...permissionOverwriteOptions,
|
||||||
|
allow: typeof allow === 'undefined' ? undefined : PermissionsBitField.resolve(allow).toString(),
|
||||||
|
deny: typeof deny === 'undefined' ? undefined : PermissionsBitField.resolve(deny).toString(),
|
||||||
|
})),
|
||||||
|
rate_limit_per_user: rateLimitPerUser,
|
||||||
|
}),
|
||||||
|
),
|
||||||
afk_channel_id: afkChannelId,
|
afk_channel_id: afkChannelId,
|
||||||
afk_timeout: afkTimeout,
|
afk_timeout: afkTimeout,
|
||||||
system_channel_id: systemChannelId,
|
system_channel_id: systemChannelId,
|
||||||
system_channel_flags: systemChannelFlags,
|
system_channel_flags:
|
||||||
|
typeof systemChannelFlags === 'undefined'
|
||||||
|
? undefined
|
||||||
|
: SystemChannelFlagsBitField.resolve(systemChannelFlags),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.client.guilds.cache.has(data.id)) return this.client.guilds.cache.get(data.id);
|
return (
|
||||||
|
this.client.guilds.cache.get(data.id) ??
|
||||||
|
new Promise(resolve => {
|
||||||
|
const handleGuild = guild => {
|
||||||
|
if (guild.id === data.id) {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
this.client.decrementMaxListeners();
|
||||||
|
resolve(guild);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.client.incrementMaxListeners();
|
||||||
|
this.client.once(Events.GuildCreate, handleGuild);
|
||||||
|
|
||||||
return new Promise(resolve => {
|
const timeout = setTimeout(() => {
|
||||||
const handleGuild = guild => {
|
|
||||||
if (guild.id === data.id) {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
this.client.removeListener(Events.GuildCreate, handleGuild);
|
this.client.removeListener(Events.GuildCreate, handleGuild);
|
||||||
this.client.decrementMaxListeners();
|
this.client.decrementMaxListeners();
|
||||||
resolve(guild);
|
resolve(this.client.guilds._add(data));
|
||||||
}
|
}, 10_000).unref();
|
||||||
};
|
})
|
||||||
this.client.incrementMaxListeners();
|
);
|
||||||
this.client.on(Events.GuildCreate, handleGuild);
|
|
||||||
|
|
||||||
const timeout = setTimeout(() => {
|
|
||||||
this.client.removeListener(Events.GuildCreate, handleGuild);
|
|
||||||
this.client.decrementMaxListeners();
|
|
||||||
resolve(this.client.guilds._add(data));
|
|
||||||
}, 10_000).unref();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
12
packages/discord.js/typings/index.d.ts
vendored
12
packages/discord.js/typings/index.d.ts
vendored
@@ -5406,16 +5406,16 @@ export interface GuildChannelOverwriteOptions {
|
|||||||
|
|
||||||
export interface GuildCreateOptions {
|
export interface GuildCreateOptions {
|
||||||
name: string;
|
name: string;
|
||||||
afkChannelId?: Snowflake | number;
|
icon?: BufferResolvable | Base64Resolvable | null;
|
||||||
afkTimeout?: number;
|
verificationLevel?: GuildVerificationLevel;
|
||||||
channels?: PartialChannelData[];
|
|
||||||
defaultMessageNotifications?: GuildDefaultMessageNotifications;
|
defaultMessageNotifications?: GuildDefaultMessageNotifications;
|
||||||
explicitContentFilter?: GuildExplicitContentFilter;
|
explicitContentFilter?: GuildExplicitContentFilter;
|
||||||
icon?: BufferResolvable | Base64Resolvable | null;
|
|
||||||
roles?: PartialRoleData[];
|
roles?: PartialRoleData[];
|
||||||
systemChannelFlags?: SystemChannelFlagsResolvable;
|
channels?: PartialChannelData[];
|
||||||
|
afkChannelId?: Snowflake | number;
|
||||||
|
afkTimeout?: number;
|
||||||
systemChannelId?: Snowflake | number;
|
systemChannelId?: Snowflake | number;
|
||||||
verificationLevel?: GuildVerificationLevel;
|
systemChannelFlags?: SystemChannelFlagsResolvable;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GuildWidgetSettings {
|
export interface GuildWidgetSettings {
|
||||||
|
|||||||
Reference in New Issue
Block a user