删除没有包含特定单词的 Discord 信息的代码。

huangapple go评论58阅读模式
英文:

How to code a discord bot to delete messages that do not have a word in them

问题

I'd like to code a bot for a vouch discord channel.

So of course I need to have people be able to give me a vouch and explain what it would be for... but how would I code a bot that will automatically delete a message that doesn't start with the word "Vouch" or "vouch" (not case sensitive), but then doesn't delete the rest of the message because it contains something after the word?

If you can, I'd also like a way to be able to exclude a certain role from that, so like with an owner role the bot does not filter me if possible, thanks for the help!

I tried this other code written by someone else, but it deleted the messages if the message had anything else other than the word "vouch" in them.

So this is the code I've done so far. The bot is active, and the code runs, but it just doesn't monitor or delete any messages. Please let me know what to change for it to work. I've turned on message content intent in the discord developer settings, and I'm sure my bot has the message managing role on both a discord role and the link that is created to add your bot. Please help.

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Project is running!");
})
app.get("/", (req, res) => {
  res.send("Dough Bot Here!");
})

const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds, 
    GatewayIntentBits.GuildMessages, 
    GatewayIntentBits.MessageContent,
  ],
})
const mySecret = process.env['token']

// Message create event listener
client.on('messageCreate', message => {

    // Checks if the message is in the vouch channel
    if(message.channel.id === 'vouch-channel'){

        // Converts message content to lowercase to make the check case insensitive
        content = message.content.toLowerCase()

        // Checks if the message starts with vouch
        if(content.startsWith('vouch')){
            return 
            // If the author has owner role, return
        } else {
            if(message.author.roles.cache.some(role => role.name === 'Obese')) return;
            message.delete()
        }
            // Delete the message
    }

});

client.login(process.env.token)
英文:

Id like to code a bot for a vouch discord channel.

So of course I need to have people be able to give me a vouch and explain what it would be for.. but how would I code a bot that will automatically delete a message that doesn't start with the word "Vouch" or "vouch" (not case sensitive), but then doesn't delete the rest of the message because it contains something after the word?

If you can id also like a way to be able to exclude a certain role from that, so like with an owner role the bot does not filter me if possible, thanks for the help!

I tried this other code written by someone else but it deleted the messages if the message had anything else other than the word vouch in them.

So this is the code ive done so far, the bot is active and the code runs but it just doesnt monitor or delete any messages, please let me know what to change for it to work, ive turned on message content intent in the discord developer settings, and im sure my bot has the message managing role on both a discord role and the link that is created to add your bot, please help.

const express = require("express");
const app = express();

app.listen(3000, () => {
  console.log("Project is running!")
})
app.get("/", (req, res) => {
  res.send("Dough Bot Here!");
})

const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds, 
    GatewayIntentBits.GuildMessages, 
    GatewayIntentBits.MessageContent,
  ],
})
const mySecret = process.env['token']

// Message create event listener
client.on('messageCreate', message => {

    // Checks if the message is in the vouch channel
    if(message.channel.id === 'vouch-channel'){

        // Converts message content to lowercase to make the check case insensitive
        content = message.content.toLowerCase()

        // Checks if the message starts with vouch
        if(content.startsWith('vouch')){
            return 
            // If the author has owner role, return
        } else {
            if(message.author.roles.cache.some(role => role.name === 'Obese')) return;
            message.delete()
        }
            // Delete the message
    }

});

client.login(process.env.token)

答案1

得分: 1

以下是已翻译的内容:

你可以检查消息是否在正确的频道中 -> 如果消息不以 "vouch" 开头 -> 如果消息发送者没有 "Owner" 角色 -> 删除消息

以下是执行此操作的代码:

// 消息创建事件监听器
client.on('messageCreate', message => {

    // 检查消息是否在 vouch 频道中
    if (message.channel.id === '在此处放入vouch频道的ID'){
    
        // 将消息内容转换为小写以进行大小写不敏感的检查
        content = message.content.toLowerCase()
    
        // 检查消息是否不以 "vouch" 开头
        if (!content.startsWith('vouch')){
    
            // 如果作者拥有 owner 角色,返回
            if (message.author.roles.cache.some(role => role.name === 'Owner')) return;
    
            // 删除消息
            message.delete();
    
        }
    }

})
英文:

You can just check if the message is in the correct channel -> if it doesnt start with vouch -> if the message sender does not have role "Owner" -> Delete Message

Following is the code which does this:

// Message create event listener
client.on('messageCreate', message => {

    // Checks if the message is in the vouch channel
    if(message.channel.id === 'Vouch channel id goes here'){
    
        // Converts message content to lowercase to make the check case insensitive
        content = message.content.toLowerCase()
    
        // Checks if the message doesn't start with vouch
        if(!content.startsWith('vouch')){
    
            // If the author has owner role, return
            if(message.author.roles.cache.some(role => role.name === 'Owner')) return;
    
            // Delete the message
            message.delete();
    
        }
    }

})

huangapple
  • 本文由 发表于 2023年4月13日 21:21:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定