【问题标题】:How I can update code from discord.js v12 to v13? - Welcome messages如何将代码从 discord.js v12 更新到 v13? - 欢迎留言
【发布时间】:2022-05-10 01:02:29
【问题描述】:

我正在观看有关欢迎消息的 YouTube 教程。我复制了整个代码等。当我在 discord.js v13 上使用此代码时,它不起作用。当我在 discord.js v12 上使用此代码时,一切正常。加入新成员后的机器人发送欢迎消息。在 discord.js v13 上,机器人什么也没说,我在终端中也没有任何错误。有人可以帮我吗?我是编码的初学者,只有很少的教程知道。请帮忙。

这里是 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)

这里是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)
    })
}


这里是 config.json:

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


【问题讨论】:

  • 您是否仔细检查过您的机器人是否可以访问Server Members Intent?
  • c++ 在哪里发挥作用?

标签: javascript node.js discord.js bots


【解决方案1】:

在这一行:

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

改成这样:

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

我认为这就是它不起作用的原因,最好像这样使用client.channels.cache.find():

 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]});
        });

也可以这样:

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

【讨论】:

    【解决方案2】:

    看起来您正在使用 code lyons code,这很好!

    此外,在 discord v13 中,您需要更改发送消息和嵌入的方式。您可以执行以下操作:

    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] });
    });
    

    当用户加入你的服务器时^^

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-03
      • 2021-03-15
      • 2021-12-24
      • 2021-12-25
      • 2021-04-28
      • 2021-12-01
      • 2021-03-26
      • 1970-01-01
      相关资源
      最近更新 更多