|
| 1 | +const SCOREBOARD_API = "https://boundvariable.space/scoreboard"; |
| 2 | +const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL; |
| 3 | +const TEAM_NAME = "Maximum"; |
| 4 | + |
| 5 | +async function postToDiscord() { |
| 6 | + try { |
| 7 | + const response = await fetch(SCOREBOARD_API); |
| 8 | + const data = await response.json(); |
| 9 | + |
| 10 | + // dataを構造体に変換 |
| 11 | + const formattedData = data.rows.map((row) => { |
| 12 | + const columns = data.columns; |
| 13 | + const obj = {}; |
| 14 | + columns.forEach((column, index) => { |
| 15 | + obj[column] = row.values[index]; |
| 16 | + }); |
| 17 | + return obj; |
| 18 | + }); |
| 19 | + |
| 20 | + // dataを#順にソート |
| 21 | + formattedData.sort((a, b) => a["#"] - b["#"]); |
| 22 | + |
| 23 | + // 上位10チームのみ抽出 |
| 24 | + const top = formattedData.filter((row) => row["#"] <= 10).slice(0, 10); |
| 25 | + |
| 26 | + // top10に自チームが含まれているか確認 |
| 27 | + const isInTop10 = top.some((row) => row.team === TEAM_NAME); |
| 28 | + |
| 29 | + // メッセージ作成、自分のチームがあれば強調 |
| 30 | + let message = ` |
| 31 | +## Scoreboard |
| 32 | +time: ${new Date().toLocaleString("ja-JP", { timeZone: "Asia/Tokyo" })} |
| 33 | +
|
| 34 | +=== Top 10 === |
| 35 | + `.trim(); |
| 36 | + message += "\n"; |
| 37 | + message += top |
| 38 | + .map((row) => { |
| 39 | + const rank = row["#"]; |
| 40 | + const team = row.team; |
| 41 | + const isUs = row.team === TEAM_NAME; |
| 42 | + return `${isUs ? "**" : ""}${rank}\\. ${team}${isUs ? "**" : ""}`; |
| 43 | + }) |
| 44 | + .join("\n"); |
| 45 | + |
| 46 | + // 自分のチームが上位10に含まれていない場合はメッセージに追加 |
| 47 | + if (!isInTop10) { |
| 48 | + const rank = formattedData.find((row) => row.team === TEAM_NAME)["#"]; |
| 49 | + message += `\n~~~~~~~~~~~~\n**${rank}\\. ${TEAM_NAME}**`; |
| 50 | + } |
| 51 | + |
| 52 | + console.log(message); |
| 53 | + |
| 54 | + const res = await fetch(DISCORD_WEBHOOK_URL, { |
| 55 | + method: "POST", |
| 56 | + headers: { |
| 57 | + "Content-Type": "application/json", |
| 58 | + }, |
| 59 | + body: JSON.stringify({ content: message }), |
| 60 | + }); |
| 61 | + |
| 62 | + if (!res.ok) { |
| 63 | + console.error(res); |
| 64 | + throw new Error(`Failed to post to Discord: ${res.statusText}`); |
| 65 | + } |
| 66 | + |
| 67 | + console.log("Posted to Discord successfully."); |
| 68 | + } catch (error) { |
| 69 | + console.error("Error posting to Discord:", error); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +postToDiscord(); |
| 74 | + |
| 75 | +### Scoreboard |
| 76 | +time: 2024/6/28 20:21:22 |
| 77 | + |
| 78 | +=== Top 10 === |
| 79 | +1. The Old Man and the C |
| 80 | +1. Hollow Purple |
| 81 | +1. High Potential |
| 82 | +1. sjoerd_visscher |
| 83 | +1. negainoido |
| 84 | +1. udfew |
| 85 | +1. D Yang |
| 86 | +1. undercut |
| 87 | +1. Cup<T> |
| 88 | +1. Cult of the Bound Variable |
| 89 | +... |
| 90 | +**1. Maximum** |
0 commit comments