Skip to content

Commit

Permalink
feat(kontrol-bot): initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
usirin committed Oct 2, 2023
1 parent 241a6d5 commit 560eacf
Show file tree
Hide file tree
Showing 9 changed files with 1,541 additions and 126 deletions.
3 changes: 3 additions & 0 deletions apps/kontrol-bot/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DISCORD_TOKEN=
DISCORD_CLIENT_ID=
DISCORD_GUILD_ID=839425986817818654
23 changes: 23 additions & 0 deletions apps/kontrol-bot/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { SlashCommandBuilder, type CommandInteraction } from "discord.js";

type Command = {
/**
* The data for the command
*/
data: SlashCommandBuilder;
/**
* The function to execute when the command is called
*
* @param interaction - The interaction of the command
*/
execute(interaction: CommandInteraction): Promise<void> | void;
};

export const commands: Record<string, Command> = {
ping: {
data: new SlashCommandBuilder().setName("ping").setDescription("pong diye cevap verir"),
async execute(interaction: { reply: (msg: string) => Promise<unknown> }) {
await interaction.reply("Pong!");
},
},
};
27 changes: 27 additions & 0 deletions apps/kontrol-bot/deploy-commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { REST, Routes } from "discord.js";

import { commands } from "./commands";
import { env } from "./env";

// Construct and prepare an instance of the REST module
const rest = new REST().setToken(env.DISCORD_TOKEN);

// and deploy your commands!
void (async () => {
try {
console.log(`Started refreshing ${Object.keys(commands).length} application (/) commands.`);

// The put method is used to fully refresh all commands in the guild with the current set
const data = (await rest.put(
Routes.applicationGuildCommands(env.DISCORD_CLIENT_ID, env.DISCORD_GUILD_ID),
{
body: Object.values(commands).map((command) => command.data.toJSON()),
}
)) as unknown[];

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
17 changes: 17 additions & 0 deletions apps/kontrol-bot/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { parseEnv } from "znv";
import { z } from "zod";

import "dotenv/config";

export const env = parseEnv(
{
DISCORD_TOKEN: process.env.DISCORD_TOKEN,
DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
DISCORD_GUILD_ID: process.env.DISCORD_GUILD_ID,
},
{
DISCORD_TOKEN: z.string().default("discord_token"),
DISCORD_CLIENT_ID: z.string().default("discord_token"),
DISCORD_GUILD_ID: z.string().default("discord_token"),
}
);
60 changes: 60 additions & 0 deletions apps/kontrol-bot/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Client, Events, GatewayIntentBits } from "discord.js";

import { commands } from "./commands";
import { env } from "./env";

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

client.once(Events.ClientReady, (event) => {
console.log("ready", event);
});

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

// bizde var mi bu komut
const command = commands[interaction.commandName];

// yoksa ekrana hata bas ve fonksiyonun calismasini durdur
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
// command'i calistirmayi dene
await command.execute(interaction);
} catch (error) {
console.error(error);

// eger interaction normalden farkli olacak sekilde "replied" veya "deferred"
// olarak geliyorsa "followUp" mesaji gonder
// (follow up: bir onceki konuyla alakali cevap vermek/iletisim kurmak)
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: "There was an error while executing this command!",
ephemeral: true,
});
} else {
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
}
});

const start = async () => {
console.log({ DISCORD_TOKEN: env.DISCORD_TOKEN });
await client.login(env.DISCORD_TOKEN);
};

start()
.then(() => {
console.log("started");
})
.catch((error) => {
console.error(error);
});
20 changes: 20 additions & 0 deletions apps/kontrol-bot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@kampus-apps/kontrol-bot",
"version": "0.0.0",
"description": "Yo",
"main": "index.ts",
"scripts": {
"dev": "tsx index.ts"
},
"author": "Umut Sirin <[email protected]>",
"license": "MIT",
"dependencies": {
"discord.js": "14.13.0",
"dotenv": "16.3.1",
"znv": "0.3.2",
"zod": "3.21.4"
},
"devDependencies": {
"tsx": "3.13.0"
}
}
5 changes: 5 additions & 0 deletions apps/kontrol-bot/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 560eacf

Please sign in to comment.