【问题标题】:Bot sends embed message but doesn't send mp4 attachmentBot 发送嵌入消息但不发送 mp4 附件
【发布时间】:2021-06-24 11:48:51
【问题描述】:

刚接触编码,最近开始使用 JS 制作 discord 机器人。这是一个机器人,其中特定的 mp4 与特定的 sn-p 一起播放。

当我输入命令时,mp4 不发送,只是嵌入消息,我遇到了麻烦。基本上,如果我这样做-snip kratos,机器人会发送嵌入消息而不是 mp4。

这是我目前所拥有的:

const fs = require('fs');
const { Client, MessageAttachment } = require('discord.js');
const config = require('./config.json');
const { prefix, token } = require('./config.json');
const client = new Client();
client.commands = new Discord.Collection();```


And here are the command events:

```  client.on('message', message => {
    if (message.content === `${prefix}snip kratos`) {
    if (message.author.bot) return
    const kratos = new Discord.MessageEmbed()
      .setColor('#ffb638')
      .setTitle('Kratos')
      .setDescription('Sending 1 snippet(s)...')
      .setTimestamp()
      .setFooter('SkiBot');
      message.channel.send(kratos);
      client.on('message', message => {
        if (message.content === `${prefix}snip kratos`) {
          if (message.author.bot) return
          const attachment = new MessageAttachment('./snippets/kratos/Kratos.mp4');
          message.channel.send(attachment);

        }
      });
    }
    });

    client.on('message', message => {
      if (message.content === `${prefix}snip johnny bravo`) {
      if (message.author.bot) return
      const kratos = new Discord.MessageEmbed()
        .setColor('#ffb638')
        .setTitle('Johnny Bravo')
        .setDescription('Sending 1 snippet(s)...')
        .setTimestamp()
        .setFooter('SkiBot');
        message.channel.send(kratos);
        client.on('message', message => {
          if (message.content === `${prefix}snip johnny bravo`) {
            if (message.author.bot) return
            const attachment = new MessageAttachment('./snippets/Johnny_Bravo/Johnny_Bravo.mp4');
            message.channel.send(attachment);

          }
        });
      }
      });```

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    问题是您正在嵌套事件侦听器。删除嵌套的client.on('message', ...)部分,发送嵌入后发送消息附件。

    client.on('message', (message) => {
      if (message.author.bot) {
        return
      }
    
      if (message.content === `${prefix}snip kratos`) {
        const kratos = new MessageEmbed()
          .setColor('#ffb638')
          .setTitle('Kratos')
          .setDescription('Sending 1 snippet...')
          .setTimestamp()
          .setFooter('SkiBot')
        message.channel.send(kratos)
        const attachment = new MessageAttachment('./snippets/kratos/Kratos.mp4')
        message.channel.send(attachment)
      }
    })
    

    而且您不需要多个message 事件侦听器。您可以通过使用有效命令创建对象来简化代码。

    client.on('message', (message) => {
      const { content, author, channel } = message
      if (author.bot) {
        return
      }
    
      const embeds = {
        [`${prefix}snip kratos`]: {
          title: 'Kratos',
          attachmentPath: './snippets/kratos/Kratos.mp4',
        },
        [`${prefix}snip johnny bravo`]: {
          title: 'Johnny Bravo',
          attachmentPath: './snippets/Johnny_Bravo/Johnny_Bravo.mp4',
        },
      }
    
      const embed = embeds[content]
      if (embed) {
        const { title, attachmentPath } = embed
    
        channel.send(
          new MessageEmbed()
            .setColor('#ffb638')
            .setTitle(title)
            .setDescription('Sending 1 snippet...')
            .setTimestamp()
            .setFooter('SkiBot')
        )
        channel.send(new MessageAttachment(attachmentPath))
      }
    })
    

    如果您没有很多命令,上述解决方案应该足够了。查看this 指南,了解如何创建单独的命令文件以保持代码井井有条。

    【讨论】:

    • 嗨,所以我想要这个,但有多个 mp4 而不是一个。我尝试按照您向我展示的方式进行操作,但没有成功。
    • @mega 你能在GitHub Gist 或其他地方分享你的代码吗?
    • @mega 问题是你覆盖了attachmentPath 的值。它应该是一个数组。一个例子:{ title: 'primadonna prod. umru', attachmentPaths: ['maddo.mp4', 'nna.mp4', 'pree.mp4'] }。然后您可以使用const { title, attachmentPaths } = embed; channel.send({ files: attachmentPaths }) 发送视频。顺便说一句,您不应该嵌套消息事件侦听器。实际上,您不需要我在上面的回答中提到的多个消息事件侦听器。
    【解决方案2】:

    你应该可以的

    message.channel.send({
      files: [
        "./snippets/kratos/Kratos.mp4"
    ]})
    

    这里提到https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=send

    也在这里client.commands = new Discord.Collection(); 在这里您正在调用 Discord 但未定义 Discord

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 2023-03-07
      • 2017-10-31
      • 1970-01-01
      • 2021-08-22
      • 2019-03-03
      • 1970-01-01
      • 2021-12-25
      • 2021-03-24
      相关资源
      最近更新 更多