【问题标题】:How to delete message if it's not starting with specified word. Discord.js如果消息不是以指定的单词开头,如何删除消息。不和谐.js
【发布时间】:2021-03-04 05:18:38
【问题描述】:

我想知道如何制作删除消息的功能,如果它不是以指定的“单词”开头,而不是当消息包含指定的单词时,而是当它以它开头时(指定频道上的所有内容具有频道 ID),这对于我和我在网上找不到任何解决方案。我什么都没试过,因为我不知道怎么做。

【问题讨论】:

  • 请记住,StackOverflow 不是代码编写服务。在发布调试支持之前,请尝试自己创建代码。

标签: javascript node.js discord.js


【解决方案1】:

欢迎,您可以收听“消息”事件并检查消息是否以您需要的字符串开头。此处为您举例:

const Discord = require("discord.js");

// Making our Discord Client
const client = new Discord.Client();

// Listens for the new message event
client.on("message", (message) => {
    // String of your need
    const str = "word";

    // Checking if string starts with your string of preference
    if (message.content.startsWith(str)) {
        // Deleting the message
        message.delete();
    }
});

希望对您有所帮助,您可以阅读有关事件侦听器here 的更多信息。

【讨论】:

    【解决方案2】:

    字符串#startsWith()

    上述方法返回某个字符串是否以某个值开头的布尔值。例如:

    const str = 'Hello world!'
    
    console.log(str.startsWith('Hello')) // Output: 'true'
    console.log(str.startsWith('world')) // Output: 'false'
    
    /* Using in an if statement */
    if (str.startsWith('Hello')) {
      // This will execute the code since the value returns true
    }
    

    您现在可以使用上述方法来删除以您选择的指定单词开头的消息。

    【讨论】:

    • 是的,但我的意思是不和谐机器人。
    • 我不会为您提供有关如何通过不和谐机器人使用此方法的代码。请学习如何自己实现。
    • 你能帮我一些消息代码吗?
    • @LixerMan here 是关于如何使用 discord.js 创建不和谐机器人的指南
    猜你喜欢
    • 2020-12-15
    • 1970-01-01
    • 2020-11-25
    • 2021-10-03
    • 2021-09-20
    • 2021-05-08
    • 2018-11-21
    • 1970-01-01
    • 2021-05-29
    相关资源
    最近更新 更多