#fetch the .addChannelOption value

1 messages · Page 1 of 1 (latest)

worldly warren
#

Hello, i was trying to create a slash command which selects a channel, on that channel i will post some data from the api every 24hrs by using cron-node.

here is my code

  Client,
  SlashCommandBuilder,
  EmbedBuilder,
  ChannelType,
  GatewayIntentBits,
} = require("discord.js");
const { fetch } = require("undici");
const cron = require("cron");

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers,
  ],
});

module.exports = {
  data: new SlashCommandBuilder()
    .setName("stadium-attr")
    .setDescription("Daily stadium attributes will be posted.")
    .addChannelOption((option) =>
      option
        .setName("channel")
        .setDescription(
          "Select the channel you want the stadium attributes to be posted."
        )
        .addChannelTypes(ChannelType.GuildText)
        .setRequired(true)
    ),
  async execute(interaction) {
    interaction.reply(`You've selected a channel`);
    console.log(interaction.options.getString("channel"));
  },
};

but im getting this error
TypeError [CommandInteractionOptionType]: Option "channel" is of type: 7; expected 3.

for now i want to console.log the selected channel.

rich arch
#

Well it's a channel option

#

So use getChannel

worldly warren
#

great. Thanks

#

just a followup questions. do i need to fetch the guildID as well to send a message in the future? @rich arch

rich arch
#

no

worldly warren
#

im having this error now @rich arch
TypeError: Cannot read properties of undefined (reading 'channels')
with this code

  data: new SlashCommandBuilder()
    .setName("stadium-attr")
    .setDescription("Daily stadium attributes will be posted.")
    .addChannelOption((option) =>
      option
        .setName("channel")
        .setDescription(
          "Select the channel you want the stadium attributes to be posted."
        )
        .addChannelTypes(ChannelType.GuildText)
        .setRequired(true)
    ),
  async execute(interaction) {
    interaction.reply(`You've selected a channel`);
    const selected_channel = interaction.options.getChannel("channel");
    const guild = client.guilds.cache.get(selected_channel.guild.guildId);
    const channel = guild.channels.cache.get(selected_channel.guild.id);

    channel.send(`hello world!`);
  },```
worldly warren
#

i was able to get the ID but when i save the channel on cache it shows empty/undefine

  data: new SlashCommandBuilder()
    .setName("stadium-attr")
    .setDescription("Daily stadium attributes will be posted.")
    .addChannelOption((option) =>
      option
        .setName("channel")
        .setDescription(
          "Select the channel you want the stadium attributes to be posted."
        )
        .addChannelTypes(ChannelType.GuildText)
        .setRequired(true)
    ),
  async execute(interaction) {
    interaction.reply(`You've selected a channel`);
    const selected_channel = await interaction.options.getChannel("channel");
    const channel_id = selected_channel.id;
    const channel = client.channels.cache.get(channel_id);
    console.log(`Selected channel id: ${channel_id}`);
    console.log(channel);
  },```