mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
feat(GuildManager): Allow for more options for GuildManager.cre… (#3742)
* typings: add GuildVerificationLevel and GuildExplicitContentFilter * implement new types * fix jsdoc on stores * typo * add more options for GuildStore#create * add channels and roles * update typings * fix typings and use snake case for permissionOverwrites * typings & jsdoc * fix tslint * remove trailing whitespace * fix jsdoc * fix jsdoc * fix oopsies * fix lint * fix lint * fix mr lint man * add typedefs and support for setting channel parents * fix tab indenation * update jsdoc * suggested changes * style: fix silly format * docs(PartialChannelData): name is not optional * style: remove silly format
This commit is contained in:
@@ -2,10 +2,17 @@
|
||||
|
||||
const BaseManager = require('./BaseManager');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
const { Events } = require('../util/Constants');
|
||||
const {
|
||||
Events,
|
||||
VerificationLevels,
|
||||
DefaultMessageNotifications,
|
||||
ExplicitContentFilterLevels,
|
||||
} = require('../util/Constants');
|
||||
const Guild = require('../structures/Guild');
|
||||
const GuildChannel = require('../structures/GuildChannel');
|
||||
const GuildMember = require('../structures/GuildMember');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const { resolveColor } = require('../util/Util');
|
||||
const GuildEmoji = require('../structures/GuildEmoji');
|
||||
const Invite = require('../structures/Invite');
|
||||
const Role = require('../structures/Role');
|
||||
@@ -36,6 +43,45 @@ class GuildManager extends BaseManager {
|
||||
* @typedef {Guild|GuildChannel|GuildMember|GuildEmoji|Role|Snowflake|Invite} GuildResolvable
|
||||
*/
|
||||
|
||||
/**
|
||||
* Partial data for a Role.
|
||||
* @typedef {Object} PartialRoleData
|
||||
* @property {number} id The ID for this role, used to set channel overrides,
|
||||
* this is a placeholder and will be replaced by the API after consumption
|
||||
* @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 {boolean} [hoist] Whether or not the role should be hoisted
|
||||
* @property {number} [position] The position of the role
|
||||
* @property {PermissionResolvable|number} [permissions] The permissions of the role
|
||||
* @property {boolean} [mentionable] Whether or not the role should be mentionable
|
||||
*/
|
||||
|
||||
/**
|
||||
* Partial overwrite data.
|
||||
* @typedef {Object} PartialOverwriteData
|
||||
* @property {number|Snowflake} id The Role or User ID for this overwrite
|
||||
* @property {string} [type] The type of this overwrite
|
||||
* @property {PermissionResolvable} [allow] The permissions to allow
|
||||
* @property {PermissionResolvable} [deny] The permissions to deny
|
||||
*/
|
||||
|
||||
/**
|
||||
* Partial data for a Channel.
|
||||
* @typedef {Object} PartialChannelData
|
||||
* @property {number} [id] The ID for this channel, used to set its parent,
|
||||
* this is a placeholder and will be replaced by the API after consumption
|
||||
* @property {number} [parentID] The parent ID for this channel
|
||||
* @property {string} [type] The type of the channel
|
||||
* @property {string} name The name of the channel
|
||||
* @property {string} [topic] The topic of the text channel
|
||||
* @property {boolean} [nsfw] Whether the channel is NSFW
|
||||
* @property {number} [bitrate] The bitrate of the voice channel
|
||||
* @property {number} [userLimit] The user limit of the channel
|
||||
* @property {PartialOverwriteData} [permissionOverwrites]
|
||||
* Overwrites of the channel
|
||||
* @property {number} [rateLimitPerUser] The rate limit per user of the channel in seconds
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resolves a GuildResolvable to a Guild object.
|
||||
* @method resolve
|
||||
@@ -75,37 +121,81 @@ class GuildManager extends BaseManager {
|
||||
* <warn>This is only available to bots in fewer than 10 guilds.</warn>
|
||||
* @param {string} name The name of the guild
|
||||
* @param {Object} [options] Options for the creating
|
||||
* @param {string} [options.region] The region for the server, defaults to the closest one available
|
||||
* @param {PartialChannelData[]} [options.channels] The channels for this guild
|
||||
* @param {DefaultMessageNotifications} [options.defaultMessageNotifications] The default message notifications
|
||||
* for the guild
|
||||
* @param {ExplicitContentFilterLevel} [options.explicitContentFilter] The explicit content filter level for the guild
|
||||
* @param {BufferResolvable|Base64Resolvable} [options.icon=null] The icon for the guild
|
||||
* @param {string} [options.region] The region for the server, defaults to the closest one available
|
||||
* @param {PartialRoleData[]} [options.roles] The roles for this guild,
|
||||
* the first element of this array is used to change properties of the guild's everyone role.
|
||||
* @param {VerificationLevel} [options.verificationLevel] The verification level for the guild
|
||||
* @returns {Promise<Guild>} The guild that was created
|
||||
*/
|
||||
create(name, { region, icon = null } = {}) {
|
||||
if (!icon || (typeof icon === 'string' && icon.startsWith('data:'))) {
|
||||
return new Promise((resolve, reject) =>
|
||||
this.client.api.guilds.post({ data: { name, region, icon } })
|
||||
.then(data => {
|
||||
if (this.client.guilds.cache.has(data.id)) return resolve(this.client.guilds.cache.get(data.id));
|
||||
|
||||
const handleGuild = guild => {
|
||||
if (guild.id === data.id) {
|
||||
this.client.removeListener(Events.GUILD_CREATE, handleGuild);
|
||||
this.client.clearTimeout(timeout);
|
||||
resolve(guild);
|
||||
}
|
||||
};
|
||||
this.client.on(Events.GUILD_CREATE, handleGuild);
|
||||
|
||||
const timeout = this.client.setTimeout(() => {
|
||||
this.client.removeListener(Events.GUILD_CREATE, handleGuild);
|
||||
resolve(this.client.guilds.add(data));
|
||||
}, 10000);
|
||||
return undefined;
|
||||
}, reject),
|
||||
);
|
||||
async create(name, {
|
||||
channels = [],
|
||||
defaultMessageNotifications,
|
||||
explicitContentFilter,
|
||||
icon = null,
|
||||
region,
|
||||
roles = [],
|
||||
verificationLevel,
|
||||
} = {}) {
|
||||
icon = await DataResolver.resolveImage(icon);
|
||||
if (typeof verificationLevel !== 'undefined' && typeof verificationLevel !== 'number') {
|
||||
verificationLevel = VerificationLevels.indexOf(verificationLevel);
|
||||
}
|
||||
if (typeof defaultMessageNotifications !== 'undefined' && typeof defaultMessageNotifications !== 'number') {
|
||||
defaultMessageNotifications = DefaultMessageNotifications.indexOf(defaultMessageNotifications);
|
||||
}
|
||||
if (typeof explicitContentFilter !== 'undefined' && typeof explicitContentFilter !== 'number') {
|
||||
explicitContentFilter = ExplicitContentFilterLevels.indexOf(explicitContentFilter);
|
||||
}
|
||||
for (const channel of channels) {
|
||||
channel.parent_id = channel.parentID;
|
||||
delete channel.parentID;
|
||||
if (!channel.permissionOverwrites) continue;
|
||||
for (const overwrite of channel.permissionOverwrites) {
|
||||
if (overwrite.allow) overwrite.allow = Permissions.resolve(overwrite.allow);
|
||||
if (overwrite.deny) overwrite.deny = Permissions.resolve(overwrite.deny);
|
||||
}
|
||||
channel.permission_overwrites = channel.permissionOverwrites;
|
||||
delete channel.permissionOverwrites;
|
||||
}
|
||||
for (const role of roles) {
|
||||
if (role.color) role.color = resolveColor(role.color);
|
||||
if (role.permissions) role.permissions = Permissions.resolve(role.permissions);
|
||||
}
|
||||
return new Promise((resolve, reject) =>
|
||||
this.client.api.guilds.post({ data: {
|
||||
name,
|
||||
region,
|
||||
icon,
|
||||
verification_level: verificationLevel,
|
||||
default_message_notifications: defaultMessageNotifications,
|
||||
explicit_content_filter: explicitContentFilter,
|
||||
channels,
|
||||
roles,
|
||||
} })
|
||||
.then(data => {
|
||||
if (this.client.guilds.cache.has(data.id)) return resolve(this.client.guilds.cache.get(data.id));
|
||||
|
||||
return DataResolver.resolveImage(icon)
|
||||
.then(data => this.create(name, { region, icon: data || null }));
|
||||
const handleGuild = guild => {
|
||||
if (guild.id === data.id) {
|
||||
this.client.removeListener(Events.GUILD_CREATE, handleGuild);
|
||||
this.client.clearTimeout(timeout);
|
||||
resolve(guild);
|
||||
}
|
||||
};
|
||||
this.client.on(Events.GUILD_CREATE, handleGuild);
|
||||
|
||||
const timeout = this.client.setTimeout(() => {
|
||||
this.client.removeListener(Events.GUILD_CREATE, handleGuild);
|
||||
resolve(this.client.guilds.add(data));
|
||||
}, 10000);
|
||||
return undefined;
|
||||
}, reject),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user