Home > other >  How I can update code from discord.js v12 to v13? - Welcome messages
How I can update code from discord.js v12 to v13? - Welcome messages

Time:02-03

I was watching YouTube tutorial about welcome messages. I copied whole code etc. When I'm using this code on discord.js v13 it doesn't work. When I use this code on discord.js v12 everything works. Bot after joining new member send welcome message. On discord.js v13 bot didn't say nothing and I don't have any errors in terminal. Can someone help me with that? I'm beginner in codding and have only few tutorials knwededge. Please help.

Here's index.js:

const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES" , "GUILD_MESSAGE_REACTIONS" , "DIRECT_MESSAGE_REACTIONS" ] , partials: ["MESSAGE" , "CHANNEL" , "REACTION"]  });

client.commands = new Discord.Collection();
const fs = require('fs');
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))
for(const file of commandFiles){
    const command = require(`./commands/${file}`)
    client.commands.set(command.name, command) 
}

const { prefix, token } = require('./config.json')
const welcome = require('./commands/welcome'); // Add This


client.once('ready', () => {
    console.log('Ready.')
    welcome(client) // Add This
})

client.on('message', message => {
    if(!message.content.startsWith(prefix)||message.author.bot) return

    const args = message.content.slice(prefix.length).split(/  /)
    const command = args.shift().toLowerCase()

    if(command === 'ping'){
        client.commands.get('ping').execute(message, args)
    } else if(command === 'yt'){
        client.commands.get('yt').execute(message, args)
    } else if(command === 'purge'){
        client.commands.get('purge').execute(message, args)
    }
})

client.login(token)

Here's welcome.js:

module.exports = (client) => {

  // Welcome Message Command
    const welcomechannelId = '' //Channel You Want to Send The Welcome Message
    const targetChannelId = `` //Channel For Rules

    client.on('guildMemberAdd', (member) => {
        console.log(member)
        
        const welocmemessage = ` <@${member.id}> Welcome To Our Server,
Please Read ${member.guild.channels.cache.get(targetChannelId).toString()}
Have A Nice Time!`
        const channel = member.guild.channels.cache.get(welcomechannelId)
        channel.send(welocmemessage)
    })
    
    // Leave Message Command

    const leavechannelId = '' //Channel You Want to Send The Leave Message

    client.on('guildMemberRemove', (member) => {
        const leavemessage = `<@${member.id}> Just Left Server.`

        const channel1 = member.guild.channels.cache.get(leavechannelId)
        channel1.send(leavemessage)
    })
}


Here's config.json:

  
{
    "token": "Your-Token",
    "prefix": " "
}


CodePudding user response:

On this line:

const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES" , "GUILD_MESSAGE_REACTIONS" , "DIRECT_MESSAGE_REACTIONS" ] , partials: ["MESSAGE" , "CHANNEL" , "REACTION"]  });

Change it into this:

const Client = require('discord.js');
const client = new Client({ intents: ["GUILDS", "GUILD_MESSAGES" , "GUILD_MESSAGE_REACTIONS" , "DIRECT_MESSAGE_REACTIONS" ] , partials: ["MESSAGE" , "CHANNEL" , "REACTION"]  });

I think that's why its not working and better to use client.channels.cache.find() just like this:

 client.on('guildMemberAdd', member => {
          let channel = client.channels.cache.find(channel => channel.name === "welcome");
          const join = new MessageEmbed()
          .setTitle("Welcome!")
          .setColor('RANDOM')
          .setDescription("some descriptions")
          .setTimestamp()
        
          channel.send({embeds: [join]});
        });

Can be like this too:

client.on('guildMemberAdd', member => {
      let channel = client.channels.cache.find(channel => channel.name === "welcome");
      message.channel.send({content: ""}) //message.channel.send("content")
    });

CodePudding user response:

It looks like you're using code lyons code, which is fine!

Also in discord v13 you would need to change the way you send messages and embeds.. here's what you can do:

client.on("guildMemberAdd", async (member, guild) => {
let channel = client.channels.cache.find(wch => wch.name === "welcome");
let rules = client.channels.cache.find(rch => rch.name === "rules");

const user = member.user;

const welcomeEmbed = new MessageEmbed()
      .setAuthor(`${user.tag}`, user.displayAvatarURL())
      .setThumbnail(user.displayAvatarURL())
      .setDescription(`Welcome to the server, ${user.tag}`)
      .setColor("GREEN");
    channel.send({ embeds: [welcomeEmbed] });
});

For when a user join your server ^^

  •  Tags:  
  • Related