OceanicJS / Oceanic

A NodeJS library for interfacing with Discord.
https://oceanic.ws
MIT License
274 stars 25 forks source link

Process errors when trying to access CommandInteraction#guild when app is not installed in that guild #150

Closed TheFallenSpirit closed 6 months ago

TheFallenSpirit commented 6 months ago

Current Behaviour:

When attempting to access CommandInteraction#guild, if the app is not installed in that guild, you will receive the following error:

UncachedError: CommandInteraction#guild is not present if you don't have the GUILDS intent.

This error is even received when simply logging the interaction console.log(interaction).

Expected Behaviour:

CommandInteraction#guild will be null and will not error when the app is not installed and the current context is 0 (Guild).

ray-1337 commented 6 months ago

The error message was pretty obvious, you need to enable the Server Members Intent on your Discord app.

image

And, also adding "GUILDS" intent to your Client options.

import { Client } from "oceanic.js";

const bot = new Client({
  gateway: {
    intents: ["GUILDS"]
  }
});
DonovanDMC commented 6 months ago

Could you mock up a script to setup the conditions to reproduce this? I'm having trouble following what's being said here

TheFallenSpirit commented 6 months ago

Could you mock up a script to setup the conditions to reproduce this? I'm having trouble following what's being said here

I don't think a script is needed, the following code should do it:

const client = new Client({
    gateway: { intents: 38571, maxShards: 'auto' },
    auth: `Bot <TOKEN-HERE>`,
});

client.on('interactionCreate', (int) => console.log(int));
client.connect();

Make sure the interaction has the integration types 0 and 1, and make sure it has contexts 0, 1, and 2. Then run the command in a guild that does not have the bot installed.

This is the code of my command:

import { ChatCmdRun, CmdInfo } from '@/interfaces/Command';
import { OptType } from '@/types';

export const run: ChatCmdRun = async (client, interaction) => {
    return console.log(interaction);
};

export const info: CmdInfo = {
    name: 'profile',
    cooldown: 15,
    contexts: [0, 1, 2],
    integrationTypes: [0, 1],
    description: 'View a users profile.',
    options: [
        {
            name: 'user',
            required: false,
            type: OptType.User,
            description: 'The user to view the profile of.'
        }
    ]
};
DonovanDMC commented 6 months ago

I can't reproduce this with just logging the interaction If you're using some specific logging that inspects the object and shows hidden properties/inspects getters, your problem lies there

TheFallenSpirit commented 6 months ago

I investigated further, it appears my command handler was checking for the existence of the guild property on CommandInteraction. To replicate this issue use console.log(interaction.guild), as that will trigger the error, but only in guilds that do not have the bot installed.

DonovanDMC commented 6 months ago

That isn't a bug, the guild property is defined as always returning a guild, if it can't find a guild to return, it has to throw an error

I can't test it at this exact moment, but guildPartial is probably present