【问题标题】:Why is my Discord bot that only responds to a specific message in a specific channel not working?为什么我的 Discord 机器人只响应特定频道中的特定消息不起作用?
【发布时间】:2021-01-09 09:20:23
【问题描述】:

这是我写的代码:

    bot.on('message', (message) => {
    // your messages should go into this array
    const messages = ["honk", "mess with the honk, you get the bonk", "*inhales*   HONKKKK", "*steals your keys*", "*steals your glasses*", "*steals slippers*", "peace was never an option", "*steals knife*", "*rips anti-goose sign down*", "*waddles into blm protests and attempts to chant along*", "never gonna honk you up", "never gonna honk you down", "never gonna honk you up, never gonna honk you down", "https://tenor.com/view/goose-honk-inhale-inhales-untitled-gif-16237480", "https://i.ytimg.com/vi/zqR5pqTjqwM/maxresdefault.jpg", "https://media1.tenor.com/images/320ce00cc388ba92de7757f0458e4ad4/tenor.gif?itemid=16337290", "https://media1.tenor.com/images/6db3d9f7cb1165a0dacd6a9bb4b4af89/tenor.gif?itemid=16627067", "https://nintendowire.com/wp-content/uploads/2019/09/GooseHonk.gif", "https://media1.giphy.com/media/Jp3v0iCuOI3vpCFvf4/giphy.gif?cid=ecf05e4729rv9siv12yp6agxx7tcfhw2pncoghn3cfaqh9yy&rid=giphy.gif", "https://media4.giphy.com/media/xT39CZ1aM1WB0dZwOY/giphy.gif?cid=ecf05e4727230bb427e31469bbd225ca01dbf99c5253f1dc&rid=giphy.gif", "https://tenor.com/view/goose-gif-5452772", "https://tenor.com/view/goose-birds-moves-dance-shake-gif-5453425", "https://tenor.com/view/goose-silly-funny-waddle-funny-walk-gif-16307417", "https://marcus.is-inside.me/asqhvav1.png", "https://cdn.discordapp.com/attachments/737982177765228614/739026340346331166/image0.png", "https://cdn.discordapp.com/attachments/737169801566224434/740210286849687612/image0.jpg", "https://marcus.is-inside.me/hgaNlUNZ.png", "https://cdn.discordapp.com/attachments/740466394361233468/755986396929327236/ZNFMTWerFYngqFZatJG9MM-1200-80.png"]
    const randomMessage = messages[Math.floor(Math.random() * messages.length)];
        if (message.author.bot) return;
        let prefix = 'honk';
        if (message.channel.name === "honk") {
            if (String(message.content.toLowerCase()) == 'honk') {
                chance = Math.floor(Math.random() * 20);
                if (chance == 0) {
                    console.log(`${message.author.tag} hit the jackpot!`)
                    return message.channel.send("h0nk")
                } else {
                    console.log(`${message.author.tag} honked!`)
                    return message.channel.send(randomMessage);
                }
            } else if (String(message.content.toLowerCase()) == 'h0nk'){
                chance = Math.floor(Math.random() * 20);
                if (chance == 0) {
                    console.log(`${message.author.tag} hit the jackpot!`);
                    return message.channel.send('*chuckles* || HONKKKKKKKKKKKKKKKKKKKKKK ||');
                } else {
                    message.delete();
                }
            } else {
                message.delete();
            }

我正在尝试让以下命令在#honk 频道中工作:

    } else if (String(message.content.toLowerCase()) == ("honkhelp")) {
    console.log(`${message.author.tag} sent honkhelp`)
    const embed = new Discord.MessageEmbed()
        .setTitle("help")
        .setDescription("commands for honkers")
        .setColor("#14ff2c")
        .addField("honk", "well, just say honk!")
        .addField("honkguilds", "check how many guilds the bot is in!")
        .addField("honkpics", "a random goose picture!")
        .addField("requirements", "please make a channel called `#honk` for honkers to work! [please check our troubleshooting page](https://[link])")
        .addField("support server", "[click here](https://discord.gg/GxfQh7H)")
        .addField("other info", "do `honkabout`")
        .setFooter(message.author.tag, message.author.avatarURL({ type: "png", dynamic: true, size: 2048 }))
        .setTimestamp();
    // help embed
    return message.channel.send(embed);

它不会响应消息(并删除原始消息),而是删除消息并且不发送嵌入。如何让它保留用户消息并发送嵌入?

源码:https://github.com/inkthought-labs/honk

感谢您的帮助!

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    而不是响应消息(并删除原始 消息),它会删除消息并且不发送嵌入。我怎样才能 让它保留用户消息并发送嵌入?

    让我们看看你的代码流程

    if (message.channel.name === 'honk') {
      // code here
    } else if (String(message.content.toLowerCase()) == 'honkhelp')) {
      // send embed help message here
    }
    

    由于您尝试在名为'honk' 的频道中使用'honkhelp' 命令,因此代码将执行第一个if,并且不会进入任何else / else if

    旁注:使用=== 而不是==(你可以阅读它here

    如果您希望帮助消息优先于通常的鸣笛回答,请将帮助块移至if,并将其余部分放在以下else if
    您还可以在else if 之后移动此块if (message.channel.name === "honk")

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2022-01-20
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 2020-12-09
      • 2020-07-06
      相关资源
      最近更新 更多