我制作了一个 discord bot,每次有人使用命令时,它都会回复该命令,我正在努力让它工作,如果我和我的朋友发送命令,它就不会回复。
我和我的朋友在不和谐中被称为 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);
}
})
这不是全部代码,还有所有命令。
答案 0 :(得分:0)
您基本上可以使用一个数组来存储您不希望机器人回复的用户的 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