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
import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook";
import process from "node:process";
import { play_history, db } from 'https://esm.town/v/alvi/SteamPlaytimeHistory_playHistorySchema';
const STEAM_WEB_API_KEY = process.env.STEAM_WEB_API_KEY;
const STEAM_ID = process.env.STEAM_ID;
const DISCORD_WEBSOCKET_URL = process.env.DISCORD_WEBSOCKET_URL;
export default async function(interval: Interval) {
if (!STEAM_WEB_API_KEY || !STEAM_ID || !DISCORD_WEBSOCKET_URL) {
throw new Error("STEAM_WEB_API_KEY, STEAM_ID or DISCORD_WEBSOCKET_URL not set");
}
const url =
`http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key=${STEAM_WEB_API_KEY}&steamid=${STEAM_ID}&format=json`;
const options = { method: "GET" };
try {
const response = await fetch(url, options);
const data = await response.json();
console.log({ data });
if (!data.response || !data.response.games) {
discordWebhook({
url: DISCORD_WEBSOCKET_URL,
content: `Wrong response data from Steam`,
});
return;
}
try {
await db.insert(play_history).values(data.response.games);
} catch (error) {
console.error(error);
discordWebhook({
url: DISCORD_WEBSOCKET_URL,
content: `Error While inserting into DB: ${error?.message}`,
});
}
} catch (error) {
console.error(error);
discordWebhook({
url: DISCORD_WEBSOCKET_URL,
content: `Error While retrieving steam data: ${error?.message}`,
});
}
}