按ID删除邮件

时间:2018-10-14 23:34:35

标签: javascript node.js bots discord discord.js

我创建了一个在服务器上显示民意调查投票面板的机器人,理想情况下,我希望它删除旧消息并发送新信息以在人们对事物进行投票时更新民意调查。

我已经设法使用message.channel.fetchMessage获得了旧的消息ID,并且'LastMessageID'是正确的ID,但是我似乎找不到不产生大量错误的选择和删除消息的方法在我的控制台中。 例如,我尝试过:

message.channel.fetchMessage(LastMessageID)
 .then(message.delete)

它仅给出以下错误:

(node:82184) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'client' of undefined
    at resolve (C:\Users\Username\Desktop\TestBot\node_modules\discord.js\src\structures\Message.js:480:14)
    at new Promise (<anonymous>)
    at delete (C:\Users\Username\Desktop\TestBot\node_modules\discord.js\src\structures\Message.js:479:14)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7) (node:82184) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:82184) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我真是愚蠢,无法解决如何简单地做一些事情,就像通过ID删除一条消息一样。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

这就是您要寻找的东西:找到消息,然后将其删除。

@Service("taskExecutor")
public class TaskExecutorRouter implements TaskExecutor {
    private final Map<String, TaskExecutor> taskExecutorMap;

    @Autowired
    public TaskExecutorRouter(@Qualifier("taskExecutorOne") TaskExecutor taskExecutorOne,
                              @Qualifier("taskExecutorTwo") TaskExecutor taskExecutorTwo,
                              @Qualifier("taskExecutorThree") TaskExecutor taskExecutorThree) {
        this.taskExecutorMap = new HashMap<>();
        taskExecutorMap.put("taskExecutorOne", taskExecutorOne);
        taskExecutorMap.put("taskExecutorTwo", taskExecutorTwo);
        taskExecutorMap.put("taskExecutorThree", taskExecutorThree);
    }

    @Override
    public void executeTask(Task task) {
        taskExecutorMap.get(task.getExecutorType()).executeTask(task);
    }
}

这很好,但是让我建议您一种更好的方法:

// ASSUMPTIONS:
// channel: the channel you want the message to be sent in
// lastmsg: the id of the last poll message

channel.fetchMessage(lastmsg).then(msg => msg.delete());