禁用自动错误 - discord.js-commando
disable automatic error - discord.js-commando
我正在尝试编写一个不和谐的机器人。
现在有一个命令选择了另一个用户。到目前为止,除一个命令外,所有命令都有效,检查用户是否存在。
我就是这样做的:
if (!userToMarry) {
return message.channel.send('Please try again with a valid user.')}
如果没有这样的用户,则应显示相应的消息。
但我收到的是这条消息:
You provided an invalid userToMarry. Please try again.
Respond with cancel to cancel the command. The command will automatically be cancelled in 30 seconds.
如何解决这个问题?
错误的第二部分更让我困扰,因为它出现了第一部分,据我所知,这是来自 discord.js-commando
的内置命令。
能以某种方式关闭它吗?
Please try again. Respond with cancel to cancel the command. The command will automatically be cancelled in 30 seconds.
客户端
const client = new CommandoClient({
unknownCommandResponse: false,
disableEveryone: true
});
client.registry
.registerDefaultTypes()
.registerGroups([
['test', 're']
])
.registerDefaultGroups()
.registerDefaultCommands({
help: false,
prefix: false,
ping: true,
eval: true,
unknownCommand: false,
commandState: false
})
.registerCommandsIn(path.join(__dirname, 'commands'));
命令
const { Command } = require('discord.js-commando');
const db = require("quick.db");
module.exports = class MarryCommand extends Command {
constructor(client) {
super(client, {
name: 'marry',
memberName: 'marry',
group: 'test',
description: 'Marry the mentioned user',
guildOnly: true,
args: [
{
key: 'userToMarry',
prompt: 'Please select the member you wish to marry.',
type: 'member'
}
]
});
}
run(message, { userToMarry }) {
const exists = db.get(`${message.author.id}.user`);
const married = db.get(`${userToMarry.id}.user`);
if (!userToMarry) {
return message.channel.send('Please try again with a valid user.')}
if (exists == message.author.id) {
return message.channel.send('You are already married!')}
if (married == userToMarry.id) {
return message.channel.send('This user is already married!')}
if (userToMarry.id == message.author.id) {
return message.channel.send('You cannot marry yourself!');
}
if (exists != message.author.id && married != userToMarry.id) {
message.channel.send(`**Important announcement!**
${message.author} makes a marriage proposal ${userToMarry}
看起来你是 运行 在检查 userToMarry
是否存在之前的数据库查询。您可能想要更改它,如下所示。此外,如果 userToMarry
存在但无效,您可能希望将查询包装在 try/catch 中。
run(message, { userToMarry }) {
const exists = db.get(`${message.author.id}.user`);
if (!userToMarry) {
return message.channel.send('Please try again with a valid user.')
}
const married = db.get(`${userToMarry.id}.user`);
}
看来您可以通过将 error
属性 添加到参数来将错误消息更改为自定义错误消息。您还可以利用 validate
属性 来验证 body.
之外的输入
查看文档 here
这就是我要做的:
// ...
module.exports = class MarryCommand extends Command {
constructor(client) {
super(client, {
name: 'marry',
memberName: 'marry',
group: 'test',
description: 'Marry the mentioned user',
guildOnly: true,
args: [
{
key: 'userToMarry',
prompt: 'Please select the member you wish to marry.',
type: 'member',
error: 'Please try again with a valid user',
validate: (_v,msg) => !!db.get(`${msg.author.id}.user`) // Double bang converts into boolean, this checks whether user exists
}
]
});
}
run(message, { userToMarry }) {
// Exists no longer needed as it has been moved up
const married = db.get(`${userToMarry.id}.user`);
// ....
}
我正在尝试编写一个不和谐的机器人。
现在有一个命令选择了另一个用户。到目前为止,除一个命令外,所有命令都有效,检查用户是否存在。
我就是这样做的:
if (!userToMarry) {
return message.channel.send('Please try again with a valid user.')}
如果没有这样的用户,则应显示相应的消息。
但我收到的是这条消息:
You provided an invalid userToMarry. Please try again. Respond with cancel to cancel the command. The command will automatically be cancelled in 30 seconds.
如何解决这个问题?
错误的第二部分更让我困扰,因为它出现了第一部分,据我所知,这是来自 discord.js-commando
的内置命令。
能以某种方式关闭它吗?
Please try again. Respond with cancel to cancel the command. The command will automatically be cancelled in 30 seconds.
客户端
const client = new CommandoClient({
unknownCommandResponse: false,
disableEveryone: true
});
client.registry
.registerDefaultTypes()
.registerGroups([
['test', 're']
])
.registerDefaultGroups()
.registerDefaultCommands({
help: false,
prefix: false,
ping: true,
eval: true,
unknownCommand: false,
commandState: false
})
.registerCommandsIn(path.join(__dirname, 'commands'));
命令
const { Command } = require('discord.js-commando');
const db = require("quick.db");
module.exports = class MarryCommand extends Command {
constructor(client) {
super(client, {
name: 'marry',
memberName: 'marry',
group: 'test',
description: 'Marry the mentioned user',
guildOnly: true,
args: [
{
key: 'userToMarry',
prompt: 'Please select the member you wish to marry.',
type: 'member'
}
]
});
}
run(message, { userToMarry }) {
const exists = db.get(`${message.author.id}.user`);
const married = db.get(`${userToMarry.id}.user`);
if (!userToMarry) {
return message.channel.send('Please try again with a valid user.')}
if (exists == message.author.id) {
return message.channel.send('You are already married!')}
if (married == userToMarry.id) {
return message.channel.send('This user is already married!')}
if (userToMarry.id == message.author.id) {
return message.channel.send('You cannot marry yourself!');
}
if (exists != message.author.id && married != userToMarry.id) {
message.channel.send(`**Important announcement!**
${message.author} makes a marriage proposal ${userToMarry}
看起来你是 运行 在检查 userToMarry
是否存在之前的数据库查询。您可能想要更改它,如下所示。此外,如果 userToMarry
存在但无效,您可能希望将查询包装在 try/catch 中。
run(message, { userToMarry }) {
const exists = db.get(`${message.author.id}.user`);
if (!userToMarry) {
return message.channel.send('Please try again with a valid user.')
}
const married = db.get(`${userToMarry.id}.user`);
}
看来您可以通过将 error
属性 添加到参数来将错误消息更改为自定义错误消息。您还可以利用 validate
属性 来验证 body.
查看文档 here
这就是我要做的:
// ...
module.exports = class MarryCommand extends Command {
constructor(client) {
super(client, {
name: 'marry',
memberName: 'marry',
group: 'test',
description: 'Marry the mentioned user',
guildOnly: true,
args: [
{
key: 'userToMarry',
prompt: 'Please select the member you wish to marry.',
type: 'member',
error: 'Please try again with a valid user',
validate: (_v,msg) => !!db.get(`${msg.author.id}.user`) // Double bang converts into boolean, this checks whether user exists
}
]
});
}
run(message, { userToMarry }) {
// Exists no longer needed as it has been moved up
const married = db.get(`${userToMarry.id}.user`);
// ....
}