-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
126 lines (116 loc) · 2.96 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import {
CrossBuild,
CustomCheckFunction,
DiscordInteractionModule,
GeneratedMessage,
} from "crossbuild"
import { Game } from "./classes/Game"
export const diceEmoji = {
0: {
locked: "1173655985986994218",
unlocked: "1173655985986994218",
},
1: {
locked: "1176069596780953630",
unlocked: "1176069624249466880",
},
2: {
locked: "1176069598068613221",
unlocked: "1176069625952350218",
},
3: {
locked: "1176069599763103765",
unlocked: "1176069627659419719",
},
4: {
locked: "1176069601872855060",
unlocked: "1176069629051949058",
},
5: {
locked: "1176069603487658015",
unlocked: "1176069631044231208",
},
6: {
locked: "1176069604716597258",
unlocked: "1176069632361246801",
},
} as const
export const games: Array<Game> = []
export const getGame = (id: string) => {
return games.find((game) => game.id === id)
}
export const getPlayerGame = (id: string) => {
return games.find((game) => game.status !== "ended" && game.players.find((p) => p.id === id))
}
const isInGame: CustomCheckFunction = async (interaction) => {
if (!interaction.user) throw new Error("uh")
if (!getPlayerGame(interaction.user.id)) {
return {
content: "You are not in a game",
ephemeral: true,
} satisfies GeneratedMessage
}
return null
}
const notInGame: CustomCheckFunction = async (interaction) => {
if (!interaction.user) throw new Error("uh")
const game = getPlayerGame(interaction.user.id)
console.log(game)
if (getPlayerGame(interaction.user.id)) {
return {
content: "You are already in a game",
ephemeral: true,
} satisfies GeneratedMessage
}
return null
}
const isGameOwner: CustomCheckFunction = async (interaction) => {
if (!interaction.user) throw new Error("uh")
if (!getPlayerGame(interaction.user.id)) {
return {
content: "You are not in a game",
ephemeral: true,
} satisfies GeneratedMessage
}
if (getPlayerGame(interaction.user.id)!.owner !== interaction.user.id) {
return {
content: "You are not the owner of this game",
ephemeral: true,
} satisfies GeneratedMessage
}
return null
}
const isYourTurn: CustomCheckFunction = async (interaction) => {
const game = getPlayerGame(interaction.user!.id)
if (!game) {
return {
content: "You are not in a game",
ephemeral: true,
} satisfies GeneratedMessage
}
if (game.activeTurn?.playerId !== interaction.user!.id) {
return {
content: "It's not your turn",
ephemeral: true,
} satisfies GeneratedMessage
}
return null
}
export const bot = new CrossBuild({
name: "Yahtzee",
modules: [
new DiscordInteractionModule({
name: "Discord",
token: process.env.DISCORD_TOKEN!,
options: {
intents: ["Guilds"],
},
}),
],
componentPaths: ["./commands", "./buttons", "./selectMenus"],
customChecks: [isInGame, isGameOwner, isYourTurn, notInGame],
})
bot.on("debug", (message) => console.log(message))
bot.on("error", (error) => console.error(error))
bot.on("warn", (warning) => console.warn(warning))
bot.on("ready", () => console.log("Ready!"))