Skip to content

Commit

Permalink
feat: init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Cavanagh committed May 11, 2023
1 parent 5a61b30 commit ca63c38
Show file tree
Hide file tree
Showing 10 changed files with 496 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.idea
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "digletbot",
"version": "1.0.0",
"description": "Typescript bot that serves the Dignity of War community",
"main": "index.js",
"author": "Matt Cavanagh <[email protected]>",
"license": "MIT",
"scripts": {
"start": "ts-node src/bot.ts"
},
"dependencies": {
"discord.js": "^14.11.0"
},
"devDependencies": {
"@types/node": "^20.1.3",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
}
}
22 changes: 22 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Client } from "discord.js";
import ready from "./listeners/ready";
import interactionCreate from "./events/InteractionCreate";

console.log("Bot is starting...");

const token = "MTEwNjMxODg4OTMxNjUyODE3OA.GCZ18R.WJLOW-YfBwDdJnpKlRW1L0PlwAwwTtgDELoF0s"

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

// Declare listeners
ready(client);

// Declare event handlers
interactionCreate(client);


client.login(token);

console.log(client);
4 changes: 4 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { CommandInterface } from "./interfaces/CommandInterface";
import { HelloCommand } from "./commands/HelloCommand";

export const Commands: CommandInterface[] = [HelloCommand];
16 changes: 16 additions & 0 deletions src/commands/HelloCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Client, CommandInteraction, ApplicationCommandType} from "discord.js";
import { CommandInterface } from "../interfaces/CommandInterface";

export const HelloCommand: CommandInterface = {
name: "hello",
description: "Returns a greeting",
type: ApplicationCommandType.ChatInput,
run: async (client: Client, interaction: CommandInteraction) => {
const content = "Hello there!";

await interaction.followUp({
ephemeral: true,
content
});
}
};
23 changes: 23 additions & 0 deletions src/events/InteractionCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {BaseInteraction, Client, CommandInteraction, Interaction} from "discord.js";
import {Commands} from "../commands";

export default (client: Client): void => {
client.on("interactionCreate", async (interaction: Interaction) => {
if (interaction.isCommand()) {
await handleSlashCommand(client, interaction);
}
});
};

const handleSlashCommand = async (client: Client, interaction: CommandInteraction): Promise<void> => {
const slashCommand = Commands.find(c => c.name === interaction.commandName);

if (!slashCommand) {
interaction.followUp({ content: "An error has occurred" });
return;
}

await interaction.deferReply();

slashCommand.run(client, interaction);
};
5 changes: 5 additions & 0 deletions src/interfaces/CommandInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CommandInteraction, ChatInputApplicationCommandData, Client } from "discord.js";

export interface CommandInterface extends ChatInputApplicationCommandData {
run: (client: Client, interaction: CommandInteraction) => void;
}
14 changes: 14 additions & 0 deletions src/listeners/ready.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Client } from "discord.js";
import {Commands} from "../commands";

export default (client: Client): void => {
client.on("ready", async () => {
if (!client.user || !client.application) {
return;
}

await client.application.commands.set(Commands);

console.log(`${client.user.username} is online`);
});
};
33 changes: 33 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "commonjs",
"rootDir": "./src/",
"outDir": "./dist/",
"strict": true,
"moduleResolution": "node",
"importHelpers": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true,
"removeComments": true,
"typeRoots": [
"node_modules/@types"
],
"sourceMap": false,
"baseUrl": "./"
},
"files": [
"src/Bot.ts"
],
"include": [
"./**/*.ts"
],
"exclude": [
"node_modules",
"dist"
],
}
Loading

0 comments on commit ca63c38

Please sign in to comment.