如果消息不是以指定的单词开头,如何删除消息。不和谐.js



我想知道如何使删除消息的功能,如果它不是以指定的"word"不是当消息包括指定的词,但当它开始与它(一切在指定的频道与频道id),这对我来说是非常重要的,我找不到任何解决方案在线。我什么都没试过,因为我不知道怎么做。

欢迎,您可以收听"留言";事件并检查消息是否以您需要的字符串开头。这里的例子:

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();
}
});

我希望这对你有所帮助,你可以在这里阅读更多关于事件侦听器的信息。

String#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
}

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

最新更新