在 Discord.js 函数之后使用 'return' 有什么区别吗?

Is there any difference using 'return' after Discord.js function?

我正在使用 Commando 框架编写 Discord 机器人,我想知道是否需要在最后一个 Discord.js 或给定函数中的 Commando 函数之后使用 return 关键字才能 end/finish 命令还是根本不需要?我想确保该特定命令已完成。如果它甚至有意义。

const { Command } = require('discord.js-commando');

module.exports = class HelloCommand extends Command {
    constructor(client) {
        super(client, {
            name: 'Hello',
            aliases: [],
            group: 'general',
            memberName: 'hello',
            description: 'Displays the Hello message',
            guildOnly: true
        });
    }

    run(message) {
        return message.channel.send("Hello!");
    }
};

如果需要,应该使用这些方法中的哪一种?这有关系吗?

run(message) {
    return message.channel.send("Hello!");
}

run(message) {
    message.channel.send("Hello!");
    return;
}

第二个是没有意义的,因为所有 javascript 函数自动 returns undefined 无论你是否在函数末尾使用 return。我建议使用第一个。

你可以通过这样做

来检查你是否需要第一个中的return
run(message) {
        const response = message.channel.send("Hello!");
        console.log(response)
    }

如果控制台打印 undefined,您根本不必使用 return。

If you are not sure you can always look for typings definition in github

export class Command {

  public run(message: CommandoMessage, args: object | string | string[], fromPattern: 
    boolean, result?: ArgumentCollectorResult): Promise<Message | Message[] | null> | 
    null;
}

嗯,这意味着您可以 return 要么承诺,要么什么都不做。