Discord.js v13 为什么我的 tempmute 命令不起作用?
Discord.js v13 why is my tempmute command not working?
我已经为我的 discord 机器人创建了一个 tempmute 命令,它几乎可以工作了。它有很多万无一失的措施,比如防止机器人自己静音,如果没有指定时间就不工作等等。我正在使用 npm ms 包来处理静音持续时间(https://www.npmjs.com/package/ms). when instead of specifying an amount of time I type in gibberish it works as intended and replies with the correct message. The problem is that when I type in a 100% correct command it responds asthough I didn't specify the time correctly instead of muting the user for that amount of time. here's how it looks。关于为什么会这样的任何想法?
我的代码在这里:
const ms = require('ms');
const { Permissions, MessageActionRow, UserFlags } = require('discord.js');
module.exports = {
name: 'tempmute',
description: "Temporarily mutes a user",
execute(message, args)
{
const target = message.mentions.members.first();
let muteRole = message.guild.roles.cache.find(role => role.name === "muted");
if(message.member.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS))
{
if(target)
{
let memberTarget = message.guild.members.cache.get(target.id);
if(target.id == 'myBotsID')
{
message.reply("I can't mute myself.")
}
else if(message.member == target)
{
message.reply("You can't mute yourself!")
}
else if(memberTarget.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS))
{
message.channel.send(`<@${memberTarget.user.id}> has been muted for ${ms(ms(args[1]))}`);
}
else
{
if(!args[1])
{
return message.reply("Time not specified.")
}
else
{
let time = ms(args[1])
memberTarget.roles.add(muteRole.id);
try {
message.reply("<@" + memberTarget.user.id + ">" + "has been muted for " + ms(ms(args[1])).catch(console.error))
setTimeout(function () {
memberTarget.roles.remove(muteRole.id);
}, time);
} catch (err) {
message.reply("Can't transform that into milliseconds `"+args[1]+"`")
return
}
}
}
}
else
{
message.reply("You have to mention a valid member of this server.")
}
}
else
{
message.reply("You can't use that.")
}
}
}
好的,我明白了。这是有问题的代码:
try {
message.reply("<@" + memberTarget.user.id + ">" + "has been muted for " + ms(ms(args[1])).catch(console.error))
setTimeout(function () {
memberTarget.roles.remove(muteRole.id);
}, time);
} catch (err) {
message.reply("Can't transform that into milliseconds `"+args[1]+"`")
return
}
IDK 为什么但是“.catch(console.error))”(这是一个剩余代码,一开始就不应该存在)导致它的行为不同,也就是将所有内容都视为错误指定时间并返回相应的消息,而不是将成员静音指定的时间量。删除那一小段代码后,一切都按预期工作。
我已经为我的 discord 机器人创建了一个 tempmute 命令,它几乎可以工作了。它有很多万无一失的措施,比如防止机器人自己静音,如果没有指定时间就不工作等等。我正在使用 npm ms 包来处理静音持续时间(https://www.npmjs.com/package/ms). when instead of specifying an amount of time I type in gibberish it works as intended and replies with the correct message. The problem is that when I type in a 100% correct command it responds asthough I didn't specify the time correctly instead of muting the user for that amount of time. here's how it looks。关于为什么会这样的任何想法? 我的代码在这里:
const ms = require('ms');
const { Permissions, MessageActionRow, UserFlags } = require('discord.js');
module.exports = {
name: 'tempmute',
description: "Temporarily mutes a user",
execute(message, args)
{
const target = message.mentions.members.first();
let muteRole = message.guild.roles.cache.find(role => role.name === "muted");
if(message.member.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS))
{
if(target)
{
let memberTarget = message.guild.members.cache.get(target.id);
if(target.id == 'myBotsID')
{
message.reply("I can't mute myself.")
}
else if(message.member == target)
{
message.reply("You can't mute yourself!")
}
else if(memberTarget.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS))
{
message.channel.send(`<@${memberTarget.user.id}> has been muted for ${ms(ms(args[1]))}`);
}
else
{
if(!args[1])
{
return message.reply("Time not specified.")
}
else
{
let time = ms(args[1])
memberTarget.roles.add(muteRole.id);
try {
message.reply("<@" + memberTarget.user.id + ">" + "has been muted for " + ms(ms(args[1])).catch(console.error))
setTimeout(function () {
memberTarget.roles.remove(muteRole.id);
}, time);
} catch (err) {
message.reply("Can't transform that into milliseconds `"+args[1]+"`")
return
}
}
}
}
else
{
message.reply("You have to mention a valid member of this server.")
}
}
else
{
message.reply("You can't use that.")
}
}
}
好的,我明白了。这是有问题的代码:
try {
message.reply("<@" + memberTarget.user.id + ">" + "has been muted for " + ms(ms(args[1])).catch(console.error))
setTimeout(function () {
memberTarget.roles.remove(muteRole.id);
}, time);
} catch (err) {
message.reply("Can't transform that into milliseconds `"+args[1]+"`")
return
}
IDK 为什么但是“.catch(console.error))”(这是一个剩余代码,一开始就不应该存在)导致它的行为不同,也就是将所有内容都视为错误指定时间并返回相应的消息,而不是将成员静音指定的时间量。删除那一小段代码后,一切都按预期工作。