Skip to content

Commit

Permalink
Merge pull request #5 from BRAVO68WEB/feat/discord-bot
Browse files Browse the repository at this point in the history
feat/discord bot
  • Loading branch information
BRAVO68WEB authored Aug 11, 2024
2 parents 5a0d006 + 9d8b5d5 commit d1a12e4
Show file tree
Hide file tree
Showing 15 changed files with 718 additions and 12 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/ci-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ jobs:
- name: Installing Rust
uses: actions-rust-lang/setup-rust-toolchain@v1

# - name: Building with Cargo
# uses: actions-rs/cargo@v1
# with:
# command: build
# args: --release --manifest-path Cargo.toml
# use-cross: true
- name: Set Example Config
run: |
cp apps/discord/config.example.json apps/discord/config.json
cp apps/frontend/config.example.json apps/frontend/config.json
cp packages/backend/config.example.json packages/backend/config.json
- name: Build packages
shell: bash
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,21 @@
- 📱 Mobile-friendly UI
- 🧈 Super smooth scrolling
- 🖼️ Badge generation for stats (seen below)
- 🤖 Discord Bot application

# Technologies used

### Frontend:

<img src="https://skillicons.dev/icons?i=ts,pug,sass,go" height=40/></a>
<img src="https://skillicons.dev/icons?i=ts,pug,sass,go,discordjs" height=40/></a>

### Backend:

<img src="https://skillicons.dev/icons?i=rust,postgresql,actix" height=40/>

### Misc:

<img src="https://skillicons.dev/icons?i=nginx,docker,markdown,git,cloudflare,githubactions" height=40/>
<img src="https://skillicons.dev/icons?i=nginx,docker,markdown,git,cloudflare,githubactions,discord" height=40/>

###### (Sorta) made with [Skill Icons](https://skillicons.dev/)

Expand Down
3 changes: 3 additions & 0 deletions apps/discord/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## zer0bin Discord Bot

![Discord](../../assets/discord.png)
30 changes: 30 additions & 0 deletions apps/discord/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import config from './config.json';

const API_URL = config.api_url;
const PUBLIC_URL = config.public_url;

export const postPaste = async (content: string) => {
let paste_url;
let success = false;
const payload = { content, single_view: false }

await fetch(`${API_URL}/p/n`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
})
.then((response) => response.json())
.then((data) => {
if (data["success"]) {
success = true;
paste_url = `${PUBLIC_URL}/${data["data"]["id"]}`;
}
})
.catch((error) => {
console.error("Error:", error);
});

return { success, paste_url };
}
56 changes: 56 additions & 0 deletions apps/discord/bot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Client, GatewayIntentBits, ModalBuilder, ActionRowBuilder, Events, TextInputBuilder, TextInputStyle, ModalActionRowComponentBuilder } from 'discord.js';
import { postPaste } from './api';

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

const uploadModal = () => {
const modal = new ModalBuilder()
.setTitle('zer0bin paste')
.setCustomId('uploadModal')

const rawTextInput = new TextInputBuilder()
.setCustomId('rawText')
.setLabel("Paste your text here")
.setStyle(TextInputStyle.Paragraph);

const actionRow = new ActionRowBuilder<ModalActionRowComponentBuilder>()
.addComponents(rawTextInput);

modal.addComponents(actionRow);

return modal;
}

export const app = async (token: string) =>{
client.on('ready', () => {
if(!client.user){
return;
}
console.log(`Logged in as ${client.user.tag}!`);
});

client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;

if (interaction.commandName === 'snip') {
await interaction.showModal(uploadModal());
}
});

client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isModalSubmit()) return;

if (interaction.customId === 'uploadModal') {
const rawText = interaction.fields.getTextInputValue('rawText');
const { success, paste_url } = await postPaste(rawText);
if (!success) {
await interaction.reply({ content: 'There was an error processing your submission', ephemeral: true });
return;
}
await interaction.reply({ content: `Your paste has been uploaded to ${paste_url}`, ephemeral: true });
await interaction.user.send(`Your paste has been uploaded to ${paste_url}`);
}
});

client.login(token);
}
29 changes: 29 additions & 0 deletions apps/discord/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { REST, Routes } from 'discord.js';

export interface Command {
name: string;
description: string;
}

export class CommandManager {
private commands : Command[] = [];
private rest_client : REST;
private client_id : string;
private guild_id : string;

constructor(token: string, client_id: string, guild_id: string) {
this.rest_client = new REST({ version: '10' }).setToken(token);
this.client_id = client_id;
this.guild_id = guild_id;
}

public addCommand = (command: Command) =>{
this.commands.push(command);
}

public registerCommands = async () => {
await this.rest_client.put(Routes.applicationGuildCommands(this.client_id, this.guild_id), { body: this.commands });
console.log(`Registered ${this.commands.length} commands.`);
}
}

9 changes: 9 additions & 0 deletions apps/discord/config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"api_url": "https://yourdomain.tld/api",
"public_url": "https://yourdomain.tld",
"discord": {
"token": "your_token_here",
"guild_id": "your_guild_id_here",
"client_id": "your_client_id_here"
}
}
30 changes: 30 additions & 0 deletions apps/discord/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import config from './config.json';

import { CommandManager, Command } from "./command";
import { app } from "./bot";

if(
!config.api_url &&
!config.discord.token &&
!config.discord.client_id &&
!config.discord.guild_id &&
!config.public_url
){
console.error("Please set DISCORD_TOKEN and DISCORD_CLIENT_ID in config.json file");
process.exit(1);
}

const uploadCommand: Command = {
name: "snip",
description: "Upload a text snippet"
};

const commandManager = new CommandManager(config.discord.token, config.discord.client_id, config.discord.guild_id);

commandManager.addCommand(uploadCommand);

await commandManager.registerCommands();

await app(
config.discord.token
);
23 changes: 23 additions & 0 deletions apps/discord/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "discord",
"version": "1.3.0",
"main": "index.ts",
"type": "module",
"optionalDependencies": {
"bufferutil": "4.0.8",
"utf-8-validate": "6.0.4",
"zlib-sync": "0.1.9"
},
"scripts": {
"build": "tsc",
"dev": "node --loader @esbuild-kit/esm-loader index.ts"
},
"dependencies": {
"discord.js": "^14.15.3",
"dotenv": "^16.4.5"
},
"devDependencies": {
"@bravo68web/tsconfig": "^1.2.4",
"@esbuild-kit/esm-loader": "^2.6.5"
}
}
11 changes: 11 additions & 0 deletions apps/discord/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@bravo68web/tsconfig/nobuild.json",
"compilerOptions": {
"outDir": "dist",
"noEmit": false,
},
"exclude": [
"node_modules",
"dist"
]
}
2 changes: 1 addition & 1 deletion apps/frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zer0bin",
"source": "index.pug",
"version": "1.2.0",
"version": "1.3.0",
"browserslist": "> 0.5%, last 2 versions, not dead",
"license": "MIT",
"scripts": {
Expand Down
Binary file added assets/discord.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zer0bin",
"version": "1.0.0",
"version": "1.3.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backend",
"version": "0.0.0",
"version": "1.3.0",
"description": "A simple backend for zer0bin",
"scripts": {
"build": "cargo build --release",
Expand Down
Loading

0 comments on commit d1a12e4

Please sign in to comment.