使用discord.js中的命令禁用机器人
有可能做这样的事情吗?根据我的理解,您应该使用,if (enabled == true)但我不确定如何使用。
回答
您可以使用全局变量(例如client.isPaused)并检查其值。查看下面的代码片段:
const client = new Client();
const prefix = '!';
// enabled by default
client.isPaused = false;
client.on('message', (message) => {
if (message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
// the only command allowed when bot is paused
if (command === 'unpause') {
if (!client.isPaused)
return message.channel.send(
`The bot is already listening to commands, can't unpause it.`,
);
client.isPaused = false;
return message.channel.send(`The bot is listening to your commands again.`);
}
// if bot is paused, exit so the commands below will not get executed
if (client.isPaused) return;
if (command === 'pause') {
client.isPaused = true;
return message.channel.send(
`The bot is paused. Use `${prefix}unpause` to unpause it.`,
);
}
if (command === 'ping') {
return message.channel.send('Pong!');
}
if (command === 'time') {
return message.channel.send(
`The time is ${new Date().toLocaleTimeString()}`,
);
}
});
client.once('ready', () => {
console.log('Bot is connected...');
});