Home > Software design >  My customizable welcome channel feature for my Discord bot isn't working, it looks like a probl
My customizable welcome channel feature for my Discord bot isn't working, it looks like a probl

Time:10-02

My customizable welcome channel feature for my Discord bot isn't working. I use MongoDB so I can make it customizable per-server.

There are 3 relevant files: welcome.js (my schema file), guildMemberAdd.js (my event file) and setwelcome.js (the command I use to set the welcome channel.)

The welcome.js file:

const mongoose = require('mongoose');


const schema = mongoose.Schema({
    _id: {
    type: String,
    required: true
  },
  welcomeChannelId: {
    type: String,
    required: true
  },
  welcomeText: {
    type: String,
    required: true
  }
});

module.exports = mongoose.model('welcome', schema);

The guildMemberAdd.js file:

const { MessageEmbed } = require('discord.js');
const schema = require('../models/welcome.js')

const welcomeData = {}

module.exports = {
    name: 'guildMemberAdd',
    async execute(member) {
        const g = member.guild;
        const ms = require('ms');
        const timeSpan = ms('10 days');

        //Alt detection
        const createdAt = new Date(member.user.createdAt).getTime();
        const difference = Date.now() - createdAt;

        if (difference < timeSpan) {
            member.send('Bye, alt.');
            member.ban({ reason: 'This is an alt.' });
        }

        //Welcome Users
    
    let data = welcomeData[member.guild.id]
    if (!data) {
      const results = await schema.find({
        _id: member.guild.id
      })
      if (!results) {
        return
      }

      const { welcomeChannelId, welcomeText } = results
      const channel = member.guild.channels.cache.get(welcomeChannelId)
      data = welcomeData[member.guild.id] = [channel, welcomeText]
    }

    data[0].send({
      content: data[1].replace(/@/g, `<@${member.id}>`)
    })


    },
};

The setwelcome.js file

const { MessageEmbed } = require('discord.js');
const schema = require('../../models/welcome.js')

module.exports = {
    name: 'setwelcome',
    description: 'Sets the welcome message for the server.',
  options: [{
    name: 'channel',
    description: 'The channel to set as the welcome channel.',
    type: 'CHANNEL',
    required: true
  },
  {
    name: 'message',
    description: 'The welcome message.',
    type: 'STRING',
    required: true
  }],
    async execute(interaction) {
        const channel = await interaction.options.getChannel('channel')
    const message = await interaction.options.getString('message')

    if (
            channel.type !== 'GUILD_TEXT' &&
            channel.type !== 'GUILD_NEWS' &&
            channel.type !== 'GUILD_NEWS_THREAD' &&
            channel.type !== 'GUILD_PUBLIC_THREAD' &&
            channel.type !== 'GUILD_PRIVATE_THREAD'
        )
            return interaction.reply('That is not a valid channel type.');


    await schema.findOneAndUpdate({
      _id: interaction.guild.id,
    },
    {
      _id: interaction.guild.id,
      welcomeChannelId: channel.id,
      welcomeText: message   
    },
    {
      upsert: true
    })

    await interaction.reply(`Welcome channel is set to ${channel} and welcome message is set to \`${message}\`.`)
    },
};

When a new member joins the guild, it throws this error:

 /home/runner/MultiBot/events/guildMemberAdd.js:38
 data[0].send({
            ^

TypeError: Cannot read properties of undefined (reading 'send')
    at Object.execute (/home/runner/MultiBot/events/guildMemberAdd.js:38:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Please help me out, thanks in advance!

CodePudding user response:

This means that message is nullish. Make sure the argument is actually there. There are 3 ways:

  1. Making the slash command options required (recommended)
  2. Returning early if the argument is not found
if (!message) return; //after the 'message' declaration
  1. Setting a default value, this uses the logical OR operator (the nullish coalescing operator will also suffice)
const message = await interaction.options.getString('message') || "Welcome to the server!"

CodePudding user response:

It might be because data is an array/object and doesn't have a send method. You would need to find the channel before sending it with something like

const channel = await client.channels.fetch(welcomeChannelId);
  • Related