Skip to content

Commit

Permalink
docs go brrrr (#22)
Browse files Browse the repository at this point in the history
Signed-off-by: Ash Entwisle <[email protected]>
  • Loading branch information
ash-entwisle authored Aug 21, 2023
1 parent 6b59e3c commit 63de33f
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 84 deletions.
122 changes: 41 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Archived

> This project has moved [here](https://github.com/ash-entwisle/openbot-rs). It *should* still work for the time being, but im no longer going to maintain it.

# openbot

> This project has moved [here](https://github.com/ash-entwisle/openbot-rs). It *should* still work for the time being, but im no longer going to actively maintain this repo.
I got bored over the summer, so I decided to learn typescript by making a discord bot.

## Features
Expand Down Expand Up @@ -36,103 +35,64 @@ Run the install script:
curl -fsSL https://raw.githubusercontent.com/ash-entwisle/openbot/main/install/install.sh | sh
```

then edit the `.env` and the `config.json` file:
then edit the `.env`

```sh
nano .env

nano config.json
```

and start the docker containers:

```sh
docker compose up -d
```

### From Source (running with bun)

Clone the repo:

```sh
git clone https://github.com/ash-entwisle/openbot.git
```

then install the dependencies:

```sh
bun install
```

create a `.env` file

```sh
touch .env
```

and then add the following:

```sh
DISCORD_TOKEN=your_discord_bot_token
DISCORD_ID=your_discord_bot_id
```

edit the `config.json` file:

```sh
nano config.json
```

and start the bot:

```sh
bun start
```

### From Source (running with docker)

Clone the repo:

```sh
git clone https://github.com/ash-entwisle/openbot.git
```

create a `.env` file

```sh
touch .env
```

and then the following:

```sh
DISCORD_TOKEN=your_discord_bot_token
DISCORD_ID=your_discord_bot_id
```

then edit the `config.json` file:

```sh
nano config.json
```

then build the docker image:
Then edit the `config.toml` file:

```sh
bun docker
nano config.toml
```

and then start the docker containers:
and start the docker containers:

```sh
docker compose up -d
```


## Contributing

If you want to contribute, feel free to fork the repo and make a pull request.
If you have any questions, feel free to open an issue.

## Adding a command

To add a command, create a file in a folder in `src/commands/` with the name of the command.
For example, if you wanted to add a command called `ping`, you would create a file called `ping.ts` in `src/commands/misc`.
Then, add the following code to the file:

```ts
import { latency } from '../../libs/sysinfo';
import { embed } from '../../libs/reply';
import { Command } from '../../libs/command';

export const data = new Command({
name: 'ping',
description: 'Get the latency of the bot.',
dmPermission: true,
nsfw: false,
admin: false,
execute: execute
})

export async function execute(interaction: any) {
embed({
interaction: interaction,
title: "",
content: `**Latency:** \`${latency(interaction)}ms.\``,
ephemeral: true
});
}
```

First, import all the libraries you need.
Then, create and export a `Command` object with the properties found in the interface `ICommandData` (found in ./src/libs/command.ts).
Then, export an `execute` function that takes an `interaction` object as an argument, this will be used to collect and format data, and send a reply.

## License

Distriuted under the AGPL-3.0 License. See `LICENSE` for more information.
19 changes: 17 additions & 2 deletions src/libs/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,38 @@ import toml from 'toml';

const fs = require('node:fs');

// create a singleton that will be used to store the client information

/**
* Singleton class representing the Discord bot.
*/
export class Bot {
private static instance: Bot;

/** The Discord.js client instance. */
public client: Client;

/** Collection of registered commands. */
public commands: Collection<string, Command>;

/** Collection of registered events. */
public events: Collection<string, any>;

/** The bot's configuration object. */
public config: any;

/**
* Private constructor to enforce singleton pattern.
*/
private constructor() {
this.client = new Client({ intents: [GatewayIntentBits.Guilds] });
this.commands = new Collection();
this.events = new Collection();
this.config = toml.parse(fs.readFileSync(`${__dirname}/../../${Bun.env.BOT_CONFIG}`, 'utf-8'));
}

/**
* Returns the singleton instance of the Bot class.
* @returns The Bot instance.
*/
public static getInstance(): Bot {
if (!Bot.instance) {
Bot.instance = new Bot();
Expand Down
86 changes: 85 additions & 1 deletion src/libs/command.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,91 @@
import { Attachment, SlashCommandBuilder } from "discord.js";

/**
* Represents the data required to define a command.
*/
export interface ICommandData {
/**
* The name of the command, this is what the user will type to execute the command.
*/
name: string;
/**
* A brief description of the command. This is displayed in the help menu and in command suggestions.
*/
description: string;
/**
* An array of options for the command. See the ICommandOption interface for more information.
* @default []
*/
options?: ICommandOption[];
/**
* Whether the command can be executed in a DM.
* @default false
*/
dmPermission?: boolean;
/**
* Whether the command is NSFW (Not Safe For Work).
* @default false
*/
nsfw?: boolean;
/**
* Whether the command requires admin permission to be executed.
* @default false
*/
admin?: boolean;
/**
* The function to execute when the command is called.
* @param interaction - The interaction object representing the command call.
* @returns void
*/
execute: (interaction: any) => void;
}

/**
* Represents an option for a command.
*/
export interface ICommandOption {
/**
* The type of the option. See the ECommandOption enum for a list of valid types.
*/
type: ECommandOption;
/**
* The name of the option. This is what the user will type to set the option.
*/
name: string;
/**
* The description of the option. This is displayed in the help menu and in command suggestions.
*/
description: string;
/**
* Whether the option is required or not. If an option is required, the user must set it when executing the command.
* @default false
*/
required?: boolean;
choices?: ICommandOptionChoice[]; // TODO: Implement choices, may need to make a pr to discord.js
/**
* The choices for the option. This is an array of objects that contain the name and value of the choice.
* TODO: Implement choices, may need to make a pr to discord.js
* @default []
*/
choices?: ICommandOptionChoice[];
}

/**
* Represents a choice for a command option.
*/
export interface ICommandOptionChoice {
/**
* The name of the choice. This is what the user will type to set the choice.
*/
name: string;
/**
* The value of the choice, can be a string or a number. This is what the choice will be set to.
*/
value: string | number;
}

/**
* Enum representing the different types of options that can be used for a command.
*/
export enum ECommandOption {
StringOption,
IntegerOption,
Expand All @@ -36,11 +99,32 @@ export enum ECommandOption {
}


/**
* Represents a command that can be executed by a user in a Discord server.
*/
export class Command {
/**
* The SlashCommandBuilder object that defines the command's properties.
*/
public command: SlashCommandBuilder;

/**
* Whether the command can only be executed by server administrators.
*/
public admin: boolean;

/**
* The function that is executed when the command is invoked.
* @param interaction The interaction object that represents the command invocation.
* @returns void
*/
public execute: (interaction: any) => void;

/**
* Creates a new Command object.
* @param data An object that contains the command's properties.
* @returns Command
*/
constructor(data: ICommandData) {
this.command = new SlashCommandBuilder()
.setName(data.name)
Expand Down
Empty file removed src/libs/event.ts
Empty file.
34 changes: 34 additions & 0 deletions src/libs/reply.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
import { EmbedBuilder } from "discord.js";
import { Bot } from "./bot";

/**
* Represents a reply to a Discord interaction.
*/
export interface Reply {
/**
* The interaction object that represents the command invocation.
*/
interaction: any;
/**
* The title of the reply.
* @default ""
*/
title?: string;
/**
* The content of the reply.
* @default ""
*/
content?: string;
/**
* The footer of the reply.
* @default ""
*/
footer?: string;
/**
* the url of the footer icon.
* @default Bot.getInstance().config.embed.footerIcon
*/
footerIcon?: string;
/**
* The Hex Code of the color of the reply.
* @default Bot.getInstance().config.embed.color
*/
color?: string;
/**
* Whether the reply should be ephemeral.
* @default false
*/
ephemeral?: boolean;
/**
* Whether the reply should have a timestamp.
* @default false
*/
timestamp?: boolean;
}

Expand Down
Loading

0 comments on commit 63de33f

Please sign in to comment.