首页 > 解决方案 > 是否可以不发送两个人的消息?

问题描述

我制作了一个不和谐机器人,每次有人使用命令时,它都会回复该命令,我试图让它工作,如果我和我的朋友发送命令它不会回复。

我和我的朋友在 discord 上被称为 SugarPancake 和 vordemolt

这是代码:

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

 const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"] });

const prefix = '-';

 const fs = require('fs');

 client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => 
file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);

client.commands.set(command.name, command);
}




client.once('ready', () => {
console.log('YES is online');
client.user.setActivity(' You Shower', { type: "WATCHING" })
});



client.on('message', message => {

if (!message.content.startsWith(prefix) || message.author.bot) return;

const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (message.author.username == "SugarPancake" || message.author.username == 
 "vordemolt" || message.author.username == "malte" || message.author.username == 
  "Dr.Garbets") {
    if (command === 'comebw') {
        client.commands.get('comebw').execute(message, args);

    } else if (command == 'come') {
        client.commands.get('come').execute(message, args);
    }
    else if (command == 'mc') {
        client.commands.get('mc').execute(message, args);
    } else if (command == 'mute') {
        console.log(message.author.username);
        client.commands.get('mute').execute(message, args);
    } else if (command == 'unmute') {
        client.commands.get('unmute').execute(message, args);
    } else if (command == 'kick') {
        client.commands.get('kick').execute(message, args);


    }
    }
    client.on("message", (message) => {
    if (message.author.bot || !message.guild) return;
     if (message.content.toLowerCase() === ".test") {
    if (!message.member.roles.cache.some(r => r.name === "Admin")) 
        return message.channel.send("You don't have the required role to use this 
    command!");
      
    const roles = ["Admin"];
    if (!message.member.roles.cache.some(r => roles.includes(r.name)))
        return message.channel.send("You don't have the required role to use this 
    command");
  }
   });

    });
    client.on("message", async message => {
// white list
var usernames = ["SugarPancake", "vordemolt"];
if (usernames.includes(message.author.username) && message.content.startsWith("-")) {
    message.channel.send("We all love SugarPancake and vordemolt");
    message.channel.bulkDelete(1);

}
})

这不是所有的代码,还有所有的命令。

标签: javascriptnode.jsdiscorddiscord.js

解决方案


您基本上可以使用一个数组来存储您不希望机器人回复的用户的 ID,然后使用 .includes()。

它看起来像这样

client.on('message', message => {
 if (!message.content.startsWith(prefix) || message.author.bot) return;
 const blacklistUsers = ['yourIDgoeshere', 'yourFriendsIDgoeshere'];
 if (blacklistUsers.includes(message.author.id)) return;
 // rest of your code

确保将数组的元素替换为相应的用户 ID

如何获取用户 ID

Discord 设置 => 外观 => 开发者模式(启用) => 右键单击​​用户 => 复制 ID


推荐阅读