如何在 Discord.js 中添加错误消息? [英] How can i add Error Message in Discord.js?

查看:16
本文介绍了如何在 Discord.js 中添加错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了一个真心话大冒险的机器人.我的前缀是 + 现在我想向它添加一条错误消息.我有两个变量t".d"如果有人键入 +something which does not match my variable to send a Message that Invalid Command +help for Help"你们能帮帮我吗?

I Create a Truth and dare Bot. My Prefix is + Now I want to add an error message to it. I have two variables "t" "d" If anyone types +something which does not match my variable to send a Message that "Invalid Command +help for Help" Can you guys help me?

const Discord = require('discord.js');
const client = new Discord.Client();
const keepAlive = require("./server")
const prefix = "+";

// ======== Ready Log ========
client.on ("ready", () => {
    
    console.log('The Bot Is Ready!');
    client.user.setPresence({
      status: 'ONLINE', // Can Be ONLINE, DND, IDLE, INVISBLE
      activity: {
          name: 'Truth Or Dare | +help',
          type: 'PLAYING', // Can Be WHATCHING, LISTENING
      }
  })
  }); 
// ======== Code ========

client.on('message', message => {
const help = new Discord.MessageEmbed()
    .setColor('#72dfa3')
    .setTitle(`Truth Or Dare`)
    .addFields(
        { name: '``+help``', value: 'For help'},
    { name: '``+t``', value: 'For Truth'},
    { name: '``+d``', value: 'For Your Dare'},
    { name: '``Created By``', value: 'AlpHa Coder [Labib Khan]'},
    )
    .setTimestamp()
  .setFooter(`${message.author.username}`, message.author.displayAvatarURL());
  if (message.content === `${prefix}help`) {
    message.channel.send(help);
  }
});

// ========= Truth =========
client.on('message', message => {
const t = [
"If you could be invisible, what is the first thing you would do?", 
"What's the strangest dream you've ever had?",
"What are the top three things you look for in a boyfriend/girlfriend?",
"What is your worst habit?",
"How many stuffed animals do you own?",
"What is your biggest insecurity?"
]
const truth = t[Math.floor(Math.random() * t.length)];
if (message.content === `${prefix}t`) {
  message.channel.send(truth);
}
});

// ========= Dare =========
client.on('message', message => {
  const d = [
"Do a free-style rap for the next minute.",
"Let another person post a status on your behalf.",
"Hand over your phone to another player who can send a single text saying anything they want to anyone they want.",
"Let the other players go through your phone for one minute.",
"Smell another player's armpit",
"Smell another player's barefoot.",
"Tell everyone your honest opinion of the person who sent this command."
  ]
  const dare = d[Math.floor(Math.random() * d.length)];
  if (message.content === `${prefix}d`) {
    message.channel.send(dare);
  }
});


const token = process.env.TOKEN;
keepAlive()
client.login(token);

请解释清楚,以便我理解.提前谢谢

Please explain clearly so that I can understand. Advance Thank you

推荐答案

不要使用单独的 message 事件处理程序,使用一个.您可以通过使用 来利用它if else 链.您正在尝试通过链匹配命令,如果未找到匹配项,则在 else 中(意味着链中的每个先前检查都失败)您回复用户说:

Don't use separate message event handlers, use one. You can take advantage of that by using if else chain. You are trying to match the command through the chain, if no match was found, in else (meaning every previous check in the chain failed) you reply to the user by saying:

命令无效,请键入 +help 寻求帮助.".

"Invalid command, type +help for help.".

还要检查开头的前缀.如果没有前缀,则从函数返回.这样你就不必在匹配消息内容时将其写入 if 语句.

Also check for the prefix at the beginning. If there is no prefix, return from the function. That way you don't have to write it to the if statements when matching the message content.

// Array of possible truth replies
const t = [
    "If you could be invisible, what is the first thing you would do?", 
    "What's the strangest dream you've ever had?",
    "What are the top three things you look for in a boyfriend/girlfriend?",
    "What is your worst habit?",
    "How many stuffed animals do you own?",
    "What is your biggest insecurity?"
];

// Array of possible dare replies
const d = [
    "Do a free-style rap for the next minute.",
    "Let another person post a status on your behalf.",
    "Hand over your phone to another player who can send a single text saying anything they want to anyone they want.",
    "Let the other players go through your phone for one minute.",
    "Smell another player's armpit",
    "Smell another player's barefoot.",
    "Tell everyone your honest opinion of the person who sent this command."
];

// Handle all commands here
client.on('message', message => {

    // Don't reply to itself
    if (message.author.id === client.user.id) return;

    // If there is no + (prefix) at the beginning of the message, exit function
    if (!message.content.startsWith(prefix)) return;

    // Remove the prefix from the message -> our command
    const command = message.content.substring(prefix.length);

    // Match the command
    if (command === "t") { // Truth
        const truth = t[Math.floor(Math.random() * t.length)];
        message.channel.send(truth);
    } else if (command === "d") { // Dare
        const dare = d[Math.floor(Math.random() * d.length)];
        message.channel.send(dare);
    } else if (command === "help") { // Help

        const help = new Discord.MessageEmbed()
            .setColor('#72dfa3')
            .setTitle(`Truth Or Dare`)
            .addFields(
                { name: '``+help``', value: 'For help' },
                { name: '``+t``', value: 'For Truth' },
                { name: '``+d``', value: 'For Your Dare' },
                { name: '``Created By``', value: 'AlpHa Coder [Labib Khan]' },
            )
            .setTimestamp()
            .setFooter(`${message.author.username}`, message.author.displayAvatarURL());

        message.channel.send(help);

    } else { // No match found, invalid command
        message.channel.send("Invalid command, type `+help` for help.");
    }

});

这篇关于如何在 Discord.js 中添加错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆