-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from BRAVO68WEB/feat/discord-bot
feat/discord bot
- Loading branch information
Showing
15 changed files
with
718 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## zer0bin Discord Bot | ||
|
||
![Discord](../../assets/discord.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.