首页 > 解决方案 > 延迟删除事件

问题描述

我的 discord.js 机器人上有一个票务系统,当你关闭它时,它会立即关闭。我很好奇是否有办法将其延迟删除 1 小时。这是我的代码:

const Discord = require('discord.js');

module.exports.run = async (bot, message, args) => {
    if (!message.channel.name.startsWith('ticket')) return message.channel.send('You are not in a ticket channel!');
    let reason = args[0] | 'Ticket Closed!'
    message.channel.delete(args[0])
}

module.exports.help = {
  name: "close"
}

标签: discord.js

解决方案


一种简单的方法是使用一个简单的setTimeout函数。例如:

module.exports.run = async (bot, message, args) => {
    if (!message.channel.name.startsWith('ticket')) return message.channel.send('You are not in a ticket channel!');
    let reason = args[0] | 'Ticket Closed!'

    setTimeout(() => {
        message.channel.delete(args[0]);
    }, 60 * 60 * 1000); // Sets timeout for 1 hour
}

推荐阅读