发送消息并快速删除

13
我正在尝试让Discord机器人在10秒后删除其“系统消息”,因为我看到了很多“无效命令”错误和“完成!”通知,我希望清除它们以便查看实际消息。这与删除用户具有命令的消息不同;我已经具备了这种能力。

{btsdaf} - Ghassen Louhaichi
{btsdaf} - haley
1
@HarutheBishie,LW001下面的回答对您有用吗?如果是,请将其标记为正确。如果不是,请解释您还在寻找什么。 - Blundering Philosopher
3个回答

31
我建议您发送信息,等待回复并在那段时间之后删除返回的消息。现在的操作方式如下:
message.reply('Invalid command')
  .then(msg => {
    setTimeout(() => msg.delete(), 10000)
  })
  .catch(/*Your Error handling if the Message isn't returned, sent, etc.*/);

请参阅Discord.JS文档以获取有关Message.delete()函数的更多信息,以及Node文档以获取有关setTimeout()的信息。
过去的做法如下:
Discord.JS v12:
message.reply('Invalid command')
  .then(msg => {
    msg.delete({ timeout: 10000 })
  })
  .catch(/*Your Error handling if the Message isn't returned, sent, etc.*/);

Discord.JS v11:

message.reply('Invalid command')
  .then(msg => {
    msg.delete(10000)
  })
  .catch(/*Your Error handling if the Message isn't returned, sent, etc.*/);

14
如果您正在使用discord.js v12(目前的主要分支),您需要将msg.delete(10000)更改为msg.delete({ timeout: 10000 }) - Tony
现已弃用,请使用 setTimeout(() => msg.delete(), 10000) - PiggyPlex
由于这仍然是活跃的,我已经更新了答案,以说明在当前主分支(v13)上执行此操作的方法。 - LW001

20

当前API与早期版本不同。现在传递超时的正确方法如下。

Discord.js v13

message.reply('Invalid command!')
  .then(msg => {
    setTimeout(() => msg.delete(), 10000)
  })
  .catch(console.error);

Discord.js v12

->

Discord.js版本12

message.reply('Invalid command!')
  .then(msg => {
    msg.delete({ timeout: 10000 })
  })
  .catch(console.error);

3

每个Discord.JS版本都有一种新的超时删除方式。

Discord.JS V11:

每个Discord.JS版本都有自己的超时删除方式。
message.channel.send('Test!').then(msg => msg.delete(10000));

Discord.JS V12:

message.channel.send('Test!').then(msg => msg.delete({timeout: 10000}));

Discord.JS V13:

message.channel.send('Test!').then(msg => setTimeout(() => msg.delete(), 10000));

时间以毫秒为单位计算


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接