【问题标题】:Discord.js v12 send the error message from dmDiscord.js v12 从 dm 发送错误消息
【发布时间】:2021-02-14 16:54:52
【问题描述】:

我想将错误消息从 dm 发送给用户。但我收到ReferenceError: error is not defined 错误。我该如何解决这个问题?

if (error) {
   client.users.cache.get(message.author.id).send(error)
}

这是我的 index.js

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`\x1b[33m${client.user.tag} \x1b[0mgiriş yaptı!`);
  client.user.setActivity(`Online on ${client.guilds.cache.size} servers`, {type: 'WATCHING'})
});

client.on('message', message => {
  const { channel } = message

  if (channel.type === 'news') {
    message.crosspost().catch(console.error)
    console.log(`\x1b[31m${message.guild.name}\x1b[0m sunucusunda duyuru yapıldı! \x1b[33m(ID: ${message.guild.id})`)
  }

  if (error) {
    client.users.cache.get(message.author.id).send(error)
  }
});

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    您的代码似乎无法识别“错误”。我不知道你的整个代码是什么,但我更喜欢使用 catch 代替。

    .catch(error => {
         return <user>.send(error);
    })
    

    【讨论】:

    • 我试过但没用。我从 dm 发送了一条消息,但我想向控制台上的错误发送一条私人消息。
    【解决方案2】:

    问题解决了。

    const Discord = require('discord.js');
    const client = new Discord.Client();
    
    client.on('ready', () => {
      console.log(`\x1b[33m${client.user.tag} \x1b[0mgiriş yaptı!`);
      client.user.setActivity(`Online on ${client.guilds.cache.size} servers`, {type: 'WATCHING'})
    });
    
    client.on('message', message => {
      const { channel } = message
    
      if (channel.type === 'news') {
        message.crosspost().catch(error => {
          console.log(error)
          message.author.send(error.message)
        })
        console.log(`\x1b[31m${message.guild.name}\x1b[0m sunucusunda duyuru yapıldı! \x1b[33m(ID: ${message.guild.id})`)
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 2020-10-25
      • 2020-12-10
      • 2020-10-18
      • 2021-11-09
      • 2021-02-08
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多