oceanic.js
Version:
A NodeJS library for interfacing with Discord.
1,163 lines • 153 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const Routes = tslib_1.__importStar(require("../util/Routes"));
const GuildScheduledEvent_1 = tslib_1.__importDefault(require("../structures/GuildScheduledEvent"));
const Webhook_1 = tslib_1.__importDefault(require("../structures/Webhook"));
const GuildTemplate_1 = tslib_1.__importDefault(require("../structures/GuildTemplate"));
const GuildPreview_1 = tslib_1.__importDefault(require("../structures/GuildPreview"));
const Role_1 = tslib_1.__importDefault(require("../structures/Role"));
const Invite_1 = tslib_1.__importDefault(require("../structures/Invite"));
const Integration_1 = tslib_1.__importDefault(require("../structures/Integration"));
const AutoModerationRule_1 = tslib_1.__importDefault(require("../structures/AutoModerationRule"));
const AuditLogEntry_1 = tslib_1.__importDefault(require("../structures/AuditLogEntry"));
const Guild_1 = tslib_1.__importDefault(require("../structures/Guild"));
const ApplicationCommand_1 = tslib_1.__importDefault(require("../structures/ApplicationCommand"));
const VoiceState_1 = tslib_1.__importDefault(require("../structures/VoiceState"));
const Soundboard_1 = tslib_1.__importDefault(require("../structures/Soundboard"));
const promises_1 = require("node:timers/promises");
/** Various methods for interacting with guilds. Located at {@link Client#rest | Client#rest}{@link RESTManager#guilds | .guilds}. */
class Guilds {
_manager;
constructor(manager) {
this._manager = manager;
}
/**
* Add a member to a guild. Requires an access token with the `guilds.join` scope.
*
* Returns the newly added member upon success, or void if the member is already in the guild.
* @param guildID The ID of the guild.
* @param userID The ID of the user to add.
* @param options The options for adding the member.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#members | Guild#members}
*/
async addMember(guildID, userID, options) {
return this._manager.authRequest({
method: "PUT",
path: Routes.GUILD_MEMBER(guildID, userID),
json: {
access_token: options.accessToken,
deaf: options.deaf,
mute: options.mute,
nick: options.nick,
roles: options.roles
}
}).then(data => data === null ? undefined : this._manager.client.util.updateMember(guildID, userID, data));
}
/**
* Add a role to a member.
* @param guildID The ID of the guild.
* @param memberID The ID of the member.
* @param roleID The ID of the role to add.
* @param reason The reason for adding the role.
* @caching This method **does not** cache its result.
*/
async addMemberRole(guildID, memberID, roleID, reason) {
await this._manager.authRequest({
method: "PUT",
path: Routes.GUILD_MEMBER_ROLE(guildID, memberID, roleID),
reason
});
}
/**
* Begin a prune.
* @param guildID The ID of the guild.
* @param options The options for the prune.
* @caching This method **does not** cache its result.
*/
async beginPrune(guildID, options) {
const reason = options?.reason;
if (options?.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_PRUNE(guildID),
json: {
days: options?.days,
compute_prune_count: options?.computePruneCount,
include_roles: options?.includeRoles
},
reason
}).then(data => data.pruned);
}
/**
* Ban up to 200 members from a guild. This requires both the `BAN_MEMBERS` and `MANAGE_GUILD` permissions.
* If no members were banned, a {@link Constants~JSONErrorCodes.FAILED_TO_BAN_USERS | FAILED_TO_BAN_USERS } will be returned.
* The bot user is ignored.
* @param guildID The ID of the guild.
* @param options The options for banning.
*/
async bulkBan(guildID, options) {
const reason = options?.reason;
if (options?.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_BULK_BAN(guildID),
json: {
delete_message_seconds: options.deleteMessageSeconds,
user_ids: options.userIDs
},
reason
}).then(data => ({
bannedUsers: data.banned_users,
failedUsers: data.failed_users
}));
}
/**
* Create a guild. This can only be used by bots in under 10 guilds.
* @param options The options for creating the guild.
* @caching This method **does not** cache its result.
*/
async create(options) {
if (options.icon) {
options.icon = this._manager.client.util._convertImage(options.icon, "icon");
}
return this._manager.authRequest({
method: "POST",
path: Routes.GUILDS,
json: {
afk_channel_id: options.afkChannelID,
afk_timeout: options.afkTimeout,
channels: options.channels,
default_message_notifications: options.defaultMessageNotifications,
explicit_content_filter: options.explicitContentFilter,
icon: options.icon,
name: options.name,
region: options.region,
roles: options.roles,
system_channel_flags: options.systemChannelFlags,
system_channel_id: options.systemChannelID,
verification_level: options.verificationLevel
}
}).then(data => new Guild_1.default(data, this._manager.client, true));
}
/**
* Create an auto moderation rule for a guild.
* @param guildID The ID of the guild.
* @param options The options for creating the rule.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#autoModerationRules | Guild#autoModerationRules}
*/
async createAutoModerationRule(guildID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_AUTOMOD_RULES(guildID),
json: {
actions: options.actions.map(a => ({
metadata: {
channel_id: a.metadata.channelID,
custom_message: a.metadata.customMessage,
duration_seconds: a.metadata.durationSeconds
},
type: a.type
})),
enabled: options.enabled,
event_type: options.eventType,
exempt_channels: options.exemptChannels,
exempt_roles: options.exemptRoles,
name: options.name,
trigger_metadata: options.triggerMetadata ? {
allow_list: options.triggerMetadata.allowList,
keyword_filter: options.triggerMetadata.keywordFilter,
mention_raid_protection_enabled: options.triggerMetadata.mentionRaidProtectionEnabled,
mention_total_limit: options.triggerMetadata.mentionTotalLimit,
presets: options.triggerMetadata.presets,
regex_patterns: options.triggerMetadata.regexPatterns
} : undefined,
trigger_type: options.triggerType
},
reason
}).then(data => this._manager.client.guilds.get(guildID)?.autoModerationRules.update(data) ?? new AutoModerationRule_1.default(data, this._manager.client));
}
/**
* Create a ban for a user.
* @param guildID The ID of the guild.
* @param userID The ID of the user to ban.
* @param options The options for creating the ban.
* @caching This method **does not** cache its result.
*/
async createBan(guildID, userID, options) {
const reason = options?.reason;
if (options?.reason) {
delete options.reason;
}
if (options?.deleteMessageDays !== undefined && !Object.hasOwn(options, "deleteMessageSeconds")) {
options.deleteMessageSeconds = options.deleteMessageDays * 86400;
}
await this._manager.authRequest({
method: "PUT",
path: Routes.GUILD_BAN(guildID, userID),
json: { delete_message_seconds: options?.deleteMessageSeconds },
reason
});
}
/**
* Create a channel in a guild.
* @param guildID The ID of the guild.
* @param options The options for creating the channel.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#channels | Guild#channels}
*/
async createChannel(guildID, type, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_CHANNELS(guildID),
json: {
available_tags: options.availableTags ? options.availableTags.map(tag => ({
emoji_id: tag.emoji?.id,
emoji_name: tag.emoji?.name,
moderated: tag.moderated,
name: tag.name
})) : options.availableTags,
bitrate: options.bitrate,
default_auto_archive_duration: options.defaultAutoArchiveDuration,
default_forum_layout: options.defaultForumLayout,
default_reaction_emoji: options.defaultReactionEmoji ? { emoji_id: options.defaultReactionEmoji.id, emoji_name: options.defaultReactionEmoji.name } : options.defaultReactionEmoji,
default_sort_order: options.defaultSortOrder,
name: options.name,
nsfw: options.nsfw,
parent_id: options.parentID,
permission_overwrites: options.permissionOverwrites,
position: options.position,
rate_limit_per_user: options.rateLimitPerUser,
rtc_region: options.rtcRegion,
topic: options.topic,
type,
user_limit: options.userLimit,
video_quality_mode: options.videoQualityMode
},
reason
}).then(data => this._manager.client.util.updateChannel(data));
}
/**
* Create an emoji in a guild.
* @param guildID The ID of the guild.
* @param options The options for creating the emoji.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#emojis | Guild#emojis}<br>{@link Client#users | Client#users} (creator, if applicable)
*/
async createEmoji(guildID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
if (options.image) {
options.image = this._manager.client.util._convertImage(options.image, "image");
}
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_EMOJIS(guildID),
json: {
image: options.image,
name: options.name,
roles: options.roles
},
reason
}).then(data => this._manager.client.guilds.get(guildID)?.emojis.update(data) ?? this._manager.client.util.convertGuildEmoji(data));
}
/**
* Create a guild from a template. This can only be used by bots in less than 10 guilds.
*
* Note: This does NOT add the guild to the client's cache.
* @param code The code of the template to use.
* @param options The options for creating the guild.
* @caching This method **does not** cache its result.
*/
async createFromTemplate(code, options) {
if (options.icon) {
options.icon = this._manager.client.util._convertImage(options.icon, "icon");
}
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_TEMPLATE_CODE(code),
json: {
icon: options.icon,
name: options.name
}
}).then(data => new Guild_1.default(data, this._manager.client, true));
}
/**
* Create a role.
* @param guildID The ID of the guild.
* @param options The options for creating the role.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#roles | Guild#roles}
*/
async createRole(guildID, options) {
const reason = options?.reason;
if (options?.reason) {
delete options.reason;
}
if (options?.icon) {
options.icon = this._manager.client.util._convertImage(options.icon, "icon");
}
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_ROLES(guildID),
json: {
color: options?.color,
hoist: options?.hoist,
icon: options?.icon,
mentionable: options?.mentionable,
name: options?.name,
permissions: options?.permissions,
unicode_emoji: options?.unicodeEmoji
},
reason
}).then(data => this._manager.client.guilds.get(guildID)?.roles.update(data, guildID) ?? new Role_1.default(data, this._manager.client, guildID));
}
/**
* Create a scheduled event in a guild.
* @param guildID The ID of the guild.
* @param options The options for creating the scheduled event.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#scheduledEvents | Guild#scheduledEvents}
*/
async createScheduledEvent(guildID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
if (options.image) {
options.image = this._manager.client.util._convertImage(options.image, "image");
}
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_SCHEDULED_EVENTS(guildID),
json: {
channel_id: options.channelID,
description: options.description,
entity_metadata: options.entityMetadata ? { location: options.entityMetadata.location } : undefined,
entity_type: options.entityType,
image: options.image,
name: options.name,
privacy_level: options.privacyLevel,
scheduled_end_time: options.scheduledEndTime,
scheduled_start_time: options.scheduledStartTime
},
reason
}).then(data => this._manager.client.guilds.get(guildID)?.scheduledEvents.update(data) ?? new GuildScheduledEvent_1.default(data, this._manager.client));
}
/**
* Create a soundboard sound
* @param guildID The ID of the guild
* @param options The options for creating the soundboard sound
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#soundboardSounds | Guild#soundboardSounds}
*/
async createSoundboardSound(guildID, options) {
const reason = options.reason;
if (options.sound) {
options.sound = this._manager.client.util._convertSound(options.sound, "sound");
}
return this._manager.authRequest({
method: "POST",
path: Routes.SOUNDBOARD_SOUNDS(guildID),
json: {
emoji_id: options.emojiID,
emoji_name: options.emojiName,
name: options.name,
sound: options.sound,
volume: options.volume
},
reason
}).then(data => this._manager.client.guilds.get(guildID)?.soundboardSounds.update(data) ?? new Soundboard_1.default(data, this._manager.client));
}
/**
* Create a sticker.
* @param guildID The ID of the guild.
* @param options The options for creating the sticker.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#stickers | Guild#stickers}<br>{@link Client#users | Client#users} (creator, if applicable)
*/
async createSticker(guildID, options) {
const magic = this._manager.client.util.getMagic(options.file.contents);
let mime;
switch (magic) {
// png & apng have the same magic
case "89504E47": {
mime = "image/png";
break;
}
// lottie
case "7B227622": {
mime = "application/json";
break;
}
}
const form = new FormData();
form.append("description", options.description);
form.append("name", options.name);
form.append("tags", options.tags);
form.append("file", new Blob([options.file.contents], { type: mime }), options.file.name);
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_STICKERS(guildID),
form,
reason: options.reason
}).then(data => this._manager.client.guilds.get(guildID)?.stickers.update(data) ?? this._manager.client.util.convertSticker(data));
}
/**
* Create a guild template.
* @param guildID The ID of the guild to create a template from.
* @param options The options for creating the template.
*/
async createTemplate(guildID, options) {
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_TEMPLATES(guildID),
json: {
description: options.description,
name: options.name
}
}).then(data => new GuildTemplate_1.default(data, this._manager.client));
}
/**
* Delete a guild.
* @param guildID The ID of the guild.
* @caching This method **does not** cache its result.
*/
async delete(guildID) {
await this._manager.authRequest({
method: "DELETE",
path: Routes.GUILD(guildID)
});
}
/**
* Delete an auto moderation rule.
* @param guildID The ID of the guild.
* @param ruleID The ID of the rule to delete.
* @param reason The reason for deleting the rule.
* @caching This method **does not** cache its result.
*/
async deleteAutoModerationRule(guildID, ruleID, reason) {
await this._manager.authRequest({
method: "DELETE",
path: Routes.GUILD_AUTOMOD_RULE(guildID, ruleID),
reason
});
}
/**
* Delete an emoji.
* @param guildID The ID of the guild.
* @param emojiID The ID of the emoji.
* @param reason The reason for deleting the emoji.
* @caching This method **does not** cache its result.
*/
async deleteEmoji(guildID, emojiID, reason) {
await this._manager.authRequest({
method: "DELETE",
path: Routes.GUILD_EMOJI(guildID, emojiID),
reason
});
}
/**
* Delete an integration.
* @param guildID The ID of the guild.
* @param integrationID The ID of the integration.
* @param reason The reason for deleting the integration.
* @caching This method **does not** cache its result.
*/
async deleteIntegration(guildID, integrationID, reason) {
await this._manager.authRequest({
method: "DELETE",
path: Routes.GUILD_INTEGRATION(guildID, integrationID),
reason
});
}
/**
* Delete a role.
* @param guildID The ID of the guild.
* @param roleID The ID of the role to delete.
* @param reason The reason for deleting the role.
* @caching This method **does not** cache its result.
*/
async deleteRole(guildID, roleID, reason) {
await this._manager.authRequest({
method: "DELETE",
path: Routes.GUILD_ROLE(guildID, roleID),
reason
});
}
/**
* Delete a scheduled event.
* @param guildID The ID of the guild.
* @param eventID The ID of the scheduled event.
* @param reason The reason for deleting the scheduled event. Discord's docs do not explicitly state a reason can be provided, so it may not be used.
* @caching This method **does not** cache its result.
*/
async deleteScheduledEvent(guildID, eventID, reason) {
await this._manager.authRequest({
method: "DELETE",
path: Routes.GUILD_SCHEDULED_EVENT(guildID, eventID),
reason
});
}
/**
*
* @param guildID The ID of the guild.
* @param soundID The ID of the soundboard sound to delete.
* @param reason The reason for deleting the soundboard sound.
* @caching This method **does not** cache its result.
*/
async deleteSoundboardSound(guildID, soundID, reason) {
await this._manager.authRequest({
method: "DELETE",
path: Routes.SOUNDBOARD_SOUND(guildID, soundID),
reason
});
}
/**
* Delete a sticker.
* @param guildID The ID of the guild.
* @param stickerID The ID of the sticker to delete.
* @param reason The reason for deleting the sticker.
* @caching This method **does not** cache its result.
*/
async deleteSticker(guildID, stickerID, reason) {
await this._manager.authRequest({
method: "DELETE",
path: Routes.GUILD_STICKER(guildID, stickerID),
reason
});
}
/**
* Delete a template.
* @param guildID The ID of the guild.
* @param code The code of the template.
* @caching This method **does not** cache its result.
*/
async deleteTemplate(guildID, code) {
await this._manager.authRequest({
method: "DELETE",
path: Routes.GUILD_TEMPLATE(guildID, code)
});
}
/**
* Edit a guild.
* @param guildID The ID of the guild.
* @param options The options for editing the guild.
* @caching This method **may** cache its result. The result will not be cached if the guild is not already cached.
* @caches {@link Client#guilds | Client#guilds}
*/
async edit(guildID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
if (options.banner) {
options.banner = this._manager.client.util._convertImage(options.banner, "banner");
}
if (options.discoverySplash) {
options.discoverySplash = this._manager.client.util._convertImage(options.discoverySplash, "discovery splash");
}
if (options.icon) {
options.icon = this._manager.client.util._convertImage(options.icon, "icon");
}
if (options.splash) {
options.splash = this._manager.client.util._convertImage(options.splash, "splash");
}
return this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD(guildID),
json: {
afk_channel_id: options.afkChannelID,
afk_timeout: options.afkTimeout,
banner: options.banner,
default_message_notifications: options.defaultMessageNotifications,
description: options.description,
discovery_splash: options.discoverySplash,
explicit_content_filter: options.explicitContentFilter,
features: options.features,
icon: options.icon,
name: options.name,
owner_id: options.ownerID,
preferred_locale: options.preferredLocale,
premium_progress_bar_enabled: options.premiumProgressBarEnabled,
public_updates_channel_id: options.publicUpdatesChannelID,
region: options.region,
rules_channel_id: options.rulesChannelID,
safety_alerts_channel_id: options.safetyAlertsChannelID,
splash: options.splash,
system_channel_flags: options.systemChannelFlags,
system_channel_id: options.systemChannelID,
verification_level: options.verificationLevel
},
reason
}).then(data => this._manager.client.guilds.has(guildID) ? this._manager.client.guilds.update(data, true) : new Guild_1.default(data, this._manager.client, true));
}
/**
* Edit an existing auto moderation rule.
* @param guildID The ID of the guild.
* @param ruleID The ID of the rule to edit.
* @param options The options for editing the rule.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#autoModerationRules | Guild#autoModerationRules}
*/
async editAutoModerationRule(guildID, ruleID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_AUTOMOD_RULE(guildID, ruleID),
json: {
actions: options.actions?.map(a => ({
metadata: {
channel_id: a.metadata.channelID,
custom_message: a.metadata.customMessage,
duration_seconds: a.metadata.durationSeconds
},
type: a.type
})),
enabled: options.enabled,
event_type: options.eventType,
exempt_channels: options.exemptChannels,
exempt_roles: options.exemptRoles,
name: options.name,
trigger_metadata: options.triggerMetadata ? {
allow_list: options.triggerMetadata.allowList,
keyword_filter: options.triggerMetadata.keywordFilter,
mention_raid_protection_enabled: options.triggerMetadata.mentionRaidProtectionEnabled,
mention_total_limit: options.triggerMetadata.mentionTotalLimit,
presets: options.triggerMetadata.presets,
regex_patterns: options.triggerMetadata.regexPatterns
} : undefined
},
reason
}).then(data => this._manager.client.guilds.get(guildID)?.autoModerationRules.update(data) ?? new AutoModerationRule_1.default(data, this._manager.client));
}
/**
* Edit the positions of channels in a guild.
* @param guildID The ID of the guild.
* @param options The channels to move. Unedited channels do not need to be specified.
* @caching This method **does not** cache its result.
*/
async editChannelPositions(guildID, options) {
await this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_CHANNELS(guildID),
json: options.map(o => ({
id: o.id,
// lock_permissions: o.lockPermissions ?? null,
// parent_id: o.parentID ?? null,
position: o.position ?? null
}))
});
}
/**
* Modify the current member in a guild.
* @param guildID The ID of the guild.
* @param options The options for editing the member.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#members | Guild#members}<br>{@link Guild#clientMember | Guild#clientMember}
*/
async editCurrentMember(guildID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_MEMBER(guildID, "@me"),
json: { nick: options.nick },
reason
}).then(data => this._manager.client.util.updateMember(guildID, data.user.id, data));
}
/**
* Edit the current member's voice state in a guild. `channelID` is required, and the current member must already be in that channel. See [Discord's docs](https://discord.com/developers/docs/resources/guild#modify-current-user-voice-state-caveats) for more information.
* @param guildID The ID of the guild.
* @param options The options for editing the voice state.
* @caching This method **does not** cache its result.
*/
async editCurrentUserVoiceState(guildID, options) {
await this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_VOICE_STATE(guildID, "@me"),
json: {
channel_id: options.channelID,
suppress: options.suppress,
request_to_speak_timestamp: options.requestToSpeakTimestamp
}
});
}
/**
* Edit an existing emoji.
* @param guildID The ID of the guild the emoji is in.
* @param options The options for editing the emoji.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#emojis | Guild#emojis}
*/
async editEmoji(guildID, emojiID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_EMOJI(guildID, emojiID),
json: {
name: options.name,
roles: options.roles
},
reason
}).then(data => this._manager.client.guilds.get(guildID)?.emojis.update(data) ?? this._manager.client.util.convertGuildEmoji(data));
}
/**
* Edit the incident actions for a guild.
* @param guildID The ID of the guild.
* @param options The options for editing the incident actions.
* @caching This method **does not** cache its result.
*/
async editIncidentActions(guildID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "PUT",
path: Routes.GUILD_INCIDENT_ACTIONS(guildID),
json: {
dmsDisabledUntil: options.dmsDisabledUntil,
invitesDisabledUntil: options.invitesDisabledUntil
},
reason
}).then(data => ({
dmsDisabledUntil: data.dms_disabled_until,
invitesDisabledUntil: data.invites_disabled_until
}));
}
/**
* Edit the [mfa level](https://discord.com/developers/docs/resources/guild#guild-object-mfa-level) of a guild. This can only be used by the guild owner.
* @param guildID The ID of the guild.
* @param options The options for editing the MFA level.
* @caching This method **does not** cache its result.
*/
async editMFALevel(guildID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_MFA(guildID),
json: { level: options.level },
reason
});
}
/**
* Edit a guild member. Use editCurrentMember if you wish to update the nick of this client using the `CHANGE_NICKNAME` permission.
* @param guildID The ID of the guild.
* @param memberID The ID of the member.
* @param options The options for editing the member.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#members | Guild#members}
*/
async editMember(guildID, memberID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_MEMBER(guildID, memberID),
json: {
channel_id: options.channelID,
communication_disabled_until: options.communicationDisabledUntil,
deaf: options.deaf,
flags: options.flags,
mute: options.mute,
nick: options.nick,
roles: options.roles
},
reason
}).then(data => this._manager.client.util.updateMember(guildID, memberID, data));
}
/**
* Edit a guild's onboarding configuration.
* @param guildID The ID of the guild.
* @param options The options for editing the onboarding configuration.
* @caching This method **does not** cache its result.
*/
async editOnboarding(guildID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_ONBOARDING(guildID),
json: {
enabled: options.enabled,
default_channel_ids: options.defaultChannelIDs,
prompts: options.prompts?.map(p => ({
id: p.id,
in_oboarding: p.inOnboarding,
options: p.options.map(o => ({
channel_ids: o.channelIDs,
description: o.description,
emoji: o.emoji,
id: o.id,
role_ids: o.roleIDs,
title: o.title
})),
required: p.required,
single_select: p.singleSelect,
title: p.title
})),
mode: options.mode
},
reason
}).then(data => ({
defaultChannelIDs: data.default_channel_ids,
enabled: data.enabled,
guildID: data.guild_id,
mode: data.mode,
prompts: data.prompts.map(p => ({
id: p.id,
inOnboarding: p.in_onboarding,
options: p.options.map(o => ({
channelIDs: o.channel_ids,
description: o.description,
emoji: o.emoji,
id: o.id,
roleIDs: o.role_ids,
title: o.title
})),
required: p.required,
singleSelect: p.single_select,
title: p.title
}))
}));
}
/**
* Edit an existing role.
* @param guildID The ID of the guild.
* @param options The options for editing the role.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#roles | Guild#roles}
*/
async editRole(guildID, roleID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
if (options.icon) {
options.icon = this._manager.client.util._convertImage(options.icon, "icon");
}
return this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_ROLE(guildID, roleID),
json: {
color: options.color,
hoist: options.hoist,
icon: options.icon,
mentionable: options.mentionable,
name: options.name,
permissions: options.permissions,
unicode_emoji: options.unicodeEmoji
},
reason
}).then(data => this._manager.client.guilds.get(guildID)?.roles.update(data, guildID) ?? new Role_1.default(data, this._manager.client, guildID));
}
/**
* Edit the position of roles in a guild.
* @param guildID The ID of the guild.
* @param options The roles to move.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#roles | Guild#roles}
*/
async editRolePositions(guildID, options, reason) {
const guild = this._manager.client.guilds.get(guildID);
return this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_ROLES(guildID),
json: options.map(o => ({
id: o.id,
position: o.position
})),
reason
}).then(data => data.map(role => guild?.roles.update(role, guildID) ?? new Role_1.default(role, this._manager.client, guildID)));
}
/**
* Edit an existing scheduled event in a guild.
* @param guildID The ID of the guild.
* @param options The options for editing the scheduled event.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#scheduledEvents | Guild#scheduledEvents}
*/
async editScheduledEvent(guildID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
if (options.image) {
options.image = this._manager.client.util._convertImage(options.image, "image");
}
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_SCHEDULED_EVENTS(guildID),
json: {
channel_id: options.channelID,
description: options.description,
entity_metadata: options.entityMetadata ? { location: options.entityMetadata.location } : undefined,
entity_type: options.entityType,
image: options.image,
name: options.name,
privacy_level: options.privacyLevel,
status: options.status,
scheduled_end_time: options.scheduledEndTime,
scheduled_start_time: options.scheduledStartTime
},
reason
}).then(data => this._manager.client.guilds.get(guildID)?.scheduledEvents.update(data) ?? new GuildScheduledEvent_1.default(data, this._manager.client));
}
/**
* Edit a soundboard sound.
* @param guildID The ID of the guild.
* @param soundID The ID of the soundboard sound to edit.
* @param options The options for editing the soundboard sound.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#soundboardSounds | Guild#soundboardSounds}
*/
async editSoundboardSound(guildID, soundID, options) {
const reason = options.reason;
return this._manager.authRequest({
method: "PATCH",
path: Routes.SOUNDBOARD_SOUND(guildID, soundID),
json: {
emoji_id: options.emojiID,
emoji_name: options.emojiName,
name: options.name,
volume: options.volume
},
reason
}).then(data => this._manager.client.guilds.get(guildID)?.soundboardSounds.update(data) ?? new Soundboard_1.default(data, this._manager.client));
}
/**
* Edit a sticker.
* @param guildID The ID of the guild.
* @param options The options for editing the sticker.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#stickers | Guild#stickers}
*/
async editSticker(guildID, stickerID, options) {
return this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_STICKER(guildID, stickerID),
json: {
description: options.description,
name: options.name,
tags: options.tags
},
reason: options.reason
}).then(data => this._manager.client.guilds.get(guildID)?.stickers.update(data) ?? this._manager.client.util.convertSticker(data));
}
/**
* Edit a guild template.
* @param guildID The ID of the guild.
* @param code The code of the template.
* @param options The options for editing the template.
* @caching This method **does not** cache its result.
*/
async editTemplate(guildID, code, options) {
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_TEMPLATE(guildID, code),
json: {
code,
description: options.description,
name: options.name
}
}).then(data => new GuildTemplate_1.default(data, this._manager.client));
}
/**
* Edit a guild member's voice state. `channelID` is required, and the user must already be in that channel. See [Discord's docs](https://discord.com/developers/docs/resources/guild#modify-user-voice-state) for more information.
* @param guildID The ID of the guild.
* @param memberID The ID of the member.
* @param options The options for editing the voice state.
* @caching This method **does not** cache its result.
*/
async editUserVoiceState(guildID, memberID, options) {
await this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_VOICE_STATE(guildID, memberID),
json: {
channel_id: options.channelID,
suppress: options.suppress
}
});
}
/**
* Edit the welcome screen in a guild.
* @param guildID The ID of the guild.
* @param options The options for editing the welcome screen.
* @caching This method **does not** cache its result.
*/
async editWelcomeScreen(guildID, options) {
const reason = options.reason;
if (options.reason) {
delete options.reason;
}
return this._manager.authRequest({
method: "PATCH",
path: Routes.GUILD_WELCOME_SCREEN(guildID),
json: {
description: options.description,
enabled: options.enabled,
welcome_channels: options.welcomeChannels.map(ch => ({
channel_id: ch.channelID,
description: ch.description,
emoji_id: ch.emojiID,
emoji_name: ch.emojiName
}))
},
reason
}).then(data => ({
description: data.description,
welcomeChannels: data.welcome_channels.map(channel => ({
channelID: channel.channel_id,
description: channel.description,
emojiID: channel.emoji_id,
emojiName: channel.emoji_name
}))
}));
}
/**
* Edit the widget of a guild.
* @param guildID The ID of the guild.
* @param options The options for editing the widget.
* @caching This method **does not** cache its result.
*/
async editWidget(guildID, options) {
return this._manager.authRequest({
method: "POST",
path: Routes.GUILD_WIDGET(guildID),
json: {
channel_id: options.channelID,
enabled: options.enabled
}
}).then(data => ({
channels: data.channels,
id: data.id,
instantInvite: data.instant_invite,
members: data.members.map(m => ({
activity: m.activity,
avatar: m.avatar,
avatarURL: m.avatar_url,
discriminator: m.discriminator,
id: m.id,
status: m.status,
tag: m.username,
username: m.username
})),
name: data.name,
presenceCount: data.presence_count
}));
}
/**
* Get a guild.
* @param guildID The ID of the guild.
* @param withCounts If the approximate number of members and online members should be included.
* @caching This method **may** cache its result. The result will not be cached if the guild is not already cached.
* @caches {@link Client#guilds | Client#guilds}
*/
async get(guildID, withCounts) {
const query = new URLSearchParams();
if (withCounts !== undefined) {
query.set("with_counts", withCounts.toString());
}
return this._manager.authRequest({
method: "GET",
path: Routes.GUILD(guildID),
query
}).then(data => this._manager.client.guilds.has(guildID) ? this._manager.client.guilds.update(data, true) : new Guild_1.default(data, this._manager.client, true));
}
/**
* Get the active threads in a guild.
* @param guildID The ID of the guild.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#threads | Guild#threads}
*/
async getActiveThreads(guildID) {
return this._manager.authRequest({
method: "GET",
path: Routes.GUILD_ACTIVE_THREADS(guildID)
}).then(data => ({
members: data.members.map(member => ({
flags: member.flags,
id: member.id,
joinTimestamp: new Date(member.join_timestamp),
userID: member.user_id
})),
threads: data.threads.map(rawThread => this._manager.client.util.updateThread(rawThread))
}));
}
/**
* Get a guild's audit log.
* @param guildID The ID of the guild.
* @param options The options for getting the audit logs.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#autoModerationRules | Guild#autoModerationRules}<br>{@link Guild#scheduledEvents | Guild#scheduledEvents}<br>{@link Guild#integrations | Guild#integrations}<br>{@link Guild#threads | Guild#threads}<br>{@link Client#users | Client#users}
*/
async getAuditLog(guildID, options) {
const guild = this._manager.client.guilds.get(guildID);
const query = new URLSearchParams();
if (options?.actionType !== undefined) {
query.set("action_type", options.actionType.toString());
}
if (options?.before !== undefined) {
query.set("before", options.before);
}
if (options?.limit !== undefined) {
query.set("limit", options.limit.toString());
}
if (options?.userID !== undefined) {
query.set("user_id", options.userID);
}
return this._manager.authRequest({
method: "GET",
path: Routes.GUILD_AUDIT_LOG(guildID),
query
}).then(data => ({
applicationCommands: data.application_commands.map(command => new ApplicationCommand_1.default(command, this._manager.client)),
autoModerationRules: data.auto_moderation_rules.map(rule => guild?.autoModerationRules.update(rule) ?? new AutoModerationRule_1.default(rule, this._manager.client)),
entries: data.audit_log_entries.map(entry => new AuditLogEntry_1.default(entry, this._manager.client)),
guildScheduledEvents: data.guild_scheduled_events.map(event => guild?.scheduledEvents.update(event) ?? new GuildScheduledEvent_1.default(event, this._manager.client)),
integrations: data.integrations.map(integration => guild?.integrations.update(integration, guildID) ?? new Integration_1.default(integration, this._manager.client, guildID)),
threads: data.threads.map(rawThread => this._manager.client.util.updateThread(rawThread)),
users: data.users.map(user => this._manager.client.users.update(user)),
webhooks: data.webhooks.map(webhook => new Webhook_1.default(webhook, this._manager.client))
}));
}
/**
* Get an auto moderation rule for a guild.
* @param guildID The ID of the guild.
* @param ruleID The ID of the rule to get.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild#autoModerationRules | Guild#autoModerationRules}
*/
async getAutoModerationRule(guildID, ruleID) {
return this._manager.authRequest({
method: "GET",
path: Routes.GUILD_AUTOMOD_RULE(guildID, ruleID)
}).then(data => this._manager.client.guilds.get(guildID)?.autoModerationRules.update(data) ?? new AutoModerationRule_1.default(data, this._manager.client));
}
/**
* Get the auto moderation rules for a guild.
* @param guildID The ID of the guild.
* @caching This method **may** cache its result. The result will not be cached if the guild is not cached.
* @caches {@link Guild