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";
// Bump version
import { Routes } from "https://deno.land/x/discord_api_types@0.37.99/v9.ts";
import { REST } from "npm:@discordjs/rest";
import { createAudioPlayer, createAudioResource, joinVoiceChannel } from "npm:@discordjs/voice";
import googleTTS from "npm:google-tts-api";
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);
}
const rest = new REST({ version: "9" }).setToken(process.env.DISCORD_BOT_TOKEN);
async function findUserVoiceChannel(guildId, userId) {
try {
const channels = await rest.get(Routes.guildChannels(guildId));
const voiceChannels = channels.filter(channel => channel.type === 2);
for (const channel of voiceChannels) {
const members = await rest.get(Routes.channelVoiceStates(channel.id));
if (members.some(member => member.user.id === userId)) {
return channel.id;
}
}
} catch (error) {
console.error("Error finding user voice channel:", error);
}
return null;
}
async function sayMessage(guildId, channelId, message) {
const url = googleTTS.getAudioUrl(message, {
lang: "en",
slow: false,
host: "https://translate.google.com",
});
const connection = joinVoiceChannel({
channelId: channelId,
guildId: guildId,
adapterCreator: createDiscordJSAdapter(channelId, guildId),
});
const player = createAudioPlayer();
const resource = createAudioResource(url);
player.play(resource);
connection.subscribe(player);
return new Promise((resolve) => {
player.on("stateChange", (oldState, newState) => {
if (newState.status === "idle") {
connection.destroy();
resolve();
}
});
});
}
function createDiscordJSAdapter(channelId, guildId) {
return {
sendPayload: (payload) => {
rest.post(Routes.channelMessages(channelId), { body: payload });
},
destroy: () => {
rest.delete(Routes.channelVoiceState(guildId, channelId));
},
};
}
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)
jamisonl-adorableredchameleon.web.val.run
September 4, 2024