Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/twlite/discord.tsx

Create Discord.js components in JSX
https://github.com/twlite/discord.tsx

babel buttons components discord discord-components-tsx discordjs hacktoberfest jsx react tsx typescript

Last synced: 17 days ago
JSON representation

Create Discord.js components in JSX

Awesome Lists containing this project

README

        

# discord.tsx

Create Discord.js components in JSX.

**[https://www.npmjs.com/package/discord.tsx](https://www.npmjs.com/package/discord.tsx)**

# Setup

## Install discord.js

```sh
$ npm i discord.js
```

# Env Setup

## TypeScript
### Add these in your **`tsconfig.json`#compilerOptions**

```json
"jsxFactory": "DiscordComponents.createComponent",
"jsx": "react",
"jsxFragmentFactory": "DiscordComponents.Fragment"
```

## Babel

Specify pragma for custom jsx factory with **[`@babel/plugin-transform-react-jsx`](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx)**.

```jsx
/** @jsx DiscordComponents.createComponent */
/** @jsxFrag DiscordComponents.Fragment */
```

# Components Available

- MessageButton
- MessageSelectMenu
- MessageEmbed

## Root Fragment Data
Root fragment: `<>...>` returns `{ embeds: MessageEmbed[], components: MessageActionRow[] }`.

# Example

## Single Row

```tsx
import {
DiscordComponents,
MessageActionRow,
MessageButton,
} from "discord.tsx";

client.on("messageCreate", (message) => {
if (message.content === "=btn") {
const componentData = (
<>







>
);

return message.channel.send({ ...componentData, content: "Buttons 🖱" });
}
});
```

### Preview

![](https://i.imgur.com/IuEqtdy.png)

## Or Multiple

```tsx
import {
DiscordComponents,
MessageActionRow,
MessageButton,
} from "discord.tsx";

client.on("messageCreate", (message) => {
if (message.content === "=btn") {
const componentData = (
<>














>
);

return message.channel.send({ ...componentData, content: "Buttons 🖱" });
}
});
```

### Preview

![](https://i.imgur.com/KxHMgn2.png)

## Select Menu

```tsx
import {
DiscordComponents,
MessageActionRow,
MessageSelectMenu,
MessageSelectOption
} from "discord.tsx";

client.on("messageCreate", (message) => {
if (message.content === "=select") {
const componentData = (
<>







>
);

return message.channel.send({ ...componentData, content: "Select It" });
}
});
```

### Preview
![](https://i.imgur.com/EmeGYYy.png)

## Message Embed

```tsx
import {
DiscordComponents,
MessageEmbed,
MessageEmbedAuthor,
MessageEmbedFields,
MessageEmbedField,
MessageEmbedFooter,
MessageEmbedImage,
MessageEmbedThumbnail
} from "discord.tsx";

client.on("messageCreate", (message) => {
if (message.content === "=embed") {
const componentData = (
<>










>
);

return message.channel.send(componentData);
}
});
```

### Preview
![](https://i.imgur.com/eaqub2x.png)

## Other examples

```tsx
const componentData = (
<>

{Array.from({ length: 5 }, (_, i) => (

))}



{Array.from({ length: 5 }, (_, i) => (

))}




{Array.from({ length: 10 }, (_, i) => {
const counter = ++i;
return
})}


>
);

return message.channel.send(componentData);
```