discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

Type Error during running library. #7748

Closed kreimben closed 2 years ago

kreimben commented 2 years ago

Which package is this bug report for?

discord.js

Issue description

  1. I added addSubcommand() on new SlashCommandBuilder().
  2. Some commands (not all) didn't get addSubcommand() because terminal said TypeError: Expected to receive a R builder, got undefined instead.
  3. Whenever I make problem codes comment, Those command is not recognized on discord!

Code sample

const {SlashCommandBuilder} = require("@discordjs/builders");

module.exports = {
    data: new SlashCommandBuilder()
        .setName('remove')
        .setDescription('Remove song that you want to do.')
        .addSubcommand((subcommand) => {
            subcommand
                .setName('number')
                .setDescription('list number')
                .addStringOption((option) => {
                    option
                        .setName('number')
                        .setDescription('You can search song numbers on /queue command.')
                        .setRequired(true)
                })
        })
    ,
    run: async ({client, interaction}) => {
        let playQueue = client.player.getQueue(interaction.guilds)
        if (!playQueue.connection) await playQueue.connect(client.member.voice.channel)

        let number = interaction.options.getString("number")
        playQueue.remove(number)
    }
}

Above are source codes.

```js
/home/kreimben/Documents/Repository/ZeroTwo_bot/node_modules/@discordjs/builders/dist/index.js:3
${e}\`\`\``}function gt(t){return`\`${t}\``}function Pt(t){return`_${t}_`}function Tt(t){return`**${t}**`}function It(t){return`__${t}__`}function Mt(t){return`~~${t}~~`}function Rt(t){return`> ${t}`}function vt(t){return`>>> ${t}`}function wt(t){return`<${t}>`}function Bt(t,e,i){return i?`[${t}](${e} "${i}")`:`[${t}](${e})`}function Nt(t){return`||${t}||`}function $t(t){return`<@${t}>`}function Et(t){return`<@!${t}>`}function _t(t){return`<#${t}>`}function Vt(t){return`<@&${t}>`}function Lt(t,e=!1){return`<${e?"a":""}:_:${t}>`}function kt(t,e){return typeof t!="number"&&(t=Math.floor((t?.getTime()??Date.now())/1e3)),typeof e=="string"?`<t:${t}:${e}>`:`<t:${t}>`}var Dt={ShortTime:"t",LongTime:"T",ShortDate:"d",LongDate:"D",ShortDateTime:"f",LongDateTime:"F",RelativeTime:"R"},je=(n=>(n.Shrug="\xAF\\_(\u30C4)\\_/\xAF",n.Tableflip="(\u256F\xB0\u25A1\xB0\uFF09\u256F\uFE35 \u253B\u2501\u253B",n.Unflip="\u252C\u2500\u252C \u30CE( \u309C-\u309C\u30CE)",n))(je||{});var Pe={};B(Pe,{buttonLabelValidator:()=>Ce,buttonStyleValidator:()=>fe,customIdValidator:()=>g,defaultValidator:()=>ye,disabledValidator:()=>E,emojiValidator:()=>$,labelValueValidator:()=>_,minMaxValidator:()=>F,optionsValidator:()=>qe,placeholderValidator:()=>xe,urlValidator:()=>Se,validateRequiredButtonParameters:()=>ge,validateRequiredSelectMenuOptionParameters:()=>Oe,validateRequiredSelectMenuParameters:()=>Ae});var q=require("discord-api-types/v9"),p=require("zod"),g=p.z.string().min(1).max(100),$=p.z.object({id:p.z.string(),name:p.z.string(),animated:p.z.boolean()}).partial().strict(),E=p.z.boolean(),Ce=p.z.string().nonempty().max(80),fe=p.z.number().int().min(q.ButtonStyle.Primary).max(q.ButtonStyle.Link),xe=p.z.string().max(100),F=p.z.number().int().min(0).max(25),qe=p.z.object({}).array().nonempty();function Ae(t,e){g.parse(e),qe.parse(t)}var _=p.z.string().min(1).max(100),ye=p.z.boolean();function Oe(t,e){_.parse(t),_.parse(e)}var Se=p.z.string().url();function ge(t,e,i,n,a){if(a&&n)throw new RangeError("URL and custom id are mutually exclusive");if(!e&&!i)throw new RangeError("Buttons must have a label and/or an emoji");if(t===q.ButtonStyle.Link){if(!a)throw new RangeError("Link buttons must have a url")}else if(a)throw new RangeError("Non-link buttons cannot have a url")}var Fe=require("discord-api-types/v9");var G=require("discord-api-types/v9");function Te(t){switch(t.type){case G.ComponentType.ActionRow:return new z(t);case G.ComponentType.Button:return new W(t);case G.ComponentType.SelectMenu:return new Z(t);default:throw new Error(`Cannot serialize component type: ${t.type}`)}}var z=class{constructor(e){o(this,"components",[]);o(this,"type",Fe.ComponentType.ActionRow);this.components=e?.components.map(Te)??[]}addComponents(...e){return this.components.push(...e),this}setComponents(e){return Reflect.set(this,"components",[...e]),this}toJSON(){return{...this,components:this.components.map(e=>e.toJSON())}}};var K=require("discord-api-types/v9");var W=class{constructor(e){o(this,"type",K.ComponentType.Button);o(this,"style");o(this,"label");o(this,"emoji");o(this,"disabled");o(this,"custom_id");o(this,"url");this.style=e?.style,this.label=e?.label,this.emoji=e?.emoji,this.disabled=e?.disabled,e?.style===K.ButtonStyle.Link?this.url=e.url:this.custom_id=e?.custom_id}setStyle(e){return fe.parse(e),Reflect.set(this,"style",e),this}setURL(e){return Se.parse(e),Reflect.set(this,"url",e),this}setCustomId(e){return g.parse(e),Reflect.set(this,"custom_id",e),this}setEmoji(e){return $.parse(e),Reflect.set(this,"emoji",e),this}setDisabled(e){return E.parse(e),Reflect.set(this,"disabled",e),this}setLabel(e){return Ce.parse(e),Reflect.set(this,"label",e),this}toJSON(){return ge(this.style,this.label,this.emoji,this.custom_id,this.url),{...this}}};var Ge=require("discord-api-types/v9");var Q=class{constructor(e){o(this,"label");o(this,"value");o(this,"description");o(this,"emoji");o(this,"default");this.label=e?.label,this.value=e?.value,this.description=e?.description,this.emoji=e?.emoji,this.default=e?.default}setLabel(e){return Reflect.set(this,"label",e),this}setValue(e){return Reflect.set(this,"value",e),this}setDescription(e){return _.parse(e),Reflect.set(this,"description",e),this}setDefault(e){return ye.parse(e),Reflect.set(this,"default",e),this}setEmoji(e){return $.parse(e),Reflect.set(this,"emoji",e),this}toJSON(){return Oe(this.label,this.value),{...this}}};var Z=class{constructor(e){o(this,"type",Ge.ComponentType.SelectMenu);o(this,"options");o(this,"placeholder");o(this,"min_values");o(this,"max_values");o(this,"custom_id");o(this,"disabled");this.options=e?.options.map(i=>new Q(i))??[],this.placeholder=e?.placeholder,this.min_values=e?.min_values,this.max_values=e?.max_values,this.custom_id=e?.custom_id,this.disabled=e?.disabled}setPlaceholder(e){return xe.parse(e),Reflect.set(this,"placeholder",e),this}setMinValues(e){return F.parse(e),Reflect.set(this,"min_values",e),this}setMaxValues(e){return F.parse(e),Reflect.set(this,"max_values",e),this}setCustomId(e){return g.parse(e),Reflect.set(this,"custom_id",e),this}setDisabled(e){return E.parse(e),Reflect.set(this,"disabled",e),this}addOptions(...e){return this.options.push(...e),this}setOptions(e){return Reflect.set(this,"options",[...e]),this}toJSON(){return Ae(this.options,this.custom_id),{...this,options:this.options.map(e=>e.toJSON())}}};var Re={};B(Re,{assertReturnOfBuilder:()=>C,validateDefaultPermission:()=>Ie,validateDescription:()=>Y,validateMaxChoicesLength:()=>Me,validateMaxOptionsLength:()=>c,validateName:()=>X,validateRequired:()=>ee,validateRequiredParameters:()=>b});var H=yt(require("@sindresorhus/is")),V=require("zod"),Ut=V.z.string().min(1).max(32).regex(/^[\P{Lu}\p{N}_-]+$/u);function X(t){Ut.parse(t)}var Jt=V.z.string().min(1).max(100);function Y(t){Jt.parse(t)}var ze=V.z.unknown().array().max(25);function c(t){ze.parse(t)}function b(t,e,i){X(t),Y(e),c(i)}var We=V.z.boolean();function Ie(t){We.parse(t)}function ee(t){We.parse(t)}function Me(t){ze.parse(t)}function C(t,e){let i=e.name;if(H.default.nullOrUndefined(t))throw new TypeError(`Expected to receive a ${i} builder, got ${t===null?"null":"undefined"} instead.`);if(H.default.primitive(t))throw new TypeError(`Expected to receive a ${i} builder, got a primitive (${typeof t}) instead.`);if(!(t instanceof e)){let n=t,a=H.default.function_(t)?t.name:n.constructor.name,y=Reflect.get(n,Symbol.toStringTag),w=y?`${a} [${y}]`:a;throw new TypeError(`Expected to receive a ${i} builder, got ${w} instead.`)}}var ut=require("ts-mixer");var Ze=require("discord-api-types/v9");var f=class{constructor(){o(this,"name");o(this,"description")}setName(e){return X(e),Reflect.set(this,"name",e),this}setDescription(e){return Y(e),Reflect.set(this,"description",e),this}};var s=class extends f{constructor(){super(...arguments);o(this,"required",!1)}setRequired(e){return ee(e),Reflect.set(this,"required",e),this}runRequiredValidations(){b(this.name,this.description,[]),ee(this.required)}};var te=class extends s{constructor(){super(...arguments);o(this,"type",Ze.ApplicationCommandOptionType.Boolean)}toJSON(){return this.runRequiredValidations(),{...this}}};var Ke=require("discord-api-types/v9"),Qe=require("ts-mixer");var d=require("discord-api-types/v9"),ve=require("zod"),jt=[d.ChannelType.GuildText,d.ChannelType.GuildVoice,d.ChannelType.GuildCategory,d.ChannelType.GuildNews,d.ChannelType.GuildStore,d.ChannelType.GuildNewsThread,d.ChannelType.GuildPublicThread,d.ChannelType.GuildPrivateThread,d.ChannelType.GuildStageVoice],qt=ve.z.union(jt.map(t=>ve.z.literal(t))),we=class{constructor(){o(this,"channel_types")}addChannelType(e){return this.channel_types===void 0&&Reflect.set(this,"channel_types",[]),qt.parse(e),this.channel_types.push(e),this}addChannelTypes(e){return e.forEach(i=>this.addChannelType(i)),this}};var P=class extends s{constructor(){super(...arguments);o(this,"type",Ke.ApplicationCommandOptionType.Channel)}toJSON(){return this.runRequiredValidations(),{...this}}};P=m([(0,Qe.mix)(we)],P);var et=require("discord-api-types/v9"),tt=require("ts-mixer"),ot=require("zod");var L=class{constructor(){o(this,"max_value");o(this,"min_value")}};var He=require("discord-api-types/v9"),T=require("zod");var oe=T.z.string().min(1).max(100),Xe=T.z.number().gt(-1/0).lt(1/0),Ye=T.z.tuple([oe,T.z.union([oe,Xe])]).array(),Ft=T.z.boolean(),x=class{constructor(){o(this,"choices");o(this,"autocomplete");o(this,"type")}addChoice(e,i){if(this.autocomplete)throw new RangeError("Autocomplete and choices are mutually exclusive to each other.");return this.choices===void 0&&Reflect.set(this,"choices",[]),Me(this.choices),oe.parse(e),this.type===He.ApplicationCommandOptionType.String?oe.parse(i):Xe.parse(i),this.choices.push({name:e,value:i}),this}addChoices(e){if(this.autocomplete)throw new RangeError("Autocomplete and choices are mutually exclusive to each other.");Ye.parse(e);for(let[i,n]of e)this.addChoice(i,n);return this}setChoices(e){if(e.length>0&&this.autocomplete)throw new RangeError("Autocomplete and choices are mutually exclusive to each other.");Ye.parse(e),Reflect.set(this,"choices",[]);for(let[i,n]of e)this.addChoice(i,n);return this}setAutocomplete(e){if(Ft.parse(e),e&&Array.isArray(this.choices)&&this.choices.length>0)throw new RangeError("Autocomplete and choices are mutually exclusive to each other.");return Reflect.set(this,"autocomplete",e),this}};var it=ot.z.number().int().nonnegative(),I=class extends s{constructor(){super(...arguments);o(this,"type",et.ApplicationCommandOptionType.Integer)}setMaxValue(e){return it.parse(e),Reflect.set(this,"max_value",e),this}setMinValue(e){return it.parse(e),Reflect.set(this,"min_value",e),this}toJSON(){if(this.runRequiredValidations(),this.autocomplete&&Array.isArray(this.choices)&&this.choices.length>0)throw new RangeError("Autocomplete and choices are mutually exclusive to each other.");return{...this}}};I=m([(0,tt.mix)(L,x)],I);var nt=require("discord-api-types/v9");var ie=class extends s{constructor(){super(...arguments);o(this,"type",nt.ApplicationCommandOptionType.Mentionable)}toJSON(){return this.runRequiredValidations(),{...this}}};var rt=require("discord-api-types/v9"),at=require("ts-mixer"),st=require("zod");var pt=st.z.number().nonnegative(),M=class extends s{constructor(){super(...arguments);o(this,"type",rt.ApplicationCommandOptionType.Number)}setMaxValue(e){return pt.parse(e),Reflect.set(this,"max_value",e),this}setMinValue(e){return pt.parse(e),Reflect.set(this,"min_value",e),this}toJSON(){if(this.runRequiredValidations(),this.autocomplete&&Array.isArray(this.choices)&&this.choices.length>0)throw new RangeError("Autocomplete and choices are mutually exclusive to each other.");return{...this}}};M=m([(0,at.mix)(L,x)],M);var mt=require("discord-api-types/v9");var ne=class extends s{constructor(){super(...arguments);o(this,"type",mt.ApplicationCommandOptionType.Role)}toJSON(){return this.runRequiredValidations(),{...this}}};var lt=require("discord-api-types/v9"),dt=require("ts-mixer");var R=class extends s{constructor(){super(...arguments);o(this,"type",lt.ApplicationCommandOptionType.String)}toJSON(){if(this.runRequiredValidations(),this.autocomplete&&Array.isArray(this.choices)&&this.choices.length>0)throw new RangeError("Autocomplete and choices are mutually exclusive to each other.");return{...this}}};R=m([(0,dt.mix)(x)],R);var ct=require("discord-api-types/v9");var re=class extends s{constructor(){super(...arguments);o(this,"type",ct.ApplicationCommandOptionType.User)}toJSON(){return this.runRequiredValidations(),{...this}}};var k=class{constructor(){o(this,"options")}addBooleanOption(e){return this._sharedAddOptionMethod(e,te)}addUserOption(e){return this._sharedAddOptionMethod(e,re)}addChannelOption(e){return this._sharedAddOptionMethod(e,P)}addRoleOption(e){return this._sharedAddOptionMethod(e,ne)}addMentionableOption(e){return this._sharedAddOptionMethod(e,ie)}addStringOption(e){return this._sharedAddOptionMethod(e,R)}addIntegerOption(e){return this._sharedAddOptionMethod(e,I)}addNumberOption(e){return this._sharedAddOptionMethod(e,M)}_sharedAddOptionMethod(e,i){let{options:n}=this;c(n);let a=typeof e=="function"?e(new i):e;return C(a,i),n.push(a),this}};var Be=require("discord-api-types/v9"),Ne=require("ts-mixer");var A=class{constructor(){o(this,"name");o(this,"description");o(this,"options",[])}addSubcommand(e){let{options:i}=this;c(i);let n=typeof e=="function"?e(new u):e;return C(n,u),i.push(n),this}toJSON(){return b(this.name,this.description,this.options),{type:Be.ApplicationCommandOptionType.SubcommandGroup,name:this.name,description:this.description,options:this.options.map(e=>e.toJSON())}}};A=m([(0,Ne.mix)(f)],A);var u=class{constructor(){o(this,"name");o(this,"description");o(this,"options",[])}toJSON(){return b(this.name,this.description,this.options),{type:Be.ApplicationCommandOptionType.Subcommand,name:this.name,description:this.description,options:this.options.map(e=>e.toJSON())}}};u=m([(0,Ne.mix)(f,k)],u);var ae=class{constructor(){o(this,"name");o(this,"description");o(this,"options",[]);o(this,"defaultPermission")}toJSON(){return b(this.name,this.description,this.options),{name:this.name,description:this.description,options:this.options.map(e=>e.toJSON()),default_permission:this.defaultPermission}}setDefaultPermission(e){return Ie(e),Reflect.set(this,"defaultPermission",e),this}addSubcommandGroup(e){let{options:i}=this;c(i);let n=typeof e=="function"?e(new A):e;return C(n,A),i.push(n),this}addSubcommand(e){let{options:i}=this;c(i);let n=typeof e=="function"?e(new u):e;return C(n,u),i.push(n),this}};ae=m([(0,ut.mix)(k,f)],ae);var Ve={};B(Ve,{validateDefaultPermission:()=>Ee,validateName:()=>se,validateRequiredParameters:()=>_e,validateType:()=>pe});var v=require("zod"),$e=require("discord-api-types/v9"),Gt=v.z.string().min(1).max(32).regex(/^( *[\p{L}\p{N}_-]+ *)+$/u),zt=v.z.union([v.z.literal($e.ApplicationCommandType.User),v.z.literal($e.ApplicationCommandType.Message)]),Wt=v.z.boolean();function Ee(t){Wt.parse(t)}function se(t){Gt.parse(t)}function pe(t){zt.parse(t)}function _e(t,e){se(t),pe(e)}var ht=class{constructor(){o(this,"name");o(this,"type");o(this,"defaultPermission")}setName(e){return se(e),Reflect.set(this,"name",e),this}setType(e){return pe(e),Reflect.set(this,"type",e),this}setDefaultPermission(e){return Ee(e),Reflect.set(this,"defaultPermission",e),this}toJSON(){return _e(this.name,this.type),{name:this.name,type:this.type,default_permission:this.defaultPermission}}};module.exports=Ot(Zt);0&&(module.exports={ActionRow,ButtonComponent,ComponentAssertions,ContextMenuCommandAssertions,ContextMenuCommandBuilder,Embed,EmbedAssertions,Faces,SelectMenuComponent,SelectMenuOption,SlashCommandAssertions,SlashCommandBooleanOption,SlashCommandBuilder,SlashCommandChannelOption,SlashCommandIntegerOption,SlashCommandMentionableOption,SlashCommandNumberOption,SlashCommandRoleOption,SlashCommandStringOption,SlashCommandSubcommandBuilder,SlashCommandSubcommandGroupBuilder,SlashCommandUserOption,TimestampStyles,blockQuote,bold,channelMention,codeBlock,createComponent,formatEmoji,hideLinkEmbed,hyperlink,inlineCode,italic,memberNicknameMention,quote,roleMention,spoiler,strikethrough,time,underscore,userMention});

Above are what terminal said.



### Package version

13.6.0

### Node.js version

Node.js v17.8.0

### Operating system

Arch Linux

### Priority this issue should have

Medium (should be fixed soon)

### Which partials do you have configured?

No Partials

### Which gateway intents are you subscribing to?

Guilds, GuildVoiceStates, GuildMessages

### I have tested this issue on a development release

_No response_
monbrey commented 2 years ago

.addSubcommand((subcommand) => {

Because you used {} for the function block, you need to explicitly return something from it. Without doing this, the function returns undefined.

monbrey commented 2 years ago

The issue tracker is only for bug reports and enhancement suggestions. If you have a question, please ask it in the Discord server instead of opening an issue – you will get redirected there anyway.

kreimben commented 2 years ago

Ok. Thanks.