【问题标题】:Discord.js media channelDiscord.js 媒体频道
【发布时间】:2021-06-11 07:06:31
【问题描述】:

我想向我的 discord.js 机器人添加一个仅限媒体的频道。我希望人们能够只发送链接和图像。我已将图像作为频道的一部分,但是当用户发送链接时,我的机器人会删除它们。例如prnt.sc/xxxxx

这是我的代码:

bot.on("message", msg => {
    if (msg.channel.id !== "815002698499096616") { 
      return;
    }

    // Checking if the author is a bot.
    if (msg.author.bot) {
        return false;
    }

    // Deleting the message if there are 0 attachments in the message.
    if (msg.attachments.size == 0) {
        msg.delete();
    }
});

【问题讨论】:

标签: node.js discord discord.js


【解决方案1】:

你可以像这样使用正则表达式:

var isLink = /^http/g
console.log(isLink.test("https://example.com")) // returns true

if(msg.attachments.size == 0 || !isLink.test(text content of your message)) {
    msg.delete();
}

【讨论】:

    【解决方案2】:

    感谢所有帮助者

     bot.on("message", msg => {
        if (msg.channel.id !== "804813461309620316") { 
          return;
        }
        // Checking if the author is a bot.
        if (msg.author.bot) return false;
    
        if (msg.content.includes('.png')) return false;
    
        if (msg.content.includes('.jpg')) return false;
    
        if (msg.content.includes('.jpeg')) return false;
    
        if (msg.content.includes('prnt.sc')) return false;
       
        // Deleting the message if there are 0 attachments in the message.
        if (msg.attachments.size == 0) msg.delete()
    });
    

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 2013-03-10
      • 2021-07-31
      • 2020-10-28
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 2021-05-04
      相关资源
      最近更新 更多