add motd system

This commit is contained in:
1e99 2025-07-22 19:43:08 +02:00
parent 7fcbabab18
commit 39b0e61796
4 changed files with 113 additions and 9 deletions

45
src/commands/motd.js Normal file
View file

@ -0,0 +1,45 @@
import { Command } from "./commands.js"
export default class MOTDCommand extends Command {
#chat
#stmt
constructor(chat, db) {
super("motd")
this.#chat = chat
this.#stmt = db.prepare(`
UPDATE player
SET
motd = ?
WHERE
username = ?;
`)
}
run(username, args) {
if (args.length !== 1) {
this.#chat.chat("Invalid usage. Please use motd <on/off>")
return
}
let state
switch (args[0]) {
case "on":
state = 1 // SQLite "false"
break
case "off":
state = 0 // SQLite "false"
break
default:
this.#chat.chat("Invalid usage. Please use motd <on/off>")
return
}
this.#stmt.run(state, username)
this.#chat.chat("Updated your MOTD preference")
}
}

View file

@ -20,6 +20,8 @@ import SeenCommand from "./commands/seen.js"
import TPSCommand from "./commands/tps.js"
import WorstPingCommand from "./commands/worst-ping.js"
import JoinsCommand from "./commands/joins.js"
import MOTD from "./motd.js"
import MOTDCommand from "./commands/motd.js"
class Bot {
@ -83,7 +85,6 @@ class Bot {
this.#chat.addPattern("player-join", new RegExp(`^([a-zA-Z0-9_]+) joined the game$`))
this.#chat.addPattern("player-leave", new RegExp(`^([a-zA-Z0-9_]+) left the game$`))
this.#chat.addListener("chat", this.#onChat.bind(this))
this.#chat.addListener("chat:player-join", this.#onChatPlayerJoin.bind(this))
this.#death = new Death(this.#client)
this.#death.addListener("death", this.#onDeath.bind(this))
@ -100,12 +101,14 @@ class Bot {
this.#commands.register(new InfoCommand(this.#chat))
this.#commands.register(new JoinDateCommand(this.#chat, this.#db, this.#dateFormat))
this.#commands.register(new JoinsCommand(this.#chat, this.#db))
this.#commands.register(new MOTDCommand(this.#chat, this.#db))
this.#commands.register(new PingCommand(this.#chat, this.#players))
this.#commands.register(new PlaytimeCommand(this.#chat, this.#db))
this.#commands.register(new SeenCommand(this.#chat, this.#db, this.#dateFormat))
this.#commands.register(new TPSCommand(this.#chat, this.#tps))
this.#commands.register(new WorstPingCommand(this.#chat, this.#players))
new MOTD(this.#client, this.#chat, this.#db)
new PlayerTracker(this.#client, this.#players, this.#db)
}
@ -113,14 +116,6 @@ class Bot {
console.log(`Chat: ${message}`)
}
#onChatPlayerJoin(username) {
if (username === this.#client.username) {
return
}
this.#chat.chat(`/w ${username} Hello ${username}! I'm a bot that adds chat based commands. Please use ${this.#prefix}help to get a list of all commands. Made by 1e99 :)`)
}
#onDeath() {
console.log("Client died")
this.#death.respawn()

View file

@ -7,6 +7,14 @@ const migrations = [
last_online INTEGER,
playtime INTEGER
);`,
`
ALTER TABLE player ADD COLUMN motd INTEGER DEFAULT 1;
CREATE TABLE motd (
id INTEGER PRIMARY KEY,
message TEXT,
expires_at INTEGER
);
`,
]
export default function migrateDB(db) {

56
src/motd.js Normal file
View file

@ -0,0 +1,56 @@
export default class MOTD {
#client
#chat
#enabledStmt
#msgStmt
constructor(client, chat, db) {
this.#client = client
this.#chat = chat
this.#enabledStmt = db.prepare(`
SELECT p.motd
FROM player p
WHERE
p.username = ?;
`)
this.#msgStmt = db.prepare(`
SELECT m.message
FROM motd m
WHERE
m.expires_at > ?
ORDER BY RANDOM()
LIMIT 1;
`)
this.#chat.addListener("chat:player-join", this.#onJoin.bind(this))
}
#onJoin(username) {
if (username === this.#client.username) {
return
}
let result = this.#enabledStmt.get(username)
// We get the chat message before we get the player_info packet
if (result !== undefined) {
// SQLite false
if (result["motd"] === 0) {
return
}
}
result = this.#msgStmt.get(Date.now())
if (result === undefined) {
return
}
let msg = result["message"]
msg = msg
.replaceAll("{username}", username)
this.#chat.chat(`/w ${username} ${msg}`)
}
}