我正在尝试制作一个不和谐的机器人,但是我被困在了Mutate命令上,它应该像这样:-mute @someone并让他保持静音状态,直到他被静音为止,我只能在发出命令的渠道,但这不是我想要的,请帮助。
client.on('message',message => {
if(message.content.startsWith(`${prefix}mute`) && (muted[message.author]==0 || muted[message.author]==undefined))
{
if(!message.member.hasPermission("MUTE_MEMBERS")) {
return message.channel.send("```You are not allowed to do this!```")
}
if(!message.guild.me.hasPermission("MUTE_MEMBERS")) {
return message.channel.send("**I can't do this!**")
}
let target = message.mentions.members.first();
if(target.id === message.author.id) {
return message.reply("**You can't mute yourself**")
}
if(muted[target]==undefined)
{
muted[target]=0;
}
if(muted[target]==0)
{
muted[target]=1;
let ch={};
ch=message.guild.channels;
for (const cha in ch)
{
let channe=ch[cha];
channe.updateOverwrite(target, {
SEND_MESSAGES: true
})
}
message.channel.send(`${target} ** was muted.**`);
}
else
{
message.reply("**You can't mute this person again**")
}
}
})
答案 0 :(得分:0)
我已经对您的代码进行了重大修改,并增加了一些额外的检查,包括确保邮件不是由漫游器发送的,并检查是否可以找到所提及的邮件。
我还使用Discord.Collection()
代替了普通的JavaScript对象,并改善了命令处理方式。
let muted = new Discord.Collection();
client.on('message', message => {
if (message.author.bot || !message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/\s+/g);
const command = args.shift().toLowerCase();
switch (command) {
case 'mute':
if (!message.member.hasPermission('MUTE_MEMBERS')) return message.reply('**You are not allowed to mute members.**');
if (!message.guild.me.hasPermission('MUTE_MEMBERS')) return message.reply('**I am not allowed to mute members.**');
const member = message.mentions.members.first();
if (!member) return message.reply('**I could not find the mentioned user.**');
if (member.id === message.author.id) return message.reply('**You cannot mute yourself.**');
if (muted.has(member.id)) {
message.reply('**You cannot mute this member again.**');
} else {
muted.set(member.id, true);
message.guild.channels.cache.each(channel => {
channel.updateOverwrite(member, { 'SEND_MESSAGES': false });
});
message.channel.send(`**${member.user.username} was muted.**`);
}
break;
default:
message.reply(`The command \`${command}\` was not recognized.`);
}
});
我还使用附加的unmute
命令创建了修改版本:
let muted = new Discord.Collection();
client.on('message', message => {
if (message.author.bot || !message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/\s+/g);
const command = args.shift().toLowerCase();
let member;
switch (command) {
case 'mute':
if (!message.member.hasPermission('MUTE_MEMBERS')) return message.reply('**You are not allowed to mute members.**');
if (!message.guild.me.hasPermission('MUTE_MEMBERS')) return message.reply('**I am not allowed to mute members.**');
member = message.mentions.members.first();
if (!member) return message.reply('**I could not find the mentioned user.**');
if (member.id === message.author.id) return message.reply('**You cannot mute yourself.**');
if (muted.has(member.id)) {
message.reply('**You cannot mute this member again.**');
} else {
muted.set(member.id, true);
message.guild.channels.cache.each(channel => {
channel.updateOverwrite(member, { 'SEND_MESSAGES': false });
});
message.channel.send(`**${member.user.username} was muted.**`);
}
break;
case 'unmute':
if (!message.member.hasPermission('MUTE_MEMBERS')) return message.reply('**You are not allowed to unmute members.**');
if (!message.guild.me.hasPermission('MUTE_MEMBERS')) return message.reply('**I am not allowed to unmute members.**');
member = message.mentions.members.first();
if (!member) return message.reply('**I could not find the mentioned user.**');
if (muted.has(member.id)) muted.delete(member.id);
message.guild.channels.cache.each(channel => {
channel.updateOverwrite(member, { 'SEND_MESSAGES': true });
});
message.channel.send(`**${member.user.username} was unmuted.**`);
break;
default:
message.reply(`The command \`${command}\` was not recognized.`);
}
});