Skip to content

Commit

Permalink
[stage-manager] initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
usirin committed Jun 1, 2024
1 parent 91a6b99 commit 0646cf4
Show file tree
Hide file tree
Showing 10 changed files with 397 additions and 0 deletions.
175 changes: 175 additions & 0 deletions bots/stage-manager/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
15 changes: 15 additions & 0 deletions bots/stage-manager/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @kampus/discord-stage-manager

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.1.10. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
32 changes: 32 additions & 0 deletions bots/stage-manager/bin/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Debug from "debug";
import { REST, Routes, SlashCommandBuilder } from "discord.js";
import { commands } from "../commands";
import { env } from "../env";

const debug = Debug("stage-manager:deploy");

// debug.enabled = true;

const rest = new REST({ version: "10" }).setToken(env.DISCORD_TOKEN);

const deploy = async () => {
try {
debug(`Started refreshing ${Object.keys(commands).length} application (/) commands`);

const body = Object.entries(commands).map(([name, { description }]) =>
new SlashCommandBuilder().setName(name).setDescription(description).toJSON(),
);

const data = (await rest.put(
Routes.applicationGuildCommands(env.DISCORD_APP_ID, env.DISCORD_SERVER_ID),
{ body },
)) as unknown[];

debug(`Successfully deployed ${data.length} application (/) commands!`);
debug(data);
} catch (error) {
debug(error);
}
};

deploy();
44 changes: 44 additions & 0 deletions bots/stage-manager/bin/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Debug from "debug";
import { Client, Events, GatewayIntentBits } from "discord.js";
import { type CommandCollection, commands } from "../commands";

const debug = Debug("stage-manager:start");
debug.enabled = true;

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

client.once(Events.ClientReady, (c) => {
debug(`Ready! Logged in as ${c.user.username}`);
});

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

debug(`Received command: ${interaction.commandName} from user: ${interaction.user.username}`);

const command = (commands as CommandCollection)[interaction.commandName];
if (!command) {
debug(`Command not found: ${interaction.commandName}`);
return;
}

try {
debug(`Executing command: ${interaction.commandName}`);
await command.handler(interaction);
} catch (error) {
debug(`Error while executing command: ${interaction.commandName}`);
if (interaction.deferred || interaction.replied) {
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,
});
}
}
});

client.login(process.env.DISCORD_TOKEN);
57 changes: 57 additions & 0 deletions bots/stage-manager/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { type ChatInputCommandInteraction, GuildScheduledEventManager } from "discord.js";

export type CommandCollection = Record<
string,
{
description: string;
handler: (interaction: ChatInputCommandInteraction) => Promise<void>;
}
>;

export const commands = {
whoami: {
description: "Provides information about the user who invoked the command.",
async handler(interaction) {
await interaction.reply(`You are ${interaction.user.username}!`);
},
},
"list-events": {
description: "Lists all events in the server.",
async handler(interaction) {
const guild = interaction.guild;
if (!guild) {
await interaction.reply("This command must be run in a server!");
return;
}

const events = await guild.scheduledEvents.fetch();

console.log(
events.map((event) => {
return {
name: event.name,
description: event.description,
endAt: event.scheduledEndAt,
endTimestamp: event.scheduledEndTimestamp,
startAt: event.scheduledStartAt,
startTimestamp: event.scheduledStartTimestamp,
url: event.url,
status: event.status,
};
}),
);

await interaction.reply(`There are ${events.size} events in this server!`);
},
},
"create-event": {
description: "Creates a new event in the server.",
async handler(interaction) {
const guild = interaction.guild;
if (!guild) {
await interaction.reply("This command must be run in a server!");
return;
}
},
},
} satisfies CommandCollection;
7 changes: 7 additions & 0 deletions bots/stage-manager/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { parseEnv, z } from "znv";

export const env = parseEnv(process.env, {
DISCORD_TOKEN: z.string(),
DISCORD_APP_ID: z.string(),
DISCORD_SERVER_ID: z.string(),
});
Empty file added bots/stage-manager/index.ts
Empty file.
22 changes: 22 additions & 0 deletions bots/stage-manager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@kampus/discord-stage-manager",
"module": "index.ts",
"type": "module",
"scripts": {
"deploy": "bun ./bin/deploy.ts",
"start": "bun ./bin/start.ts"
},
"devDependencies": {
"@types/bun": "latest",
"@types/debug": "^4.1.12"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"debug": "^4.3.4",
"discord.js": "^14.15.2",
"znv": "^0.4.0",
"zod": "^3.23.8"
}
}
27 changes: 27 additions & 0 deletions bots/stage-manager/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
Loading

0 comments on commit 0646cf4

Please sign in to comment.