blazing-8s/index.ts

102 lines
3 KiB
TypeScript
Raw Permalink Normal View History

2024-07-07 01:09:04 +00:00
import type { User } from "@slack/web-api/dist/response/UsersInfoResponse";
const { App } = (await import("@slack/bolt"));
import postgres from "postgres";
import "dotenv/config";
import { v4 } from "uuid";
import type { BlockElementAction, ButtonAction } from "@slack/bolt";
import { addSyntheticLeadingComment } from "typescript";
const sql = postgres({
host: '/var/run/postgresql',
database: 'haroon_uno',
username: 'haroon'
})
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET
});
app.command("start-game", async (ctx) => {
await ctx.ack();
const gameId = ctx.payload.channel_id;
const exists = await sql`SELECT * FROM games WHERE gameId = ${gameId};`
if (exists.length) {
return ctx.respond({
text: "Yeah uh... there's already a game in this channel. Sorry."
})
}
await sql`INSERT INTO games VALUES (${gameId}, ${[ ctx.context.userId! ]})`;
ctx.respond({
response_type: 'in_channel',
text: "To join the game, view this in your client. If you see this in the client, update your Slack.",
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `:hyper-dino-wave-flip: Righto, <@${ctx.context.userId}>! Starting a new game of Blazing 8s. To join, click the join button below:`
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Join Game",
"emoji": true
},
"value": gameId,
"action_id": "joinGame"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "View Players",
"emoji": true
},
"value": gameId,
"action_id": "viewCurrentPlayers"
}
]
}
]
})
})
app.action("joinGame", async (ctx) => {
await ctx.ack();
await sql`UPDATE games SET players = players || ARRAY[${ctx.context.userId!}] WHERE gameId = ${(ctx.action as ButtonAction).value!}`;
ctx.respond({
response_type: 'ephemeral',
text: 'Okie dokie! You\'ve been added.'
})
})
app.action("viewCurrentPlayers", async (ctx) => {
await ctx.ack();
const players = await sql`SELECT * FROM games WHERE gameId = ${(ctx.action as ButtonAction).value!}`;
ctx.respond({
response_type: 'ephemeral',
text: `There are ${players.length} in-game.`
})
})
; (async () => {
await app.start({ path: "/run/user/2151/uno-bot.sock" });
console.log('⚡️ Bolt app is running!');
})();