Skip to content
Advertisement

How to delete message if it’s not starting with specified word. Discord.js

I want to know how to make function that deletes message if it’s not starting with specified “word” not when message includes the word specified but when it’s starting with it (everything on specified channel with channel id), it’s very important for me and I can’t find any solution online. I’ve tried nothing because I don’t know how to make it.

Advertisement

Answer

Welcome, you can listen to the “message” event and check if the message starts with the string of your need. Example for you here:

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

I hope this helped you, you can read more about the event listeners here.

Advertisement