-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is exposed over a server so that my Mac can contact the bot to get it to run commands that I vocalize.
- Loading branch information
1 parent
180e746
commit ffce71f
Showing
7 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Reason why this exists | ||
|
||
For the code jam, I wanted the bot to be able to type commands in chat based on what I _say_, e.g. saying "check out the keyboard command" would type `!keyboard` in chat. In order for this to work, I need my main computer to have a way of interfacing with the bot. The web interface _seems_ like it would be the right approach, but we would still need a communication route between the web interface and the bot. That means that the bot needs its own server eventually, which is what this code is! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { serve } from "@hono/node-server" | ||
import dotenvFlow from "dotenv-flow" | ||
import { Hono, type Context } from "hono" | ||
import type { BlankInput, Env } from "hono/types" | ||
|
||
import type { Bot } from "../twitch/Bot.js" | ||
|
||
dotenvFlow.config() | ||
|
||
const serverPassword = process.env.SERVER_PASSWORD | ||
const serverPort = process.env.SERVER_PORT | ||
|
||
interface FuzzyCommandRequestBody { | ||
query: string | ||
password: string | ||
} | ||
|
||
async function handleRunFuzzyCommand( | ||
bot: Bot, | ||
c: Context<Env, "/run-fuzzy-command", BlankInput>, | ||
) { | ||
const { password, query } = await c.req.json<FuzzyCommandRequestBody>() | ||
|
||
if (password !== serverPassword) { | ||
return c.text("Invalid password.", 403) | ||
} | ||
|
||
await bot.emitFuzzyCommand(query) | ||
|
||
return c.text("Successfully emitted a command.") | ||
} | ||
|
||
function startServer(bot: Bot) { | ||
const app = new Hono() | ||
|
||
app.post("/run-fuzzy-command", (c) => handleRunFuzzyCommand(bot, c)) | ||
|
||
const port = Number.parseInt(serverPort as string, 10) | ||
console.log(`Server is running on port ${port}`) | ||
|
||
serve({ | ||
fetch: app.fetch, | ||
hostname: "0.0.0.0", | ||
port, | ||
}) | ||
} | ||
|
||
export function init(bot: Bot) { | ||
if (serverPassword === undefined || serverPort === undefined) { | ||
throw new Error( | ||
"Missing environment variables. Make sure to copy .env.example to .env and fill out the values.", | ||
) | ||
} | ||
|
||
startServer(bot) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.