-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrematch.js
82 lines (76 loc) · 1.97 KB
/
rematch.js
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
const { SlashCommandBuilder } = require("discord.js");
const {
clearDeleteTimer,
createPtnFile,
drawBoard,
getGameData,
getTheme,
getTurnMessage,
isPlayer,
saveGameData,
sendMessage,
sendPngToDiscord,
setInactiveTimer,
} = require("../util");
module.exports = {
data: new SlashCommandBuilder()
.setName("rematch")
.setDescription("Swap seats and play again using the same game settings."),
async execute(interaction) {
const gameData = getGameData(interaction);
if (!gameData) {
return sendMessage(
interaction,
"I couldn't find a previous game in this channel.",
true
);
} else if (gameData.tps) {
return sendMessage(
interaction,
"There's still a game in progress!",
true
);
} else if (!isPlayer(interaction.member.id, gameData)) {
return sendMessage(
interaction,
"Only the previous players can rematch.",
true
);
}
// Swap players
[
gameData.player1,
gameData.player1Id,
gameData.player2,
gameData.player2Id,
] = [
gameData.player2,
gameData.player2Id,
gameData.player1,
gameData.player1Id,
];
// Generate new game ID
const gameId = createPtnFile(gameData);
gameData.gameId = gameId;
interaction.channel.setName(`${gameData.player1}-🆚-${gameData.player2}`);
let canvas;
try {
canvas = drawBoard(
{ ...gameData, tps: gameData.initialTPS || gameData.size },
getTheme(interaction)
);
} catch (err) {
console.error(err);
return sendMessage(
interaction,
"Something went wrong when I tried to draw the board.",
true
);
}
saveGameData(interaction, { tps: canvas.id, gameData });
const message = getTurnMessage(gameData, canvas);
sendPngToDiscord(interaction, canvas, message);
clearDeleteTimer(interaction);
setInactiveTimer(interaction, gameData, canvas);
},
};