# discordjs-decorate

> typescript decorators for discordjs

Latest version **0.0.64** (published 2021-10-08) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install discordjs-decorate
pnpm add discordjs-decorate
yarn add discordjs-decorate
bun add discordjs-decorate
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.64 |
| Published | 2021-10-08 |
| First published | 2021-10-05 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 27.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | xdarkyne |
| Maintainers | xdarkyne |
| Keywords | discord.js, discord.js-decorate, typescript, decorators, discordapp, client, node, bot, api |

## Links

- npm: https://www.npmjs.com/package/discordjs-decorate
- Repository: https://github.com/xdarkyne/discordjs-decorate
- Homepage: https://github.com/xdarkyne/discordjs-decorate#readme
- Issues: https://github.com/xdarkyne/discordjs-decorate/issues
- npm.io page: https://npm.io/package/discordjs-decorate

## Dependencies (4)

- [discord.js](https://npm.io/package/discord.js.md) ^13.1.0
- [typescript](https://npm.io/package/typescript.md) ^4.4.3
- [@discordjs/rest](https://npm.io/package/@discordjs/rest.md) ^0.1.0-canary.0
- [discord-api-types](https://npm.io/package/discord-api-types.md) ^0.23.1

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.0.64 (latest) — 2021-10-08
- 0.0.63 — 2021-10-08
- 0.0.62 — 2021-10-06
- 0.0.61 — 2021-10-06
- 0.0.6 — 2021-10-06
- 0.0.5 — 2021-10-05
- 0.0.4 — 2021-10-05
- 0.0.3 — 2021-10-05
- 0.0.2 — 2021-10-05
- 0.0.1 — 2021-10-05

## README

<div align="center">
<h1>Decorate</h1>
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/workflow/status/xDarkyne/discordjs-decorate/Publish?label=Publish&logo=npm&style=for-the-badge">
<img alt="npm" src="https://img.shields.io/npm/v/discordjs-decorate?label=Version&style=for-the-badge">
<img alt="npm" src="https://img.shields.io/npm/dw/discordjs-decorate?style=for-the-badge">
</div>

### Table of Contents
1. [ Why ](#why)
2. [ Basic Usage ](#usage)
3. [ Options ](#options)
4. [ Todo ](#todo)
5. [ Changelog ](#changes)
6. [ Dependencies ](#deps)

<a name="why"></a>

### 1. Why?
This is a fun/learning project for me to learn TypeScript decorators. So this repository is not actively developed on to deliver the fastest and best experience in creating discord bots. If you want a already established version of what I'm trying to do use [OwenCalvin's discord.ts](https://github.com/OwenCalvin/discord.ts)

Pull requests welcome!

<a name="usage"></a>

### 2. Basic Usage
#### 1. Install npm  package
```ts
npm install discordjs-decorate
//or
yarn add discordjs-decorate
```
#### 2. create a slash command in another directory
```ts
//./commands/ping.command.ts
import { BaseCommand, Command } from 'discordjs-decorate';
import { CommandInteraction } from 'discord.js';

@Command({ name: "ping", description: "responds with pong" })
export class PingCommand extends BaseCommand {
  async execute(interaction: CommandInteraction) {
    await interaction.reply("pong");
  }
}
```
#### 3. Register slash commands - this is 99% like in discordjs
```ts
/// index.ts
import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v9';
import { CommandService } from 'discordjs-decorate'; // <-- this is new
const rest = new REST({ version: '9' }).setToken("YOUR BOT TOKEN");

(async() => {
  try {
    console.log("Started refreshing Application");
    const commands = await CommandService.getSlashCommandsObject("YOUR COMMANDS PATH"); // <-- and this is new
    // default command path  is commands/

    await rest.put(
      Routes.applicationGuildCommands("YOUR APPLICATION ID ", "YOUR GUILD ID"),
      { body: commands},
    );

    console.log("Successfully reloaded application");
  } catch(error) {
    console.error(error);
  }
})();
```
#### 3. Setup client like -- also 99% like in discordjs
```ts
import { Client, Intents, CommandInteraction } from 'discord.js';
import { CommandService } from 'discordjs-decorate'; // <-- this is new
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}`);
});

client.on('interactionCreate', async(interaction: CommandInteraction) => {
  if (!interaction.isCommand()) return;
  await CommandService.getInstance().executeCommand(interaction); // <-- this is new
});

client.login("YOUR BOT TOKEN");
```
#### 4. Start client and test the command
```ts
>> input: /example
>> expected output: works!
```

<a name="options"></a>

### 3. Options
if you want to use options for your slash commands you can use the `@Option` Decorator.

The fields are the same as in discordjs, `name`, `description`, `type`, `required` and `choices`.
#### Option without choices
```ts
@Command({ name: "ping", description: "replies with pong" })
@Option({ name: "option", description: "string  option", type: 3, required: false })
class PingCommand extends BaseCommand {
  async execute(interaction: CommandInteraction) {
    interaction.reply(interaction.options.getString("option"));
  }
}
```
#### Option with choices
```ts
@Command({ name: "ping", description: "replies with pong" })
@Option({ name: "animal", description: "string  option", type: 3, required: false, choices: [
  { name: "cat", value: "animal_cat" }, 
  { name: "dog", value: "animal_dog" }
]})
class PingCommand extends BaseCommand {
  async execute(interaction: CommandInteraction) {
    interaction.reply(interaction.options.getString("animal"));
  }
}
```

<a name="todo"></a>

### 4. Todo
- [x] Basic Command Service
- [x] Slash Commands
- [x] Required Parameters
- [x] Optional Parameters
- [x] NPM Package
- [x] Automatic integration with npm
- [x] Automatic Command Registration
- [ ] Decorator for discord client
- [ ] Decorator for discord rest api 
- [ ] Automatic Help Command
- [ ] Sub Commands
- [ ] Optimizations

<a name="changes"></a>

### 5. Changelog

### v0.0.62
**[Fixes]**
- Solved an issue that resulted in the CommandService not finding your command directory

### v0.0.61
**[Additions]**
- Added `readCommandDir(path: string)` to CommandService to automatically import all commands from given directory (default `commands/`, files have to end with `.command.ts` or `.command.js`)

**[Fixes]**
- Solved an issue where the bot would crash if the given directory would not exits

<a name="deps"></a>

### 6. Dependencies:
 - @discordjs/rest
 - discord-api-types
 - discord.js
 - typescript

---
_Source: https://npm.io/package/discordjs-decorate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
