Skip to content

Instantly share code, notes, and snippets.

@Xetera
Created March 11, 2019 01:13
Show Gist options
  • Save Xetera/b742ff3782d1c46c58ff3683ea7950bf to your computer and use it in GitHub Desktop.
Save Xetera/b742ff3782d1c46c58ff3683ea7950bf to your computer and use it in GitHub Desktop.
Helper function that makes creating functions with Akairo much less verbose
import { CommandOptions, Command } from "discord-akairo";
interface CreateCommand extends CommandOptions {
/**
* The exec function can be called in 3 different ways, however,
* Typescript doesn't seem to be able to support explicit `this`
* coming from external types, union types or overloads. In order to
* specify the `this` type the signature MUST be directly assigned
* in the interface
*/
exec: (this: Command, message: Message, args: any, edited: boolean) => any;
id: string;
}
export const createCommand = ({ id, exec, ...rest }: CreateCommand) => class extends Command {
constructor() {
super(id, exec, rest);
}
};
import { createCommand } from "./akairo.ts";
import { Message } from "discord.js"
export default createCommand({
id: "ping",
aliases: ["ping", "🏓"],
category: "utility",
description: "Checks latency of the bot",
async exec(msg: Message) {
const m = await msg.channel.send("Ping...") as Message;
const latency = Math.round(m.createdTimestamp - msg.createdTimestamp);
const response = `Pong! 🏓 Latency: **${latency}ms**`;
return m.edit(response);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment