-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,541 additions
and
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DISCORD_TOKEN= | ||
DISCORD_CLIENT_ID= | ||
DISCORD_GUILD_ID=839425986817818654 |
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 @@ | ||
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!"); | ||
}, | ||
}, | ||
}; |
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,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); | ||
} | ||
})(); |
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,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"), | ||
} | ||
); |
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,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); | ||
}); |
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,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" | ||
} | ||
} |
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,5 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["**/*.ts", "**/*.tsx"], | ||
"exclude": ["node_modules"] | ||
} |
Oops, something went wrong.