【问题标题】:How to grab an attachment url from a specific channel?如何从特定频道获取附件 url?
【发布时间】:2020-06-01 15:26:25
【问题描述】:

我有以下代码,现在它正在从任何频道抓取所有附件并将其作为嵌入发送到频道“luv”中。我希望它只从“luv”频道抓取附件并将​​嵌入内容也发布到“luv”中。有什么帮助吗?

  client.on("message", async (message) => {
    const channel = message.guild.channels.find(channel => channel.name === "luv");
    if (!channel) return;
    message.attachments.forEach(attachment => { // do something with the attachment // changes all attachements into embed #luv
      if (message.deletable) {
        message.delete(1000)
        const url = attachment.url;
        const aesEmbed = new RichEmbed()
        .setImage(attachment.url)

        client.channels.get("678987398838747166").send(aesEmbed);
        console.log(attachment.url);
        }
      })
    });

【问题讨论】:

    标签: javascript discord discord.js attachment channel


    【解决方案1】:

    您可以检查消息通道 ID 或名称。

    client.on("message", async (message) => {
        if (message.channel.id !== 'PLACE CHANNEL ID HERE') return //Stop function if channel ID !== 'luv' channeld.id 
        const channel = message.guild.channels.find(channel => channel.name === "luv");
        if (!channel) return;
        message.attachments.forEach(attachment => { // do something with the attachment // changes all attachements into embed #luv
            if (message.deletable) {
                message.delete(1000)
                const url = attachment.url;
                const aesEmbed = new RichEmbed()
                    .setImage(attachment.url)
                client.channels.get("678987398838747166")
                    .send(aesEmbed);
                console.log(attachment.url);
            }
        })
    });
    

    而且最好不要使用find by name 方法来获取频道。

    替换

    const channel = message.guild.channels.find(channel => channel.name === "luv");
    

    const channel = message.guild.channels.get('CHANNEL ID')
    

    【讨论】:

      猜你喜欢
      • 2020-06-03
      • 2020-06-29
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      相关资源
      最近更新 更多