discord-sb.js 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +2 -0
- package/package.json +91 -0
- package/src/WebSocket.js +39 -0
- package/src/client/BaseClient.js +86 -0
- package/src/client/Client.js +978 -0
- package/src/client/WebhookClient.js +61 -0
- package/src/client/actions/Action.js +116 -0
- package/src/client/actions/ActionsManager.js +80 -0
- package/src/client/actions/ApplicationCommandPermissionsUpdate.js +34 -0
- package/src/client/actions/AutoModerationActionExecution.js +27 -0
- package/src/client/actions/AutoModerationRuleCreate.js +28 -0
- package/src/client/actions/AutoModerationRuleDelete.js +32 -0
- package/src/client/actions/AutoModerationRuleUpdate.js +30 -0
- package/src/client/actions/ChannelCreate.js +23 -0
- package/src/client/actions/ChannelDelete.js +39 -0
- package/src/client/actions/ChannelUpdate.js +43 -0
- package/src/client/actions/GuildAuditLogEntryCreate.js +29 -0
- package/src/client/actions/GuildBanAdd.js +20 -0
- package/src/client/actions/GuildBanRemove.js +25 -0
- package/src/client/actions/GuildChannelsPositionUpdate.js +21 -0
- package/src/client/actions/GuildDelete.js +65 -0
- package/src/client/actions/GuildEmojiCreate.js +20 -0
- package/src/client/actions/GuildEmojiDelete.js +21 -0
- package/src/client/actions/GuildEmojiUpdate.js +20 -0
- package/src/client/actions/GuildEmojisUpdate.js +34 -0
- package/src/client/actions/GuildIntegrationsUpdate.js +19 -0
- package/src/client/actions/GuildMemberRemove.js +33 -0
- package/src/client/actions/GuildMemberUpdate.js +44 -0
- package/src/client/actions/GuildRoleCreate.js +25 -0
- package/src/client/actions/GuildRoleDelete.js +31 -0
- package/src/client/actions/GuildRoleUpdate.js +39 -0
- package/src/client/actions/GuildRolesPositionUpdate.js +21 -0
- package/src/client/actions/GuildScheduledEventCreate.js +27 -0
- package/src/client/actions/GuildScheduledEventDelete.js +31 -0
- package/src/client/actions/GuildScheduledEventUpdate.js +30 -0
- package/src/client/actions/GuildScheduledEventUserAdd.js +32 -0
- package/src/client/actions/GuildScheduledEventUserRemove.js +32 -0
- package/src/client/actions/GuildStickerCreate.js +20 -0
- package/src/client/actions/GuildStickerDelete.js +21 -0
- package/src/client/actions/GuildStickerUpdate.js +20 -0
- package/src/client/actions/GuildStickersUpdate.js +34 -0
- package/src/client/actions/GuildUpdate.js +33 -0
- package/src/client/actions/InviteCreate.js +28 -0
- package/src/client/actions/InviteDelete.js +30 -0
- package/src/client/actions/MessageCreate.js +50 -0
- package/src/client/actions/MessageDelete.js +32 -0
- package/src/client/actions/MessageDeleteBulk.js +46 -0
- package/src/client/actions/MessagePollVoteAdd.js +33 -0
- package/src/client/actions/MessagePollVoteRemove.js +33 -0
- package/src/client/actions/MessageReactionAdd.js +68 -0
- package/src/client/actions/MessageReactionRemove.js +50 -0
- package/src/client/actions/MessageReactionRemoveAll.js +33 -0
- package/src/client/actions/MessageReactionRemoveEmoji.js +28 -0
- package/src/client/actions/MessageUpdate.js +26 -0
- package/src/client/actions/PresenceUpdate.js +50 -0
- package/src/client/actions/StageInstanceCreate.js +28 -0
- package/src/client/actions/StageInstanceDelete.js +33 -0
- package/src/client/actions/StageInstanceUpdate.js +30 -0
- package/src/client/actions/ThreadCreate.js +24 -0
- package/src/client/actions/ThreadDelete.js +32 -0
- package/src/client/actions/ThreadListSync.js +59 -0
- package/src/client/actions/ThreadMemberUpdate.js +30 -0
- package/src/client/actions/ThreadMembersUpdate.js +34 -0
- package/src/client/actions/TypingStart.js +29 -0
- package/src/client/actions/UserUpdate.js +35 -0
- package/src/client/actions/VoiceStateUpdate.js +50 -0
- package/src/client/actions/WebhooksUpdate.js +20 -0
- package/src/client/voice/ClientVoiceManager.js +151 -0
- package/src/client/voice/VoiceConnection.js +1249 -0
- package/src/client/voice/dispatcher/AnnexBDispatcher.js +120 -0
- package/src/client/voice/dispatcher/AudioDispatcher.js +145 -0
- package/src/client/voice/dispatcher/BaseDispatcher.js +459 -0
- package/src/client/voice/dispatcher/VPxDispatcher.js +54 -0
- package/src/client/voice/dispatcher/VideoDispatcher.js +68 -0
- package/src/client/voice/networking/VoiceUDPClient.js +173 -0
- package/src/client/voice/networking/VoiceWebSocket.js +286 -0
- package/src/client/voice/player/MediaPlayer.js +321 -0
- package/src/client/voice/player/processing/AnnexBNalSplitter.js +244 -0
- package/src/client/voice/player/processing/IvfSplitter.js +106 -0
- package/src/client/voice/player/processing/PCMInsertSilence.js +37 -0
- package/src/client/voice/receiver/PacketHandler.js +260 -0
- package/src/client/voice/receiver/Receiver.js +96 -0
- package/src/client/voice/receiver/Recorder.js +173 -0
- package/src/client/voice/util/Function.js +116 -0
- package/src/client/voice/util/PlayInterface.js +122 -0
- package/src/client/voice/util/Secretbox.js +64 -0
- package/src/client/voice/util/Silence.js +16 -0
- package/src/client/voice/util/Socket.js +62 -0
- package/src/client/voice/util/VolumeInterface.js +104 -0
- package/src/client/websocket/WebSocketManager.js +392 -0
- package/src/client/websocket/WebSocketShard.js +907 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_CREATE.js +18 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_DELETE.js +20 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_PERMISSIONS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_UPDATE.js +20 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_ACTION_EXECUTION.js +5 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_CREATE.js +5 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_DELETE.js +5 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/CALL_CREATE.js +14 -0
- package/src/client/websocket/handlers/CALL_DELETE.js +11 -0
- package/src/client/websocket/handlers/CALL_UPDATE.js +11 -0
- package/src/client/websocket/handlers/CHANNEL_CREATE.js +5 -0
- package/src/client/websocket/handlers/CHANNEL_DELETE.js +5 -0
- package/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js +22 -0
- package/src/client/websocket/handlers/CHANNEL_RECIPIENT_ADD.js +19 -0
- package/src/client/websocket/handlers/CHANNEL_RECIPIENT_REMOVE.js +16 -0
- package/src/client/websocket/handlers/CHANNEL_UPDATE.js +16 -0
- package/src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_BAN_ADD.js +5 -0
- package/src/client/websocket/handlers/GUILD_BAN_REMOVE.js +5 -0
- package/src/client/websocket/handlers/GUILD_CREATE.js +52 -0
- package/src/client/websocket/handlers/GUILD_DELETE.js +5 -0
- package/src/client/websocket/handlers/GUILD_EMOJIS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_INTEGRATIONS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_MEMBERS_CHUNK.js +39 -0
- package/src/client/websocket/handlers/GUILD_MEMBER_ADD.js +20 -0
- package/src/client/websocket/handlers/GUILD_MEMBER_REMOVE.js +5 -0
- package/src/client/websocket/handlers/GUILD_MEMBER_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_ROLE_CREATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_ROLE_DELETE.js +5 -0
- package/src/client/websocket/handlers/GUILD_ROLE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_CREATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_DELETE.js +5 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_USER_ADD.js +5 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_USER_REMOVE.js +5 -0
- package/src/client/websocket/handlers/GUILD_STICKERS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_UPDATE.js +5 -0
- package/src/client/websocket/handlers/INTERACTION_MODAL_CREATE.js +12 -0
- package/src/client/websocket/handlers/INVITE_CREATE.js +5 -0
- package/src/client/websocket/handlers/INVITE_DELETE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_CREATE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_DELETE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_DELETE_BULK.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_POLL_VOTE_ADD.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_POLL_VOTE_REMOVE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_ADD.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE_ALL.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE_EMOJI.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_UPDATE.js +16 -0
- package/src/client/websocket/handlers/PRESENCE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/READY.js +121 -0
- package/src/client/websocket/handlers/RELATIONSHIP_ADD.js +19 -0
- package/src/client/websocket/handlers/RELATIONSHIP_REMOVE.js +17 -0
- package/src/client/websocket/handlers/RELATIONSHIP_UPDATE.js +41 -0
- package/src/client/websocket/handlers/RESUMED.js +14 -0
- package/src/client/websocket/handlers/STAGE_INSTANCE_CREATE.js +5 -0
- package/src/client/websocket/handlers/STAGE_INSTANCE_DELETE.js +5 -0
- package/src/client/websocket/handlers/STAGE_INSTANCE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/THREAD_CREATE.js +5 -0
- package/src/client/websocket/handlers/THREAD_DELETE.js +5 -0
- package/src/client/websocket/handlers/THREAD_LIST_SYNC.js +5 -0
- package/src/client/websocket/handlers/THREAD_MEMBERS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/THREAD_MEMBER_UPDATE.js +5 -0
- package/src/client/websocket/handlers/THREAD_UPDATE.js +16 -0
- package/src/client/websocket/handlers/TYPING_START.js +5 -0
- package/src/client/websocket/handlers/USER_GUILD_SETTINGS_UPDATE.js +6 -0
- package/src/client/websocket/handlers/USER_NOTE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/USER_REQUIRED_ACTION_UPDATE.js +78 -0
- package/src/client/websocket/handlers/USER_SETTINGS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/USER_UPDATE.js +5 -0
- package/src/client/websocket/handlers/VOICE_CHANNEL_EFFECT_SEND.js +16 -0
- package/src/client/websocket/handlers/VOICE_CHANNEL_STATUS_UPDATE.js +12 -0
- package/src/client/websocket/handlers/VOICE_SERVER_UPDATE.js +6 -0
- package/src/client/websocket/handlers/VOICE_STATE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/WEBHOOKS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/index.js +84 -0
- package/src/errors/DJSError.js +61 -0
- package/src/errors/Messages.js +217 -0
- package/src/errors/index.js +4 -0
- package/src/index.js +172 -0
- package/src/managers/ApplicationCommandManager.js +264 -0
- package/src/managers/ApplicationCommandPermissionsManager.js +417 -0
- package/src/managers/AutoModerationRuleManager.js +296 -0
- package/src/managers/BaseGuildEmojiManager.js +80 -0
- package/src/managers/BaseManager.js +19 -0
- package/src/managers/BillingManager.js +66 -0
- package/src/managers/CachedManager.js +71 -0
- package/src/managers/ChannelManager.js +148 -0
- package/src/managers/ClientUserSettingManager.js +372 -0
- package/src/managers/DataManager.js +61 -0
- package/src/managers/DeveloperManager.js +254 -0
- package/src/managers/GuildBanManager.js +250 -0
- package/src/managers/GuildChannelManager.js +488 -0
- package/src/managers/GuildEmojiManager.js +171 -0
- package/src/managers/GuildEmojiRoleManager.js +118 -0
- package/src/managers/GuildForumThreadManager.js +108 -0
- package/src/managers/GuildInviteManager.js +213 -0
- package/src/managers/GuildManager.js +338 -0
- package/src/managers/GuildMemberManager.js +599 -0
- package/src/managers/GuildMemberRoleManager.js +195 -0
- package/src/managers/GuildScheduledEventManager.js +314 -0
- package/src/managers/GuildSettingManager.js +155 -0
- package/src/managers/GuildStickerManager.js +179 -0
- package/src/managers/GuildTextThreadManager.js +98 -0
- package/src/managers/InteractionManager.js +39 -0
- package/src/managers/MessageManager.js +423 -0
- package/src/managers/PermissionOverwriteManager.js +164 -0
- package/src/managers/PresenceManager.js +71 -0
- package/src/managers/QuestManager.js +353 -0
- package/src/managers/ReactionManager.js +67 -0
- package/src/managers/ReactionUserManager.js +73 -0
- package/src/managers/RelationshipManager.js +278 -0
- package/src/managers/RoleManager.js +448 -0
- package/src/managers/SessionManager.js +66 -0
- package/src/managers/StageInstanceManager.js +162 -0
- package/src/managers/ThreadManager.js +175 -0
- package/src/managers/ThreadMemberManager.js +186 -0
- package/src/managers/UserManager.js +136 -0
- package/src/managers/UserNoteManager.js +53 -0
- package/src/managers/VoiceStateManager.js +59 -0
- package/src/rest/APIRequest.js +154 -0
- package/src/rest/APIRouter.js +53 -0
- package/src/rest/DiscordAPIError.js +119 -0
- package/src/rest/HTTPError.js +62 -0
- package/src/rest/RESTManager.js +67 -0
- package/src/rest/RateLimitError.js +55 -0
- package/src/rest/RequestHandler.js +466 -0
- package/src/sharding/Shard.js +444 -0
- package/src/sharding/ShardClientUtil.js +279 -0
- package/src/sharding/ShardingManager.js +319 -0
- package/src/structures/AnonymousGuild.js +98 -0
- package/src/structures/ApplicationCommand.js +593 -0
- package/src/structures/ApplicationRoleConnectionMetadata.js +48 -0
- package/src/structures/AutoModerationActionExecution.js +89 -0
- package/src/structures/AutoModerationRule.js +294 -0
- package/src/structures/AutocompleteInteraction.js +107 -0
- package/src/structures/Base.js +43 -0
- package/src/structures/BaseCommandInteraction.js +211 -0
- package/src/structures/BaseGuild.js +116 -0
- package/src/structures/BaseGuildEmoji.js +56 -0
- package/src/structures/BaseGuildTextChannel.js +191 -0
- package/src/structures/BaseGuildVoiceChannel.js +241 -0
- package/src/structures/BaseMessageComponent.js +181 -0
- package/src/structures/ButtonInteraction.js +11 -0
- package/src/structures/CallState.js +63 -0
- package/src/structures/CategoryChannel.js +85 -0
- package/src/structures/Channel.js +284 -0
- package/src/structures/ClientPresence.js +77 -0
- package/src/structures/ClientUser.js +655 -0
- package/src/structures/CommandInteraction.js +41 -0
- package/src/structures/CommandInteractionOptionResolver.js +276 -0
- package/src/structures/ContainerComponent.js +68 -0
- package/src/structures/ContextMenuInteraction.js +65 -0
- package/src/structures/DMChannel.js +219 -0
- package/src/structures/DirectoryChannel.js +20 -0
- package/src/structures/Emoji.js +148 -0
- package/src/structures/FileComponent.js +49 -0
- package/src/structures/ForumChannel.js +31 -0
- package/src/structures/GroupDMChannel.js +394 -0
- package/src/structures/Guild.js +1791 -0
- package/src/structures/GuildAuditLogs.js +746 -0
- package/src/structures/GuildBan.js +59 -0
- package/src/structures/GuildBoost.js +108 -0
- package/src/structures/GuildChannel.js +470 -0
- package/src/structures/GuildEmoji.js +161 -0
- package/src/structures/GuildMember.js +636 -0
- package/src/structures/GuildPreview.js +191 -0
- package/src/structures/GuildPreviewEmoji.js +27 -0
- package/src/structures/GuildScheduledEvent.js +536 -0
- package/src/structures/GuildTemplate.js +236 -0
- package/src/structures/Integration.js +188 -0
- package/src/structures/IntegrationApplication.js +96 -0
- package/src/structures/Interaction.js +290 -0
- package/src/structures/InteractionCollector.js +248 -0
- package/src/structures/InteractionWebhook.js +43 -0
- package/src/structures/Invite.js +358 -0
- package/src/structures/InviteGuild.js +23 -0
- package/src/structures/InviteStageInstance.js +86 -0
- package/src/structures/MediaChannel.js +11 -0
- package/src/structures/MediaGalleryComponent.js +41 -0
- package/src/structures/MediaGalleryItem.js +47 -0
- package/src/structures/Message.js +1252 -0
- package/src/structures/MessageActionRow.js +105 -0
- package/src/structures/MessageAttachment.js +216 -0
- package/src/structures/MessageButton.js +166 -0
- package/src/structures/MessageCollector.js +146 -0
- package/src/structures/MessageComponentInteraction.js +120 -0
- package/src/structures/MessageContextMenuInteraction.js +20 -0
- package/src/structures/MessageEmbed.js +596 -0
- package/src/structures/MessageMentions.js +273 -0
- package/src/structures/MessagePayload.js +354 -0
- package/src/structures/MessageReaction.js +181 -0
- package/src/structures/MessageSelectMenu.js +141 -0
- package/src/structures/Modal.js +161 -0
- package/src/structures/ModalSubmitFieldsResolver.js +53 -0
- package/src/structures/ModalSubmitInteraction.js +119 -0
- package/src/structures/NewsChannel.js +32 -0
- package/src/structures/OAuth2Guild.js +28 -0
- package/src/structures/PermissionOverwrites.js +198 -0
- package/src/structures/Poll.js +108 -0
- package/src/structures/PollAnswer.js +88 -0
- package/src/structures/Presence.js +1157 -0
- package/src/structures/ReactionCollector.js +229 -0
- package/src/structures/ReactionEmoji.js +31 -0
- package/src/structures/Role.js +590 -0
- package/src/structures/SectionComponent.js +48 -0
- package/src/structures/SelectMenuInteraction.js +21 -0
- package/src/structures/SeparatorComponent.js +48 -0
- package/src/structures/Session.js +81 -0
- package/src/structures/StageChannel.js +104 -0
- package/src/structures/StageInstance.js +208 -0
- package/src/structures/Sticker.js +310 -0
- package/src/structures/StickerPack.js +95 -0
- package/src/structures/StoreChannel.js +56 -0
- package/src/structures/Team.js +118 -0
- package/src/structures/TeamMember.js +80 -0
- package/src/structures/TextChannel.js +33 -0
- package/src/structures/TextDisplayComponent.js +40 -0
- package/src/structures/TextInputComponent.js +132 -0
- package/src/structures/ThreadChannel.js +605 -0
- package/src/structures/ThreadMember.js +105 -0
- package/src/structures/ThreadOnlyChannel.js +249 -0
- package/src/structures/ThumbnailComponent.js +57 -0
- package/src/structures/Typing.js +74 -0
- package/src/structures/UnfurledMediaItem.js +29 -0
- package/src/structures/User.js +640 -0
- package/src/structures/UserContextMenuInteraction.js +29 -0
- package/src/structures/VoiceChannel.js +110 -0
- package/src/structures/VoiceChannelEffect.js +69 -0
- package/src/structures/VoiceRegion.js +53 -0
- package/src/structures/VoiceState.js +354 -0
- package/src/structures/WebEmbed.js +373 -0
- package/src/structures/Webhook.js +478 -0
- package/src/structures/WelcomeChannel.js +60 -0
- package/src/structures/WelcomeScreen.js +48 -0
- package/src/structures/Widget.js +87 -0
- package/src/structures/WidgetMember.js +99 -0
- package/src/structures/interfaces/Application.js +953 -0
- package/src/structures/interfaces/Collector.js +300 -0
- package/src/structures/interfaces/InteractionResponses.js +313 -0
- package/src/structures/interfaces/TextBasedChannel.js +821 -0
- package/src/util/APITypes.js +59 -0
- package/src/util/ActivityFlags.js +44 -0
- package/src/util/ApplicationFlags.js +76 -0
- package/src/util/AttachmentFlags.js +38 -0
- package/src/util/BitField.js +170 -0
- package/src/util/ChannelFlags.js +45 -0
- package/src/util/Constants.js +1914 -0
- package/src/util/DataResolver.js +146 -0
- package/src/util/Formatters.js +228 -0
- package/src/util/GuildMemberFlags.js +43 -0
- package/src/util/Intents.js +74 -0
- package/src/util/InviteFlags.js +34 -0
- package/src/util/LimitedCollection.js +131 -0
- package/src/util/MessageFlags.js +63 -0
- package/src/util/Options.js +358 -0
- package/src/util/Permissions.js +202 -0
- package/src/util/PremiumUsageFlags.js +31 -0
- package/src/util/PurchasedFlags.js +33 -0
- package/src/util/RemoteAuth.js +415 -0
- package/src/util/RoleFlags.js +37 -0
- package/src/util/SnowflakeUtil.js +92 -0
- package/src/util/Speaking.js +33 -0
- package/src/util/Sweepers.js +466 -0
- package/src/util/SystemChannelFlags.js +55 -0
- package/src/util/ThreadMemberFlags.js +30 -0
- package/src/util/UserFlags.js +104 -0
- package/src/util/Util.js +1048 -0
- package/typings/enums.d.ts +439 -0
- package/typings/index.d.ts +8505 -0
- package/typings/index.test-d.ts +0 -0
- package/typings/rawDataTypes.d.ts +403 -0
|
@@ -0,0 +1,821 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/* eslint-disable import/order */
|
|
4
|
+
const MessageCollector = require('../MessageCollector');
|
|
5
|
+
const MessagePayload = require('../MessagePayload');
|
|
6
|
+
const { InteractionTypes, ApplicationCommandOptionTypes, Events } = require('../../util/Constants');
|
|
7
|
+
const { Error } = require('../../errors');
|
|
8
|
+
const SnowflakeUtil = require('../../util/SnowflakeUtil');
|
|
9
|
+
const { setTimeout } = require('node:timers');
|
|
10
|
+
const { s } = require('@sapphire/shapeshift');
|
|
11
|
+
const Util = require('../../util/Util');
|
|
12
|
+
const validateName = stringName =>
|
|
13
|
+
s
|
|
14
|
+
.string()
|
|
15
|
+
.lengthGreaterThanOrEqual(1)
|
|
16
|
+
.lengthLessThanOrEqual(32)
|
|
17
|
+
.regex(/^[\p{Ll}\p{Lm}\p{Lo}\p{N}\p{sc=Devanagari}\p{sc=Thai}_-]+$/u)
|
|
18
|
+
.setValidationEnabled(true)
|
|
19
|
+
.parse(stringName);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Interface for classes that have text-channel-like features.
|
|
23
|
+
* @interface
|
|
24
|
+
*/
|
|
25
|
+
class TextBasedChannel {
|
|
26
|
+
constructor() {
|
|
27
|
+
/**
|
|
28
|
+
* A manager of the messages sent to this channel
|
|
29
|
+
* @type {MessageManager}
|
|
30
|
+
*/
|
|
31
|
+
this.messages = new MessageManager(this);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The channel's last message id, if one was sent
|
|
35
|
+
* @type {?Snowflake}
|
|
36
|
+
*/
|
|
37
|
+
this.lastMessageId = null;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The timestamp when the last pinned message was pinned, if there was one
|
|
41
|
+
* @type {?number}
|
|
42
|
+
*/
|
|
43
|
+
this.lastPinTimestamp = null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The Message object of the last message in the channel, if one was sent
|
|
48
|
+
* @type {?Message}
|
|
49
|
+
* @readonly
|
|
50
|
+
*/
|
|
51
|
+
get lastMessage() {
|
|
52
|
+
return this.messages.resolve(this.lastMessageId);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The date when the last pinned message was pinned, if there was one
|
|
57
|
+
* @type {?Date}
|
|
58
|
+
* @readonly
|
|
59
|
+
*/
|
|
60
|
+
get lastPinAt() {
|
|
61
|
+
return this.lastPinTimestamp ? new Date(this.lastPinTimestamp) : null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Represents the data for a poll answer.
|
|
66
|
+
* @typedef {Object} PollAnswerData
|
|
67
|
+
* @property {string} text The text for the poll answer
|
|
68
|
+
* @property {EmojiIdentifierResolvable} [emoji] The emoji for the poll answer
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Represents the data for a poll.
|
|
73
|
+
* @typedef {Object} PollData
|
|
74
|
+
* @property {PollQuestionMedia} question The question for the poll
|
|
75
|
+
* @property {PollAnswerData[]} answers The answers for the poll
|
|
76
|
+
* @property {number} duration The duration in hours for the poll
|
|
77
|
+
* @property {boolean} allowMultiselect Whether the poll allows multiple answers
|
|
78
|
+
* @property {PollLayoutType} [layoutType] The layout type for the poll
|
|
79
|
+
*/
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Base options provided when sending.
|
|
83
|
+
* @typedef {Object} BaseMessageOptions
|
|
84
|
+
* @property {MessageActivity} [activity] Group activity
|
|
85
|
+
* @property {boolean} [tts=false] Whether or not the message should be spoken aloud
|
|
86
|
+
* @property {string} [nonce=''] The nonce for the message
|
|
87
|
+
* @property {string} [content=''] The content for the message
|
|
88
|
+
* @property {Array<(MessageEmbed|APIEmbed)>} [embeds] The embeds for the message
|
|
89
|
+
* (see [here](https://discord.com/developers/docs/resources/channel#embed-object) for more details)
|
|
90
|
+
* @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content
|
|
91
|
+
* (see [here](https://discord.com/developers/docs/resources/channel#allowed-mentions-object) for more details)
|
|
92
|
+
* @property {Array<(FileOptions|BufferResolvable|MessageAttachment[])>} [files] Files to send with the message
|
|
93
|
+
* @property {Array<(MessageActionRow|MessageActionRowOptions)>} [components]
|
|
94
|
+
* Action rows containing interactive components for the message (buttons, select menus)
|
|
95
|
+
* @property {MessageAttachment[]} [attachments] Attachments to send in the message
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The base message options for messages including a poll.
|
|
100
|
+
* @typedef {BaseMessageOptions} BaseMessageOptionsWithPoll
|
|
101
|
+
* @property {PollData} [poll] The poll to send with the message
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @typedef {Object} ForwardOptions
|
|
106
|
+
* @property {MessageResolvable} message The originating message
|
|
107
|
+
* @property {TextBasedChannelResolvable} [channel] The channel of the originating message
|
|
108
|
+
* @property {GuildResolvable} [guild] The guild of the originating message
|
|
109
|
+
*/
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Options provided when sending or editing a message.
|
|
113
|
+
* @typedef {BaseMessageOptions} MessageOptions
|
|
114
|
+
* @property {ReplyOptions} [reply] The options for replying to a message
|
|
115
|
+
* @property {ForwardOptions} [forward] The options for forwarding a message
|
|
116
|
+
* @property {StickerResolvable[]} [stickers=[]] Stickers to send in the message
|
|
117
|
+
* @property {MessageFlags} [flags] Which flags to set for the message.
|
|
118
|
+
* Only `SUPPRESS_EMBEDS`, `SUPPRESS_NOTIFICATIONS` and `IS_VOICE_MESSAGE` can be set.
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Options provided to control parsing of mentions by Discord
|
|
123
|
+
* @typedef {Object} MessageMentionOptions
|
|
124
|
+
* @property {MessageMentionTypes[]} [parse] Types of mentions to be parsed
|
|
125
|
+
* @property {Snowflake[]} [users] Snowflakes of Users to be parsed as mentions
|
|
126
|
+
* @property {Snowflake[]} [roles] Snowflakes of Roles to be parsed as mentions
|
|
127
|
+
* @property {boolean} [repliedUser=true] Whether the author of the Message being replied to should be pinged
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Types of mentions to enable in MessageMentionOptions.
|
|
132
|
+
* - `roles`
|
|
133
|
+
* - `users`
|
|
134
|
+
* - `everyone`
|
|
135
|
+
* @typedef {string} MessageMentionTypes
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* @typedef {Object} FileOptions
|
|
140
|
+
* @property {BufferResolvable} attachment File to attach
|
|
141
|
+
* @property {string} [name='file.jpg'] Filename of the attachment
|
|
142
|
+
* @property {string} description The description of the file
|
|
143
|
+
*/
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Options for sending a message with a reply.
|
|
147
|
+
* @typedef {Object} ReplyOptions
|
|
148
|
+
* @property {MessageResolvable} messageReference The message to reply to (must be in the same channel and not system)
|
|
149
|
+
* @property {boolean} [failIfNotExists=true] Whether to error if the referenced message
|
|
150
|
+
* does not exist (creates a standard message in this case when false)
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Sends a message to this channel.
|
|
155
|
+
* @param {string|MessagePayload|MessageOptions} options The options to provide
|
|
156
|
+
* @returns {Promise<Message>}
|
|
157
|
+
* @example
|
|
158
|
+
* // Send a basic message
|
|
159
|
+
* channel.send('hello!')
|
|
160
|
+
* .then(message => console.log(`Sent message: ${message.content}`))
|
|
161
|
+
* .catch(console.error);
|
|
162
|
+
* @example
|
|
163
|
+
* // Send a remote file
|
|
164
|
+
* channel.send({
|
|
165
|
+
* files: ['https://cdn.discordapp.com/icons/222078108977594368/6e1019b3179d71046e463a75915e7244.png?size=2048']
|
|
166
|
+
* })
|
|
167
|
+
* .then(console.log)
|
|
168
|
+
* .catch(console.error);
|
|
169
|
+
* @example
|
|
170
|
+
* // Send a local file
|
|
171
|
+
* channel.send({
|
|
172
|
+
* files: [{
|
|
173
|
+
* attachment: 'entire/path/to/file.jpg',
|
|
174
|
+
* name: 'file.jpg',
|
|
175
|
+
* description: 'A description of the file'
|
|
176
|
+
* }]
|
|
177
|
+
* })
|
|
178
|
+
* .then(console.log)
|
|
179
|
+
* .catch(console.error);
|
|
180
|
+
*/
|
|
181
|
+
async send(options) {
|
|
182
|
+
const User = require('../User');
|
|
183
|
+
const { GuildMember } = require('../GuildMember');
|
|
184
|
+
|
|
185
|
+
if (this instanceof User || this instanceof GuildMember) {
|
|
186
|
+
const dm = await this.createDM();
|
|
187
|
+
return dm.send(options);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
let messagePayload;
|
|
191
|
+
|
|
192
|
+
if (options instanceof MessagePayload) {
|
|
193
|
+
messagePayload = options.resolveData();
|
|
194
|
+
} else {
|
|
195
|
+
messagePayload = MessagePayload.create(this, options).resolveData();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const { data, files } = await messagePayload.resolveFiles();
|
|
199
|
+
// New API
|
|
200
|
+
const attachments = await Util.getUploadURL(this.client, this.id, files);
|
|
201
|
+
const requestPromises = attachments.map(async attachment => {
|
|
202
|
+
await Util.uploadFile(files[attachment.id].file, attachment.upload_url);
|
|
203
|
+
return {
|
|
204
|
+
id: attachment.id,
|
|
205
|
+
filename: files[attachment.id].name,
|
|
206
|
+
uploaded_filename: attachment.upload_filename,
|
|
207
|
+
description: files[attachment.id].description,
|
|
208
|
+
duration_secs: files[attachment.id].duration_secs,
|
|
209
|
+
waveform: files[attachment.id].waveform,
|
|
210
|
+
};
|
|
211
|
+
});
|
|
212
|
+
const attachmentsData = await Promise.all(requestPromises);
|
|
213
|
+
attachmentsData.sort((a, b) => parseInt(a.id) - parseInt(b.id));
|
|
214
|
+
data.attachments = attachmentsData;
|
|
215
|
+
// Empty Files
|
|
216
|
+
const d = await this.client.api.channels[this.id].messages.post({ data });
|
|
217
|
+
|
|
218
|
+
return this.messages.cache.get(d.id) ?? this.messages._add(d);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
searchInteractionFromGuildAndPrivateChannel() {
|
|
222
|
+
// Support Slash / ContextMenu
|
|
223
|
+
// API https://canary.discord.com/api/v9/guilds/:id/application-command-index // Guild
|
|
224
|
+
// https://canary.discord.com/api/v9/channels/:id/application-command-index // DM Channel
|
|
225
|
+
// Updated: 07/01/2023
|
|
226
|
+
return this.client.api[this.guild ? 'guilds' : 'channels'][this.guild?.id || this.id]['application-command-index']
|
|
227
|
+
.get()
|
|
228
|
+
.catch(() => ({
|
|
229
|
+
application_commands: [],
|
|
230
|
+
applications: [],
|
|
231
|
+
version: '',
|
|
232
|
+
}));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
searchInteractionUserApps() {
|
|
236
|
+
return this.client.api.users['@me']['application-command-index'].get().catch(() => ({
|
|
237
|
+
application_commands: [],
|
|
238
|
+
applications: [],
|
|
239
|
+
version: '',
|
|
240
|
+
}));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
searchInteraction() {
|
|
244
|
+
return Promise.all([this.searchInteractionFromGuildAndPrivateChannel(), this.searchInteractionUserApps()]).then(
|
|
245
|
+
([dataA, dataB]) => ({
|
|
246
|
+
applications: [...dataA.applications, ...dataB.applications],
|
|
247
|
+
application_commands: [...dataA.application_commands, ...dataB.application_commands],
|
|
248
|
+
}),
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async sendSlash(botOrApplicationId, commandNameString, ...args) {
|
|
253
|
+
// Parse commandName /role add user
|
|
254
|
+
const cmd = commandNameString.trim().split(' ');
|
|
255
|
+
// Ex: role add user => [role, add, user]
|
|
256
|
+
// Parse: name, subGr, sub
|
|
257
|
+
const commandName = validateName(cmd[0]);
|
|
258
|
+
// Parse: role
|
|
259
|
+
const sub = cmd.slice(1);
|
|
260
|
+
// Parse: [add, user]
|
|
261
|
+
for (let i = 0; i < sub.length; i++) {
|
|
262
|
+
if (sub.length > 2) {
|
|
263
|
+
throw new Error('INVALID_COMMAND_NAME', cmd);
|
|
264
|
+
}
|
|
265
|
+
validateName(sub[i]);
|
|
266
|
+
}
|
|
267
|
+
// Search all
|
|
268
|
+
const data = await this.searchInteraction();
|
|
269
|
+
// Find command...
|
|
270
|
+
const filterCommand = data.application_commands.filter(obj =>
|
|
271
|
+
// Filter: name | name_default
|
|
272
|
+
[obj.name, obj.name_default].includes(commandName),
|
|
273
|
+
);
|
|
274
|
+
// Filter Bot
|
|
275
|
+
botOrApplicationId = this.client.users.resolveId(botOrApplicationId);
|
|
276
|
+
const application = data.applications.find(obj => obj.id == botOrApplicationId || obj.bot_id == botOrApplicationId);
|
|
277
|
+
if (!application) {
|
|
278
|
+
throw new Error('INVALID_APPLICATION_COMMAND', "Bot/Application doesn't exist");
|
|
279
|
+
}
|
|
280
|
+
// Find Command with application
|
|
281
|
+
const command = filterCommand.find(command => command.application_id == application.id);
|
|
282
|
+
if (!command) {
|
|
283
|
+
throw new Error('INVALID_APPLICATION_COMMAND', application.id);
|
|
284
|
+
}
|
|
285
|
+
args = args.flat(2);
|
|
286
|
+
let optionFormat = [];
|
|
287
|
+
let attachments = [];
|
|
288
|
+
let optionsMaxdepth, subGroup, subCommand;
|
|
289
|
+
if (sub.length == 2) {
|
|
290
|
+
// Subcommand Group > Subcommand
|
|
291
|
+
// Find Sub group
|
|
292
|
+
subGroup = command.options.find(
|
|
293
|
+
obj =>
|
|
294
|
+
obj.type == ApplicationCommandOptionTypes.SUB_COMMAND_GROUP && [obj.name, obj.name_default].includes(sub[0]),
|
|
295
|
+
);
|
|
296
|
+
if (!subGroup) throw new Error('SLASH_COMMAND_SUB_COMMAND_GROUP_INVALID', sub[0]);
|
|
297
|
+
// Find Sub
|
|
298
|
+
subCommand = subGroup.options.find(
|
|
299
|
+
obj => obj.type == ApplicationCommandOptionTypes.SUB_COMMAND && [obj.name, obj.name_default].includes(sub[1]),
|
|
300
|
+
);
|
|
301
|
+
if (!subCommand) throw new Error('SLASH_COMMAND_SUB_COMMAND_INVALID', sub[1]);
|
|
302
|
+
// Options
|
|
303
|
+
optionsMaxdepth = subCommand.options;
|
|
304
|
+
} else if (sub.length == 1) {
|
|
305
|
+
// Subcommand
|
|
306
|
+
subCommand = command.options.find(
|
|
307
|
+
obj => obj.type == ApplicationCommandOptionTypes.SUB_COMMAND && [obj.name, obj.name_default].includes(sub[0]),
|
|
308
|
+
);
|
|
309
|
+
if (!subCommand) throw new Error('SLASH_COMMAND_SUB_COMMAND_INVALID', sub[0]);
|
|
310
|
+
// Options
|
|
311
|
+
optionsMaxdepth = subCommand.options;
|
|
312
|
+
} else {
|
|
313
|
+
optionsMaxdepth = command.options;
|
|
314
|
+
}
|
|
315
|
+
const valueRequired = optionsMaxdepth?.filter(o => o.required).length || 0;
|
|
316
|
+
for (let i = 0; i < Math.min(args.length, optionsMaxdepth?.length || 0); i++) {
|
|
317
|
+
const optionInput = optionsMaxdepth[i];
|
|
318
|
+
const value = args[i];
|
|
319
|
+
const parseData = await parseOption(
|
|
320
|
+
this.client,
|
|
321
|
+
optionInput,
|
|
322
|
+
value,
|
|
323
|
+
optionFormat,
|
|
324
|
+
attachments,
|
|
325
|
+
command,
|
|
326
|
+
application.id,
|
|
327
|
+
this.guild?.id,
|
|
328
|
+
this.id,
|
|
329
|
+
subGroup,
|
|
330
|
+
subCommand,
|
|
331
|
+
);
|
|
332
|
+
optionFormat = parseData.optionFormat;
|
|
333
|
+
attachments = parseData.attachments;
|
|
334
|
+
}
|
|
335
|
+
if (valueRequired > args.length) {
|
|
336
|
+
throw new Error('SLASH_COMMAND_REQUIRED_OPTIONS_MISSING', valueRequired, optionFormat.length);
|
|
337
|
+
}
|
|
338
|
+
// Post
|
|
339
|
+
let postData;
|
|
340
|
+
if (subGroup) {
|
|
341
|
+
postData = [
|
|
342
|
+
{
|
|
343
|
+
type: ApplicationCommandOptionTypes.SUB_COMMAND_GROUP,
|
|
344
|
+
name: subGroup.name,
|
|
345
|
+
options: [
|
|
346
|
+
{
|
|
347
|
+
type: ApplicationCommandOptionTypes.SUB_COMMAND,
|
|
348
|
+
name: subCommand.name,
|
|
349
|
+
options: optionFormat,
|
|
350
|
+
},
|
|
351
|
+
],
|
|
352
|
+
},
|
|
353
|
+
];
|
|
354
|
+
} else if (subCommand) {
|
|
355
|
+
postData = [
|
|
356
|
+
{
|
|
357
|
+
type: ApplicationCommandOptionTypes.SUB_COMMAND,
|
|
358
|
+
name: subCommand.name,
|
|
359
|
+
options: optionFormat,
|
|
360
|
+
},
|
|
361
|
+
];
|
|
362
|
+
} else {
|
|
363
|
+
postData = optionFormat;
|
|
364
|
+
}
|
|
365
|
+
const nonce = SnowflakeUtil.generate();
|
|
366
|
+
const body = createPostData(
|
|
367
|
+
this.client,
|
|
368
|
+
false,
|
|
369
|
+
application.id,
|
|
370
|
+
nonce,
|
|
371
|
+
this.guild?.id,
|
|
372
|
+
Boolean(command.guild_id),
|
|
373
|
+
this.id,
|
|
374
|
+
command.version,
|
|
375
|
+
command.id,
|
|
376
|
+
command.name_default || command.name,
|
|
377
|
+
command.type,
|
|
378
|
+
postData,
|
|
379
|
+
attachments,
|
|
380
|
+
);
|
|
381
|
+
this.client.api.interactions.post({
|
|
382
|
+
data: body,
|
|
383
|
+
usePayloadJSON: true,
|
|
384
|
+
});
|
|
385
|
+
return Util.createPromiseInteraction(this.client, nonce, 5000);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Search for messages in this channel
|
|
390
|
+
* @param {ChannelSearchOptions} [options] Search options
|
|
391
|
+
* @returns {Promise<Object>} The search results
|
|
392
|
+
* @example
|
|
393
|
+
* // Search for messages by author
|
|
394
|
+
* channel.search({ authorId: '123456789012345678' });
|
|
395
|
+
*
|
|
396
|
+
* // Search for messages with images
|
|
397
|
+
* channel.search({ has: ['image'] });
|
|
398
|
+
*
|
|
399
|
+
* // Search for pinned messages
|
|
400
|
+
* channel.search({ pinned: true });
|
|
401
|
+
*/
|
|
402
|
+
async search(options = {}) {
|
|
403
|
+
const {
|
|
404
|
+
authorId,
|
|
405
|
+
mentions,
|
|
406
|
+
has = [],
|
|
407
|
+
pinned,
|
|
408
|
+
sortBy = 'timestamp',
|
|
409
|
+
sortOrder = 'desc',
|
|
410
|
+
offset = 0,
|
|
411
|
+
limit,
|
|
412
|
+
maxTime,
|
|
413
|
+
} = options;
|
|
414
|
+
|
|
415
|
+
const query = {
|
|
416
|
+
sort_by: sortBy,
|
|
417
|
+
sort_order: sortOrder,
|
|
418
|
+
offset: offset,
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
if (maxTime) {
|
|
422
|
+
const time = new Date(maxTime).getTime();
|
|
423
|
+
const maxId = (BigInt(time) - 1420070400000n) << 22n;
|
|
424
|
+
query.max_id = maxId.toString();
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
if (authorId) query.author_id = authorId;
|
|
428
|
+
if (mentions) query.mentions = mentions;
|
|
429
|
+
if (pinned) query.pinned = true;
|
|
430
|
+
|
|
431
|
+
for (const hasType of has) {
|
|
432
|
+
if (!query.has) query.has = [];
|
|
433
|
+
query.has.push(hasType);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if (this.guild) query.channel_id = this.id;
|
|
437
|
+
|
|
438
|
+
const endpoint = this.guild
|
|
439
|
+
? this.client.api.guilds(this.guild.id).messages.search
|
|
440
|
+
: this.client.api.channels(this.id).messages.search;
|
|
441
|
+
|
|
442
|
+
const data = await endpoint.get({ query });
|
|
443
|
+
|
|
444
|
+
if (limit && data.messages) data.messages = data.messages.flat().slice(0, limit);
|
|
445
|
+
|
|
446
|
+
return data;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Sends a typing indicator in the channel.
|
|
451
|
+
* @returns {Promise<{ message_send_cooldown_ms: number, thread_create_cooldown_ms: number }|void>} Resolves upon the typing status being sent
|
|
452
|
+
* @example
|
|
453
|
+
* // Start typing in a channel
|
|
454
|
+
* channel.sendTyping();
|
|
455
|
+
*/
|
|
456
|
+
sendTyping() {
|
|
457
|
+
return this.client.api.channels(this.id).typing.post();
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Creates a Message Collector.
|
|
462
|
+
* @param {MessageCollectorOptions} [options={}] The options to pass to the collector
|
|
463
|
+
* @returns {MessageCollector}
|
|
464
|
+
* @example
|
|
465
|
+
* // Create a message collector
|
|
466
|
+
* const filter = m => m.content.includes('discord');
|
|
467
|
+
* const collector = channel.createMessageCollector({ filter, time: 15_000 });
|
|
468
|
+
* collector.on('collect', m => console.log(`Collected ${m.content}`));
|
|
469
|
+
* collector.on('end', collected => console.log(`Collected ${collected.size} items`));
|
|
470
|
+
*/
|
|
471
|
+
createMessageCollector(options = {}) {
|
|
472
|
+
return new MessageCollector(this, options);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* An object containing the same properties as CollectorOptions, but a few more:
|
|
477
|
+
* @typedef {MessageCollectorOptions} AwaitMessagesOptions
|
|
478
|
+
* @property {string[]} [errors] Stop/end reasons that cause the promise to reject
|
|
479
|
+
*/
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Similar to createMessageCollector but in promise form.
|
|
483
|
+
* Resolves with a collection of messages that pass the specified filter.
|
|
484
|
+
* @param {AwaitMessagesOptions} [options={}] Optional options to pass to the internal collector
|
|
485
|
+
* @returns {Promise<Collection<Snowflake, Message>>}
|
|
486
|
+
* @example
|
|
487
|
+
* // Await !vote messages
|
|
488
|
+
* const filter = m => m.content.startsWith('!vote');
|
|
489
|
+
* // Errors: ['time'] treats ending because of the time limit as an error
|
|
490
|
+
* channel.awaitMessages({ filter, max: 4, time: 60_000, errors: ['time'] })
|
|
491
|
+
* .then(collected => console.log(collected.size))
|
|
492
|
+
* .catch(collected => console.log(`After a minute, only ${collected.size} out of 4 voted.`));
|
|
493
|
+
*/
|
|
494
|
+
awaitMessages(options = {}) {
|
|
495
|
+
return new Promise((resolve, reject) => {
|
|
496
|
+
const collector = this.createMessageCollector(options);
|
|
497
|
+
collector.once('end', (collection, reason) => {
|
|
498
|
+
if (options.errors?.includes(reason)) {
|
|
499
|
+
reject(collection);
|
|
500
|
+
} else {
|
|
501
|
+
resolve(collection);
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Fetches all webhooks for the channel.
|
|
509
|
+
* @returns {Promise<Collection<Snowflake, Webhook>>}
|
|
510
|
+
* @example
|
|
511
|
+
* // Fetch webhooks
|
|
512
|
+
* channel.fetchWebhooks()
|
|
513
|
+
* .then(hooks => console.log(`This channel has ${hooks.size} hooks`))
|
|
514
|
+
* .catch(console.error);
|
|
515
|
+
*/
|
|
516
|
+
fetchWebhooks() {
|
|
517
|
+
return this.guild.channels.fetchWebhooks(this.id);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Options used to create a {@link Webhook} in a guild text-based channel.
|
|
522
|
+
* @typedef {Object} ChannelWebhookCreateOptions
|
|
523
|
+
* @property {?(BufferResolvable|Base64Resolvable)} [avatar] Avatar for the webhook
|
|
524
|
+
* @property {string} [reason] Reason for creating the webhook
|
|
525
|
+
*/
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Creates a webhook for the channel.
|
|
529
|
+
* @param {string} name The name of the webhook
|
|
530
|
+
* @param {ChannelWebhookCreateOptions} [options] Options for creating the webhook
|
|
531
|
+
* @returns {Promise<Webhook>} Returns the created Webhook
|
|
532
|
+
* @example
|
|
533
|
+
* // Create a webhook for the current channel
|
|
534
|
+
* channel.createWebhook('Snek', {
|
|
535
|
+
* avatar: 'https://i.imgur.com/mI8XcpG.jpg',
|
|
536
|
+
* reason: 'Needed a cool new Webhook'
|
|
537
|
+
* })
|
|
538
|
+
* .then(console.log)
|
|
539
|
+
* .catch(console.error)
|
|
540
|
+
*/
|
|
541
|
+
createWebhook(name, options = {}) {
|
|
542
|
+
return this.guild.channels.createWebhook(this.id, name, options);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Sets the rate limit per user (slowmode) for this channel.
|
|
547
|
+
* @param {number} rateLimitPerUser The new rate limit in seconds
|
|
548
|
+
* @param {string} [reason] Reason for changing the channel's rate limit
|
|
549
|
+
* @returns {Promise<this>}
|
|
550
|
+
*/
|
|
551
|
+
setRateLimitPerUser(rateLimitPerUser, reason) {
|
|
552
|
+
return this.edit({ rateLimitPerUser }, reason);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* Sets whether this channel is flagged as NSFW.
|
|
557
|
+
* @param {boolean} [nsfw=true] Whether the channel should be considered NSFW
|
|
558
|
+
* @param {string} [reason] Reason for changing the channel's NSFW flag
|
|
559
|
+
* @returns {Promise<this>}
|
|
560
|
+
*/
|
|
561
|
+
setNSFW(nsfw = true, reason) {
|
|
562
|
+
return this.edit({ nsfw }, reason);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
static applyToClass(structure, full = false, ignore = []) {
|
|
566
|
+
const props = ['send'];
|
|
567
|
+
if (full) {
|
|
568
|
+
props.push(
|
|
569
|
+
'sendSlash',
|
|
570
|
+
'searchInteraction',
|
|
571
|
+
'searchInteractionFromGuildAndPrivateChannel',
|
|
572
|
+
'searchInteractionUserApps',
|
|
573
|
+
'lastMessage',
|
|
574
|
+
'lastPinAt',
|
|
575
|
+
'search',
|
|
576
|
+
'sendTyping',
|
|
577
|
+
'createMessageCollector',
|
|
578
|
+
'awaitMessages',
|
|
579
|
+
'fetchWebhooks',
|
|
580
|
+
'createWebhook',
|
|
581
|
+
'setRateLimitPerUser',
|
|
582
|
+
'setNSFW',
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
for (const prop of props) {
|
|
586
|
+
if (ignore.includes(prop)) continue;
|
|
587
|
+
Object.defineProperty(
|
|
588
|
+
structure.prototype,
|
|
589
|
+
prop,
|
|
590
|
+
Object.getOwnPropertyDescriptor(TextBasedChannel.prototype, prop),
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
module.exports = TextBasedChannel;
|
|
597
|
+
|
|
598
|
+
// Fixes Circular
|
|
599
|
+
const MessageManager = require('../../managers/MessageManager');
|
|
600
|
+
|
|
601
|
+
// Utils
|
|
602
|
+
function parseChoices(parent, list_choices, value) {
|
|
603
|
+
if (value !== undefined) {
|
|
604
|
+
if (Array.isArray(list_choices) && list_choices.length) {
|
|
605
|
+
const choice = list_choices.find(c => [c.name, c.value].includes(value));
|
|
606
|
+
if (choice) {
|
|
607
|
+
return choice.value;
|
|
608
|
+
} else {
|
|
609
|
+
throw new Error('INVALID_SLASH_COMMAND_CHOICES', parent, value);
|
|
610
|
+
}
|
|
611
|
+
} else {
|
|
612
|
+
return value;
|
|
613
|
+
}
|
|
614
|
+
} else {
|
|
615
|
+
return undefined;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
async function addDataFromAttachment(value, client, channelId, attachments) {
|
|
620
|
+
value = await MessagePayload.resolveFile(value);
|
|
621
|
+
if (!value?.file) {
|
|
622
|
+
throw new TypeError('The attachment data must be a BufferResolvable or Stream or FileOptions of MessageAttachment');
|
|
623
|
+
}
|
|
624
|
+
const data = await Util.getUploadURL(client, channelId, [value]);
|
|
625
|
+
await Util.uploadFile(value.file, data[0].upload_url);
|
|
626
|
+
const id = attachments.length;
|
|
627
|
+
attachments.push({
|
|
628
|
+
id,
|
|
629
|
+
filename: value.name,
|
|
630
|
+
uploaded_filename: data[0].upload_filename,
|
|
631
|
+
});
|
|
632
|
+
return {
|
|
633
|
+
id,
|
|
634
|
+
attachments,
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
async function parseOption(
|
|
639
|
+
client,
|
|
640
|
+
optionCommand,
|
|
641
|
+
value,
|
|
642
|
+
optionFormat,
|
|
643
|
+
attachments,
|
|
644
|
+
command,
|
|
645
|
+
applicationId,
|
|
646
|
+
guildId,
|
|
647
|
+
channelId,
|
|
648
|
+
subGroup,
|
|
649
|
+
subCommand,
|
|
650
|
+
) {
|
|
651
|
+
const data = {
|
|
652
|
+
type: optionCommand.type,
|
|
653
|
+
name: optionCommand.name,
|
|
654
|
+
};
|
|
655
|
+
if (value !== undefined) {
|
|
656
|
+
switch (optionCommand.type) {
|
|
657
|
+
case ApplicationCommandOptionTypes.BOOLEAN:
|
|
658
|
+
case 'BOOLEAN': {
|
|
659
|
+
data.value = Boolean(value);
|
|
660
|
+
break;
|
|
661
|
+
}
|
|
662
|
+
case ApplicationCommandOptionTypes.INTEGER:
|
|
663
|
+
case 'INTEGER': {
|
|
664
|
+
data.value = Number(value);
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
case ApplicationCommandOptionTypes.ATTACHMENT:
|
|
668
|
+
case 'ATTACHMENT': {
|
|
669
|
+
const parseData = await addDataFromAttachment(value, client, channelId, attachments);
|
|
670
|
+
data.value = parseData.id;
|
|
671
|
+
attachments = parseData.attachments;
|
|
672
|
+
break;
|
|
673
|
+
}
|
|
674
|
+
case ApplicationCommandOptionTypes.SUB_COMMAND_GROUP:
|
|
675
|
+
case 'SUB_COMMAND_GROUP': {
|
|
676
|
+
break;
|
|
677
|
+
}
|
|
678
|
+
default: {
|
|
679
|
+
value = parseChoices(optionCommand.name, optionCommand.choices, value);
|
|
680
|
+
if (optionCommand.autocomplete) {
|
|
681
|
+
const nonce = SnowflakeUtil.generate();
|
|
682
|
+
// Post
|
|
683
|
+
let postData;
|
|
684
|
+
if (subGroup) {
|
|
685
|
+
postData = [
|
|
686
|
+
{
|
|
687
|
+
type: ApplicationCommandOptionTypes.SUB_COMMAND_GROUP,
|
|
688
|
+
name: subGroup.name,
|
|
689
|
+
options: [
|
|
690
|
+
{
|
|
691
|
+
type: ApplicationCommandOptionTypes.SUB_COMMAND,
|
|
692
|
+
name: subCommand.name,
|
|
693
|
+
options: [
|
|
694
|
+
{
|
|
695
|
+
type: optionCommand.type,
|
|
696
|
+
name: optionCommand.name,
|
|
697
|
+
value,
|
|
698
|
+
focused: true,
|
|
699
|
+
},
|
|
700
|
+
],
|
|
701
|
+
},
|
|
702
|
+
],
|
|
703
|
+
},
|
|
704
|
+
];
|
|
705
|
+
} else if (subCommand) {
|
|
706
|
+
postData = [
|
|
707
|
+
{
|
|
708
|
+
type: ApplicationCommandOptionTypes.SUB_COMMAND,
|
|
709
|
+
name: subCommand.name,
|
|
710
|
+
options: [
|
|
711
|
+
{
|
|
712
|
+
type: optionCommand.type,
|
|
713
|
+
name: optionCommand.name,
|
|
714
|
+
value,
|
|
715
|
+
focused: true,
|
|
716
|
+
},
|
|
717
|
+
],
|
|
718
|
+
},
|
|
719
|
+
];
|
|
720
|
+
} else {
|
|
721
|
+
postData = [
|
|
722
|
+
{
|
|
723
|
+
type: optionCommand.type,
|
|
724
|
+
name: optionCommand.name,
|
|
725
|
+
value,
|
|
726
|
+
focused: true,
|
|
727
|
+
},
|
|
728
|
+
];
|
|
729
|
+
}
|
|
730
|
+
const body = createPostData(
|
|
731
|
+
client,
|
|
732
|
+
true,
|
|
733
|
+
applicationId,
|
|
734
|
+
nonce,
|
|
735
|
+
guildId,
|
|
736
|
+
Boolean(command.guild_id),
|
|
737
|
+
channelId,
|
|
738
|
+
command.version,
|
|
739
|
+
command.id,
|
|
740
|
+
command.name_default || command.name,
|
|
741
|
+
command.type,
|
|
742
|
+
postData,
|
|
743
|
+
[],
|
|
744
|
+
);
|
|
745
|
+
await client.api.interactions.post({
|
|
746
|
+
data: body,
|
|
747
|
+
});
|
|
748
|
+
data.value = await awaitAutocomplete(client, nonce, value);
|
|
749
|
+
} else {
|
|
750
|
+
data.value = value;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
optionFormat.push(data);
|
|
755
|
+
}
|
|
756
|
+
return {
|
|
757
|
+
optionFormat,
|
|
758
|
+
attachments,
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
function awaitAutocomplete(client, nonce, defaultValue) {
|
|
763
|
+
return new Promise(resolve => {
|
|
764
|
+
const handler = data => {
|
|
765
|
+
if (data.t !== 'APPLICATION_COMMAND_AUTOCOMPLETE_RESPONSE') return;
|
|
766
|
+
if (data.d?.nonce !== nonce) return;
|
|
767
|
+
clearTimeout(timeout);
|
|
768
|
+
client.removeListener(Events.UNHANDLED_PACKET, handler);
|
|
769
|
+
client.decrementMaxListeners();
|
|
770
|
+
if (data.d.choices.length >= 1) {
|
|
771
|
+
resolve(data.d.choices[0].value);
|
|
772
|
+
} else {
|
|
773
|
+
resolve(defaultValue);
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
const timeout = setTimeout(() => {
|
|
777
|
+
client.removeListener(Events.UNHANDLED_PACKET, handler);
|
|
778
|
+
client.decrementMaxListeners();
|
|
779
|
+
resolve(defaultValue);
|
|
780
|
+
}, 5_000).unref();
|
|
781
|
+
client.incrementMaxListeners();
|
|
782
|
+
client.on(Events.UNHANDLED_PACKET, handler);
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
function createPostData(
|
|
787
|
+
client,
|
|
788
|
+
isAutocomplete = false,
|
|
789
|
+
applicationId,
|
|
790
|
+
nonce,
|
|
791
|
+
guildId,
|
|
792
|
+
isGuildCommand,
|
|
793
|
+
channelId,
|
|
794
|
+
commandVersion,
|
|
795
|
+
commandId,
|
|
796
|
+
commandName,
|
|
797
|
+
commandType,
|
|
798
|
+
postData,
|
|
799
|
+
attachments = [],
|
|
800
|
+
) {
|
|
801
|
+
const data = {
|
|
802
|
+
type: isAutocomplete ? InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE : InteractionTypes.APPLICATION_COMMAND,
|
|
803
|
+
application_id: applicationId,
|
|
804
|
+
guild_id: guildId,
|
|
805
|
+
channel_id: channelId,
|
|
806
|
+
session_id: client.sessionId,
|
|
807
|
+
data: {
|
|
808
|
+
version: commandVersion,
|
|
809
|
+
id: commandId,
|
|
810
|
+
name: commandName,
|
|
811
|
+
type: commandType,
|
|
812
|
+
options: postData,
|
|
813
|
+
attachments: attachments,
|
|
814
|
+
},
|
|
815
|
+
nonce,
|
|
816
|
+
};
|
|
817
|
+
if (isGuildCommand) {
|
|
818
|
+
data.data.guild_id = guildId;
|
|
819
|
+
}
|
|
820
|
+
return data;
|
|
821
|
+
}
|