Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Bot for Cama discord server. To initialize new slash commands, you have to run a separate bit of code. This is for modifying their functionality

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
import { verify_discord_signature } from "https://esm.town/v/mattx/verify_discord_signature?v=8";
import { blob } from "https://esm.town/v/std/blob";
let bank = await blob.getJSON("bank");
let bets = await blob.getJSON("bets");
const starting_amount = 5;
if (!bank) {
bank = {};
await blob.setJSON("bank", bank);
}
if (!bets) {
bets = [];
await blob.setJSON("bets", bets);
}
export const handleDiscordInteraction = async (req: Request) => {
if (req.method === "GET") return new Response("GET Method Not Allowed", { status: 405 });
const body = await req.json();
const verified = await verify_discord_signature(
Deno.env.get("discordPublicKey"),
JSON.stringify(body),
req.headers.get("X-Signature-Ed25519"),
req.headers.get("X-Signature-Timestamp"),
);
// Get the user calling this slash command
const userId = await body.member?.user?.id;
if (bank[userId] === undefined) bank[userId] = starting_amount;
if (!verified) {
return new Response("signature invalid", {
status: 401,
statusText: "signature invalid",
});
}
// PING
if (body.type === 1)
return Response.json({ type: 1 }); // PONG
// APPLICATION_COMMAND interactions
if (body.type === 2) {
const command = body.data?.name;
const options = body.data?.options;
if (command === "balance") {
const amount = bank[userId];
return Response.json({
type: 4,
data: {
content: `You have ${amount} <:jopacoin:954159801049440297>`,
},
});
}
if (command === "tip") {
const recipientId = options[0].value;
const tipAmount = options[1].value;
if (tipAmount <= 0) {
return Response.json({
type: 4,
data: {
content: `You cannot tip less than 0 <:jopacoin:954159801049440297>, <@${userId}>. Nice try though.`,
},
});
}
if (tipAmount > bank[userId]) {
return Response.json({
type: 4,
data: {
content: `You cannot tip more than you have <:jopacoin:954159801049440297>, <@${userId}>. Nice try though.`,
},
});
}
bank[userId] -= tipAmount;
if (bank[recipientId] === undefined) {
bank[recipientId] = starting_amount;
}
bank[recipientId] += tipAmount;
await blob.setJSON("bank", bank);
return Response.json({
type: 4,
data: {
content: `<@${userId}> has tipped <@${recipientId}> ${tipAmount} <:jopacoin:954159801049440297>!`,
},
});
}
if (command === "bet") {
const party2 = options[0].value;
const betAmount = options[1].value;
const description = options[2].value;
// const mediatorId = options[3].value;
if (betAmount <= 0) {
return Response.json({
type: 4,
data: {
jamisonl-coppermammal.web.val.run
June 7, 2024