Meridian
Meridian
DIAdiscord.js - Imagine a bot
Created by Meridian on 10/11/2024 in #djs-questions
Type error with ApplicationCommandType and ContextMenuCommandType
I just upgraded TypeScript and some other things in my package.json and it apparently now thinks that the following code is invalid:
new ContextMenuCommandBuilder()
.setName('...')
.setType(ApplicationCommandType.Message)
// TS2345: Argument of type ApplicationCommandType. Message is not assignable to parameter of type ContextMenuCommandType
new ContextMenuCommandBuilder()
.setName('...')
.setType(ApplicationCommandType.Message)
// TS2345: Argument of type ApplicationCommandType. Message is not assignable to parameter of type ContextMenuCommandType
When checking the types ContextMenuCommandBuilder#setType() requires the type ContextMenuCommandType, which is resolved to ApplicationCommandType.Message | ApplicationCommandType.User. So why are these types not compatible to each other? Is this possibly due to a version mismatch? My versions:
"discord-api-types": "^0.37.101"
"discord.js": "^14.16.3"
"typescript": "^5.6.3"

Node: v22.9.0
"discord-api-types": "^0.37.101"
"discord.js": "^14.16.3"
"typescript": "^5.6.3"

Node: v22.9.0
Thanks in advance.
14 replies
DIAdiscord.js - Imagine a bot
Created by Meridian on 3/7/2023 in #djs-questions
Is there any disadvantage when registering global commands on bot startup?
I'm trying to create a bot that is simple to setup and run yourself, which would also require it to register its own commands itself. Are there any issues if I register the commands before logging the bot in, which would essentially overwrite the global commands every time you restart it? As far as I can tell, overwriting commands does not count towards the daily creation limit. Or should I get the currently registered commands and manually check them for differences before trying to overwrite them? I know that the common practice is to use a deploy-commands.js script, but I'd like to automate deploying.
5 replies
DIAdiscord.js - Imagine a bot
Created by Meridian on 9/21/2022 in #djs-questions
DiscordAPIError - Invalid code in request
i'm getting this error sometimes when trying to exchange a token on /oauth2/token after logging in with oauth:
DiscordAPIError[invalid_request]: Invalid "code" in request.
DiscordAPIError[invalid_request]: Invalid "code" in request.
the weird thing: its working most of the time, which is also why i cant really reproduce it. my callback looks like this:
app.get('/callback', async (req, res) => {
if (!req.query.code) { return res.redirect('/') }

const body = new URLSearchParams({ 'client_id': appId, 'client_secret': clientSecret, 'code': req.query.code, 'grant_type': 'authorization_code', 'redirect_uri': `${host}/callback` })
const token = await client.rest.post(Routes.oauth2TokenExchange(), { body: body, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, auth: false, passThroughBody: true })
...
app.get('/callback', async (req, res) => {
if (!req.query.code) { return res.redirect('/') }

const body = new URLSearchParams({ 'client_id': appId, 'client_secret': clientSecret, 'code': req.query.code, 'grant_type': 'authorization_code', 'redirect_uri': `${host}/callback` })
const token = await client.rest.post(Routes.oauth2TokenExchange(), { body: body, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, auth: false, passThroughBody: true })
...
which means the request shouldnt even be made when there is no code. /callback is also the direct redirect_uri, which means the request there should only have valid codes directly from discord any ideas here?
5 replies
DIAdiscord.js - Imagine a bot
Created by Meridian on 8/14/2022 in #djs-questions
voiceStateUpdate returns same channel IDs when disconnecting a user via right-click.
How can I detect if the bot has been disconnected by a user with client.on('voiceStateUpdate', (oldState, newState) => {})? I'm first comparing if the voiceStateUpdate even is about the bot using if (newState.guild.me.id === newState.member.id) and then trying to detect a disconnect using if (!newState.channelId) but it's not working. Any ideas? v13.8.0
13 replies