Compare commits
57
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d64a532a81 | ||
|
|
136377d81a | ||
|
|
0218676e2d | ||
|
|
e333f084b5 | ||
|
|
f285367511 | ||
|
|
e10e1f7a86 | ||
|
|
177c820821 | ||
|
|
e453a9ba94 | ||
|
|
95072fcd53 | ||
|
|
3f87e68a2c | ||
|
|
d294b3463b | ||
|
|
ffb8b963fa | ||
|
|
b1ea5f17e3 | ||
|
|
23d417ef27 | ||
|
|
52dfe859ee | ||
|
|
da66eb8dd5 | ||
|
|
fcc7c9c66a | ||
|
|
7a17c55774 | ||
|
|
39962457b3 | ||
|
|
539c0a69af | ||
|
|
d6687879dd | ||
|
|
e507225cae | ||
|
|
c7b960e14a | ||
|
|
691b505175 | ||
|
|
95ba434194 | ||
|
|
5f4e893971 | ||
|
|
01dbb98450 | ||
|
|
5024af2789 | ||
|
|
483dc666d1 | ||
|
|
3585fa0658 | ||
|
|
d1be60e201 | ||
|
|
26e1d9b094 | ||
|
|
461f8435c0 | ||
|
|
1b47bd13a3 | ||
|
|
035e383db2 | ||
|
|
2b14079e50 | ||
|
|
a92c4131db | ||
|
|
397a8a459a | ||
|
|
4080ce8aec | ||
|
|
e6aa5def96 | ||
|
|
6410619662 | ||
|
|
df656ca12d | ||
|
|
076e616b9c | ||
|
|
891f3f6ad2 | ||
|
|
ded31409e2 | ||
|
|
ae3d574c68 | ||
|
|
d7edfea45e | ||
|
|
db5d23c2ac | ||
|
|
a85ac23a6b | ||
|
|
d177114ddb | ||
|
|
10c2efef7d | ||
|
|
e33eea6d24 | ||
|
|
76aeca3de6 | ||
|
|
c4dd7302bc | ||
|
|
0b36bc9e93 | ||
|
|
dee5008c60 | ||
|
|
a664ae15fd |
No files matched your search
@@ -0,0 +1,7 @@
|
|||||||
|
.git
|
||||||
|
.gitignore
|
||||||
|
.env
|
||||||
|
*.md
|
||||||
|
Dockerfile
|
||||||
|
docker-compose*.yml
|
||||||
|
.dockerignore
|
||||||
+8
-1
@@ -1,3 +1,10 @@
|
|||||||
.env
|
*.env
|
||||||
.cursor
|
.cursor
|
||||||
tmp/
|
tmp/
|
||||||
|
|
||||||
|
# Local /tldr benchmark: chat dumps and results never get committed.
|
||||||
|
bench-out/
|
||||||
|
internal/tldr/tldrbench/private/
|
||||||
|
|
||||||
|
# Real chat dumps used by the benchmark never get committed.
|
||||||
|
internal/tldr/tldrbench/testdata/private/
|
||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
# Build stage
|
||||||
|
FROM golang:1.26.1-alpine AS builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux go build -o /velox-bot .
|
||||||
|
|
||||||
|
# Run stage
|
||||||
|
FROM alpine:3.19
|
||||||
|
|
||||||
|
RUN apk add --no-cache ca-certificates tzdata
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY --from=builder /velox-bot .
|
||||||
|
|
||||||
|
ENTRYPOINT ["./velox-bot"]
|
||||||
@@ -0,0 +1,149 @@
|
|||||||
|
# Velox Bot
|
||||||
|
|
||||||
|
A Discord bot written in Go with a PostgreSQL backing store.
|
||||||
|
|
||||||
|
It currently includes:
|
||||||
|
|
||||||
|
- Fun commands (`/ping`, `/joke`, `/dice`, `/rps`, …)
|
||||||
|
- Leveling system (opt-in per server via `/slvl toggle`)
|
||||||
|
- Meeting rooms (voice lobby → auto-created temporary voice channels)
|
||||||
|
- Scheduling (1:1 sessions with DM invites + reaction-based accept/decline/reschedule)
|
||||||
|
- Projects (list/create projects + helpers)
|
||||||
|
- Support tickets (a panel with categories, a private thread per ticket, claim/close, transcripts to a log channel)
|
||||||
|
- Anti-raid and anti-nuke (Join Gate, raid mode, quarantine of anyone who mass-deletes or mass-bans, lockdowns), modelled on Wick
|
||||||
|
- Music (Lavalink-powered `/play`, `/queue`, `/volume`)
|
||||||
|
- Twitch live notifications (per-guild `/config` setup)
|
||||||
|
- Welcome messages, DMs, and default role assignment (per-guild `/config` setup)
|
||||||
|
- Focused moderation tools (`/moderation purge|timeout|untimeout|kick|ban|unban|slowmode`)
|
||||||
|
- Server logging system (edits/deletes/joins/leaves/roles/ban/kick/mod actions)
|
||||||
|
|
||||||
|
When the bot is added to a new server for the first time, toggleable systems are defaulted to **disabled** and a setup DM is sent to the inviter (or server owner fallback) with enable instructions.
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- Go (see `go.mod`)
|
||||||
|
- PostgreSQL 16+ (or use the provided `docker-compose.yml`)
|
||||||
|
- (Optional) [Air](https://github.com/air-verse/air) for live reload during development
|
||||||
|
- (Optional) Lavalink if you want music commands
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
The app loads environment variables from `.env` (see `internal/config/config.go`).
|
||||||
|
|
||||||
|
Required:
|
||||||
|
|
||||||
|
- `BOT_TOKEN`: Discord bot token
|
||||||
|
- `APP_ID`: Discord application ID
|
||||||
|
- `GUILD_ID`: Target guild/server ID (commands are registered per-guild)
|
||||||
|
- `DB_HOST`: Postgres connection string (pgx), e.g. `postgres://velox:velox_pwd@localhost:5432/velox?sslmode=disable`
|
||||||
|
|
||||||
|
Optional (music):
|
||||||
|
|
||||||
|
- `LAVALINK_HOST`: Lavalink WebSocket/HTTP host (depends on your Lavalink setup)
|
||||||
|
- `LAVALINK_PASSWORD`: Lavalink password
|
||||||
|
|
||||||
|
Optional (Twitch live notifications):
|
||||||
|
|
||||||
|
- `TWITCH_CLIENT_ID`: Twitch Client ID used to query live status
|
||||||
|
|
||||||
|
## Database setup
|
||||||
|
|
||||||
|
If you use the compose file:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
Then apply the schema:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
psql "postgres://velox:velox_pwd@localhost:5432/velox?sslmode=disable" -f schema.sql
|
||||||
|
```
|
||||||
|
|
||||||
|
## Run
|
||||||
|
|
||||||
|
Plain Go:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go run .
|
||||||
|
```
|
||||||
|
|
||||||
|
With live reload (Air):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
air
|
||||||
|
```
|
||||||
|
|
||||||
|
If you use `just`, a small workflow is included:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
just up # start postgres
|
||||||
|
just start # start postgres + run air
|
||||||
|
just down # stop postgres
|
||||||
|
```
|
||||||
|
|
||||||
|
## Commands (high-level)
|
||||||
|
|
||||||
|
- **Fun**
|
||||||
|
- `/ping`, `/joke [type]`, `/coinflip`, `/dice <expr>`
|
||||||
|
- `/rps <hand>`, `/rpsstats`, `/rpsleaderboard` (server only)
|
||||||
|
- **Help**
|
||||||
|
- `/help` (sends a DM with command reference)
|
||||||
|
- **Leveling**
|
||||||
|
- `/slvl ...` admin/config
|
||||||
|
- `/rank`, `/leaderboard`, `/rewards`
|
||||||
|
- **Meetings** (**Manage Server**)
|
||||||
|
- `/meeting set-lobby`, `/meeting lock`, `/meeting lock-private`, `/meeting unlock`, `/meeting invite`, `/meeting uninvite`
|
||||||
|
- **Timezone**
|
||||||
|
- `/timezone set`, `/timezone show`
|
||||||
|
- **Scheduling**
|
||||||
|
- `/schedule create`, `/schedule list`, `/schedule reschedule`
|
||||||
|
- **Projects**
|
||||||
|
- `/projects list`, `/projects create`, `/projects add-helper`
|
||||||
|
- **Tickets**
|
||||||
|
- `/tickets setup channel:#tickets [log-channel] [staff-role]` — Where tickets open and closed ones are logged (**Manage Server**)
|
||||||
|
- `/tickets category add|remove|list` — One entry in the panel's dropdown per category, each with its own staff role (**Manage Server**)
|
||||||
|
- `/tickets panel` — Post or refresh the panel (**Manage Server**)
|
||||||
|
- `/ticket add`, `/ticket close [reason]` — Opener or staff; `/ticket claim`, `/ticket remove` — staff
|
||||||
|
- The bot needs View Channel, Send Messages, Embed Links, Create Private Threads, Send Messages in Threads and Manage Threads in the ticket channel, and Attach Files in the log channel. Give the staff role Manage Threads there too, so staff see every ticket.
|
||||||
|
- **Security** (anti-raid and anti-nuke; everything is off until turned on)
|
||||||
|
- `/security status`, `/security toggle feature on`, `/security alerts channel [role]`
|
||||||
|
- `/security trust add|remove user|role` — owner only; trusted members are exempt from anti-nuke and may change security settings
|
||||||
|
- `/security release user`, `/security quarantined`, `/security end-raid`, `/security quarantine-role`
|
||||||
|
- `/lockdown channel|server|joins`, `/unlock channel|server|joins` — unlocking needs the owner or a trusted member
|
||||||
|
- `/security snapshot`, `/security restore` — snapshots of roles and channels are also taken every 6 hours while anti-nuke is on (last three kept); restore recreates deleted roles and channels and reverts dangerous permission changes, and can run automatically after a stopped nuke
|
||||||
|
- Heat (anti-spam): messages build a per-member score that cools over time; spam, repeats, mass mentions and invite links max it, which deletes the message and times the member out (doubling for repeat offences). A wave of spammers turns on heat panic (newcomers timed out on sight for 10 minutes); a mention flood can lock the server. Moderators (Manage Messages) and trusted members are exempt.
|
||||||
|
- Raid mode survives a bot restart.
|
||||||
|
- Critical alerts (raids, stopped nukes, restores, spam waves, removed bots) are also DMed to the owner and trusted users (up to 10; trusted roles aren't expanded), since the alert channel may be the first thing a nuker deletes. One setting turns this off.
|
||||||
|
- Join Gate filters, raid thresholds, anti-nuke limits and heat settings are set on the dashboard.
|
||||||
|
- The bot needs View Audit Log, Manage Roles, Manage Channels, Manage Server (to pause invites), Moderate Members, Kick Members and Ban Members, and its role must be above the roles it protects against: it can only quarantine members whose highest role is below its own.
|
||||||
|
- Turning things off, unlocking and releasing always need the owner or someone on the trusted list, whatever Discord's command permissions say.
|
||||||
|
- **Music** (requires **DJ** role)
|
||||||
|
- `/play`, `/queue`, `/volume`
|
||||||
|
- **Moderation** (focused + practical)
|
||||||
|
- `/moderation purge amount:<1-100>` — Fast recent message cleanup (**Manage Messages**)
|
||||||
|
- `/moderation timeout user:@someone duration:<10m|1h|...> [reason]` — Temporary member timeout (**Moderate Members**)
|
||||||
|
- `/moderation kick user:@someone [reason]` — Kick member (**Kick Members**)
|
||||||
|
- `/moderation ban user:@someone [delete_days] [reason]` — Ban member (**Ban Members**)
|
||||||
|
- `/moderation unban user_id:<id> [reason]` — Unban by user ID (**Ban Members**)
|
||||||
|
- `/moderation untimeout user:@someone [reason]` — Remove timeout (**Moderate Members**)
|
||||||
|
- `/moderation slowmode channel:#channel seconds:<0-21600>` — Control channel flood (**Manage Channels**)
|
||||||
|
- **Twitch live notifications** (**Manage Server**)
|
||||||
|
- `/config addstreamer username:<twitch-name>` — Add a streamer for notifications
|
||||||
|
- `/config removestreamer username:<twitch-name>` — Remove a streamer
|
||||||
|
- `/config settwitchnotificationchannel channel:#channel` — Channel for live pings
|
||||||
|
- **Welcome / onboarding** (**Manage Server**)
|
||||||
|
- `/config setwelcomechannel channel:#channel` — Channel for welcome messages
|
||||||
|
- `/config setwelcomemessage message:"..."` — Server welcome template (supports `{user}`, `{server}`)
|
||||||
|
- `/config setwelcomedm message:"..."` — DM welcome template (supports `{user}`, `{server}`)
|
||||||
|
- `/config setwelcomegif url:<image-url>` — GIF/image used in welcome embed
|
||||||
|
- `/config setdefaultrole role:@role` — Role automatically granted on join
|
||||||
|
- **Logging** (**Manage Server**)
|
||||||
|
- `/config setlogchannel channel:#channel` — Set where logs are sent
|
||||||
|
- `/config enablelogging` — Enable event/moderation logs
|
||||||
|
- `/config disablelogging` — Disable event/moderation logs
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
- Slash commands are registered to the configured `GUILD_ID` on startup.
|
||||||
|
- Some features require server permissions (e.g. **Manage Server**) or roles (e.g. **DJ** for music).
|
||||||
+15
-2
@@ -1,5 +1,3 @@
|
|||||||
version: "3.9"
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
db:
|
db:
|
||||||
image: postgres:16
|
image: postgres:16
|
||||||
@@ -19,5 +17,20 @@ services:
|
|||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 5
|
retries: 5
|
||||||
|
|
||||||
|
bot:
|
||||||
|
build: .
|
||||||
|
container_name: velox-bot
|
||||||
|
restart: unless-stopped
|
||||||
|
env_file: .env
|
||||||
|
environment:
|
||||||
|
DB_HOST: ${DB_HOST:-postgres://velox:velox_pwd@db:5432/velox?sslmode=disable}
|
||||||
|
LAVALINK_HOST: ${LAVALINK_HOST:-ws://lavalink:2333}
|
||||||
|
LAVALINK_PASSWORD: ${LAVALINK_PASSWORD:-youshallnotpass}
|
||||||
|
depends_on:
|
||||||
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
networks:
|
||||||
|
- default
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
velox_pgdata:
|
velox_pgdata:
|
||||||
@@ -4,6 +4,8 @@ go 1.26.1
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bwmarrin/discordgo v0.29.0
|
github.com/bwmarrin/discordgo v0.29.0
|
||||||
|
github.com/disgoorg/disgolink/v3 v3.1.0
|
||||||
|
github.com/disgoorg/snowflake/v2 v2.0.3
|
||||||
github.com/fogleman/gg v1.3.0
|
github.com/fogleman/gg v1.3.0
|
||||||
github.com/jackc/pgx/v5 v5.8.0
|
github.com/jackc/pgx/v5 v5.8.0
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
@@ -11,8 +13,10 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/disgoorg/json v1.2.0 // indirect
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||||
github.com/gorilla/websocket v1.4.2 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
github.com/gorilla/websocket v1.5.3 // indirect
|
||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||||
|
|||||||
@@ -3,12 +3,21 @@ github.com/bwmarrin/discordgo v0.29.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/disgoorg/disgolink/v3 v3.1.0 h1:IwhycUfvw87VbvvNSwBcdGMQNSFvNQ7/AQofmbp1NS8=
|
||||||
|
github.com/disgoorg/disgolink/v3 v3.1.0/go.mod h1:UjHfrC4NT4vzibG3GyqtY5l3aMzFwfkU+B3RiW3AQQ8=
|
||||||
|
github.com/disgoorg/json v1.2.0 h1:6e/j4BCfSHIvucG1cd7tJPAOp1RgnnMFSqkvZUtEd1Y=
|
||||||
|
github.com/disgoorg/json v1.2.0/go.mod h1:BHDwdde0rpQFDVsRLKhma6Y7fTbQKub/zdGO5O9NqqA=
|
||||||
|
github.com/disgoorg/snowflake/v2 v2.0.3 h1:3B+PpFjr7j4ad7oeJu4RlQ+nYOTadsKapJIzgvSI2Ro=
|
||||||
|
github.com/disgoorg/snowflake/v2 v2.0.3/go.mod h1:W6r7NUA7DwfZLwr00km6G4UnZ0zcoLBRufhkFWgAc4c=
|
||||||
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
|
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
|
||||||
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
|
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
||||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||||
|
|||||||
+140
-1
@@ -1,10 +1,16 @@
|
|||||||
package bot
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
"velox-bot/internal/commands"
|
"velox-bot/internal/commands"
|
||||||
|
"velox-bot/internal/db/repos/musicrepo"
|
||||||
"velox-bot/internal/db/services"
|
"velox-bot/internal/db/services"
|
||||||
"velox-bot/internal/events"
|
"velox-bot/internal/events"
|
||||||
|
"velox-bot/internal/music"
|
||||||
|
"velox-bot/internal/tickets"
|
||||||
|
"velox-bot/internal/security"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
@@ -16,20 +22,41 @@ type Bot struct {
|
|||||||
Commands []*discordgo.ApplicationCommand
|
Commands []*discordgo.ApplicationCommand
|
||||||
registeredCommands []*discordgo.ApplicationCommand
|
registeredCommands []*discordgo.ApplicationCommand
|
||||||
Services *services.Services
|
Services *services.Services
|
||||||
|
MusicRepo *musicrepo.Repo
|
||||||
|
DBHost string
|
||||||
|
LavalinkHost string
|
||||||
|
LavalinkPass string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBot(token, appID, guildID string, cmds []*discordgo.ApplicationCommand, services *services.Services) (*Bot, error) {
|
func NewBot(token, appID, guildID, lavalinkHost, lavalinkPass string, cmds []*discordgo.ApplicationCommand, services *services.Services, musicRepo *musicrepo.Repo, dbHost string) (*Bot, error) {
|
||||||
session, err := discordgo.New("Bot " + token)
|
session, err := discordgo.New("Bot " + token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Intents needed for:
|
||||||
|
// - InteractionCreate (slash commands): Guilds
|
||||||
|
// - MessageCreate (leveling): GuildMessages
|
||||||
|
// - GuildMemberAdd (welcome messages): GuildMembers
|
||||||
|
// - VoiceStateUpdate (meeting lobby): GuildVoiceStates
|
||||||
|
session.Identify.Intents = discordgo.IntentsGuilds |
|
||||||
|
discordgo.IntentsGuildBans |
|
||||||
|
discordgo.IntentsGuildMembers |
|
||||||
|
discordgo.IntentsGuildMessages |
|
||||||
|
discordgo.IntentsMessageContent |
|
||||||
|
discordgo.IntentsGuildVoiceStates |
|
||||||
|
discordgo.IntentsDirectMessages
|
||||||
|
|
||||||
return &Bot{
|
return &Bot{
|
||||||
Session: session,
|
Session: session,
|
||||||
AppID: appID,
|
AppID: appID,
|
||||||
GuildID: guildID,
|
GuildID: guildID,
|
||||||
Commands: cmds,
|
Commands: cmds,
|
||||||
Services: services,
|
Services: services,
|
||||||
|
MusicRepo: musicRepo,
|
||||||
|
DBHost: dbHost,
|
||||||
|
LavalinkHost: lavalinkHost,
|
||||||
|
LavalinkPass: lavalinkPass,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,6 +65,44 @@ func (b *Bot) Start() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_ = music.Init(b.Session, b.MusicRepo, b.AppID, b.LavalinkHost, b.LavalinkPass)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := music.StartCommandListener(context.Background(), b.DBHost, b.MusicRepo); err != nil {
|
||||||
|
log.Printf("music: command listener stopped: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if st := tickets.Default(); st != nil {
|
||||||
|
go func() {
|
||||||
|
// The connection can drop (database restart, network blip):
|
||||||
|
// reconnect instead of silently ignoring the dashboard from then on.
|
||||||
|
for {
|
||||||
|
err := tickets.StartCommandListener(context.Background(), b.DBHost, b.Session, st)
|
||||||
|
log.Printf("tickets: command listener stopped: %v (retrying in 10s)", err)
|
||||||
|
time.Sleep(10 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
if eng := security.Start(b.Session, b.Session.State.User.ID); eng != nil {
|
||||||
|
// Moderators are exempt from heat; their permissions come from the
|
||||||
|
// gateway cache so a message never costs an API call.
|
||||||
|
eng.ChannelPerms = b.Session.State.UserChannelPermissions
|
||||||
|
eng.ResumeRaids(context.Background())
|
||||||
|
go eng.WatchRaids(context.Background(), 30*time.Second)
|
||||||
|
go eng.WatchSnapshots(context.Background())
|
||||||
|
go func() {
|
||||||
|
// Reconnect if the database connection drops, instead of
|
||||||
|
// silently ignoring the dashboard from then on.
|
||||||
|
for {
|
||||||
|
err := eng.StartCommandListener(context.Background(), b.DBHost)
|
||||||
|
log.Printf("security: command listener stopped: %v (retrying in 10s)", err)
|
||||||
|
time.Sleep(10 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
b.registeredCommands = make([]*discordgo.ApplicationCommand, len(b.Commands))
|
b.registeredCommands = make([]*discordgo.ApplicationCommand, len(b.Commands))
|
||||||
for _, cmd := range b.Commands {
|
for _, cmd := range b.Commands {
|
||||||
created, err := b.Session.ApplicationCommandCreate(b.AppID, b.GuildID, cmd)
|
created, err := b.Session.ApplicationCommandCreate(b.AppID, b.GuildID, cmd)
|
||||||
@@ -51,9 +116,83 @@ func (b *Bot) Start() error {
|
|||||||
b.Session.AddHandler(commands.HandleInteraction)
|
b.Session.AddHandler(commands.HandleInteraction)
|
||||||
|
|
||||||
b.Session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
b.Session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
if eng := security.Default(); eng != nil {
|
||||||
|
eng.OnMessage(context.Background(), m)
|
||||||
|
}
|
||||||
events.HandleMessageCreate(s, m, b.Services)
|
events.HandleMessageCreate(s, m, b.Services)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
b.Session.AddHandler(func(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
|
||||||
|
events.HandleBotAddedOnGuild(s, m, b.Services)
|
||||||
|
|
||||||
|
// Security runs first: someone it removes (join lock, Join Gate,
|
||||||
|
// raid) gets no welcome message or default role.
|
||||||
|
if eng := security.Default(); eng != nil && m != nil && m.Member != nil {
|
||||||
|
if eng.OnMemberJoin(context.Background(), m.GuildID, m.Member) {
|
||||||
|
events.HandleGuildMemberAddLog(s, m, b.Services)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore bot members for welcome/log onboarding of normal users.
|
||||||
|
if m != nil && m.Member != nil && m.User != nil && m.User.Bot {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
events.HandleGuildMemberAdd(s, m, b.Services)
|
||||||
|
events.HandleGuildMemberAddLog(s, m, b.Services)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Anti-nuke and unauthorized bot additions read the audit log as it
|
||||||
|
// happens (needs the Guild Moderation intent and View Audit Log).
|
||||||
|
b.Session.AddHandler(func(s *discordgo.Session, ev *discordgo.GuildAuditLogEntryCreate) {
|
||||||
|
if eng := security.Default(); eng != nil && ev.AuditLogEntry != nil {
|
||||||
|
eng.OnAuditLogEntry(context.Background(), ev.GuildID, ev.AuditLogEntry)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Session.AddHandler(func(s *discordgo.Session, m *discordgo.GuildMemberRemove) {
|
||||||
|
events.HandleGuildMemberRemoveLog(s, m, b.Services)
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Session.AddHandler(func(s *discordgo.Session, m *discordgo.GuildMemberUpdate) {
|
||||||
|
events.HandleGuildMemberUpdateLog(s, m, b.Services)
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageUpdate) {
|
||||||
|
events.HandleMessageUpdateLog(s, m, b.Services)
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageDelete) {
|
||||||
|
events.HandleMessageDeleteLog(s, m, b.Services)
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Session.AddHandler(func(s *discordgo.Session, m *discordgo.GuildBanAdd) {
|
||||||
|
events.HandleGuildBanAddLog(s, m, b.Services)
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Session.AddHandler(func(s *discordgo.Session, m *discordgo.GuildBanRemove) {
|
||||||
|
events.HandleGuildBanRemoveLog(s, m, b.Services)
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Session.AddHandler(func(s *discordgo.Session, t *discordgo.ThreadDelete) {
|
||||||
|
if st := tickets.Default(); st != nil && t.Channel != nil {
|
||||||
|
tickets.ThreadDeleted(context.Background(), st, t.ID)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Session.AddHandler(func(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) {
|
||||||
|
events.HandleVoiceStateUpdate(s, vs, b.Services)
|
||||||
|
music.OnVoiceStateUpdate(vs)
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Session.AddHandler(func(s *discordgo.Session, ev *discordgo.VoiceServerUpdate) {
|
||||||
|
music.OnVoiceServerUpdate(ev)
|
||||||
|
})
|
||||||
|
|
||||||
|
events.StartScheduleReminderLoop(b.Session, b.Services)
|
||||||
|
events.StartTwitchLiveLoop(b.Session, b.Services)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,33 +1,114 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
"velox-bot/internal/commands/config"
|
||||||
"velox-bot/internal/commands/fun"
|
"velox-bot/internal/commands/fun"
|
||||||
"velox-bot/internal/commands/help"
|
"velox-bot/internal/commands/help"
|
||||||
cmdlevel "velox-bot/internal/commands/level"
|
cmdlevel "velox-bot/internal/commands/level"
|
||||||
|
"velox-bot/internal/commands/meeting"
|
||||||
|
"velox-bot/internal/commands/moderation"
|
||||||
|
cmdmusic "velox-bot/internal/commands/music"
|
||||||
|
"velox-bot/internal/commands/projects"
|
||||||
|
"velox-bot/internal/commands/schedule"
|
||||||
|
cmdtickets "velox-bot/internal/commands/tickets"
|
||||||
|
cmdsecurity "velox-bot/internal/commands/security"
|
||||||
|
"velox-bot/internal/commands/timezone"
|
||||||
|
"velox-bot/internal/music"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var AllCommands = []*discordgo.ApplicationCommand{
|
var AllCommands = []*discordgo.ApplicationCommand{
|
||||||
fun.Ping,
|
fun.Ping,
|
||||||
|
fun.Joke,
|
||||||
|
fun.Coinflip,
|
||||||
|
fun.Dice,
|
||||||
|
fun.Rps,
|
||||||
|
fun.RpsStats,
|
||||||
|
fun.RpsLeaderboard,
|
||||||
help.Help,
|
help.Help,
|
||||||
cmdlevel.Slvl,
|
cmdlevel.Slvl,
|
||||||
cmdlevel.Rank,
|
cmdlevel.Rank,
|
||||||
cmdlevel.Leaderboard,
|
cmdlevel.Leaderboard,
|
||||||
cmdlevel.Rewards,
|
cmdlevel.Rewards,
|
||||||
|
cmdmusic.Play,
|
||||||
|
cmdmusic.Queue,
|
||||||
|
cmdmusic.Volume,
|
||||||
|
cmdmusic.Music,
|
||||||
|
meeting.Meeting,
|
||||||
|
moderation.Moderation,
|
||||||
|
timezone.Timezone,
|
||||||
|
schedule.Schedule,
|
||||||
|
projects.Projects,
|
||||||
|
config.Config,
|
||||||
|
cmdtickets.Tickets,
|
||||||
|
cmdtickets.Ticket,
|
||||||
|
cmdsecurity.Security,
|
||||||
|
cmdsecurity.Lockdown,
|
||||||
|
cmdsecurity.Unlock,
|
||||||
}
|
}
|
||||||
|
|
||||||
var handlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
var handlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
||||||
"ping": fun.PingHandler,
|
"ping": fun.PingHandler,
|
||||||
|
"joke": fun.JokeHandler,
|
||||||
|
"coinflip": fun.CoinflipHandler,
|
||||||
|
"dice": fun.DiceHandler,
|
||||||
|
"rps": fun.RpsHandler,
|
||||||
|
"rpsstats": fun.RpsStatsHandler,
|
||||||
|
"rpsleaderboard": fun.RpsLeaderboardHandler,
|
||||||
"help": help.HelpHandler,
|
"help": help.HelpHandler,
|
||||||
"slvl": cmdlevel.SlvlHandler,
|
"slvl": cmdlevel.SlvlHandler,
|
||||||
"rank": cmdlevel.RankHandler,
|
"rank": cmdlevel.RankHandler,
|
||||||
"leaderboard": cmdlevel.LeaderboardHandler,
|
"leaderboard": cmdlevel.LeaderboardHandler,
|
||||||
"rewards": cmdlevel.RewardsHandler,
|
"rewards": cmdlevel.RewardsHandler,
|
||||||
|
"meeting": meeting.MeetingHandler,
|
||||||
|
"moderation": moderation.ModerationHandler,
|
||||||
|
"timezone": timezone.TimezoneHandler,
|
||||||
|
"schedule": schedule.ScheduleHandler,
|
||||||
|
"projects": projects.ProjectsHandler,
|
||||||
|
"play": cmdmusic.PlayHandler,
|
||||||
|
"queue": cmdmusic.QueueHandler,
|
||||||
|
"volume": cmdmusic.VolumeHandler,
|
||||||
|
"music": cmdmusic.MusicHandler,
|
||||||
|
"config": config.ConfigHandler,
|
||||||
|
"tickets": cmdtickets.TicketsHandler,
|
||||||
|
"ticket": cmdtickets.TicketHandler,
|
||||||
|
"security": cmdsecurity.SecurityHandler,
|
||||||
|
"lockdown": cmdsecurity.LockdownHandler,
|
||||||
|
"unlock": cmdsecurity.UnlockHandler,
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleInteraction(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
func HandleInteraction(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
switch i.Type {
|
||||||
|
case discordgo.InteractionApplicationCommand:
|
||||||
if h, ok := handlers[i.ApplicationCommandData().Name]; ok {
|
if h, ok := handlers[i.ApplicationCommandData().Name]; ok {
|
||||||
h(s, i)
|
h(s, i)
|
||||||
}
|
}
|
||||||
|
case discordgo.InteractionMessageComponent:
|
||||||
|
data := i.MessageComponentData()
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(data.CustomID, "music:"):
|
||||||
|
music.HandleComponent(s, i)
|
||||||
|
case strings.HasPrefix(data.CustomID, "schedule:"):
|
||||||
|
schedule.HandleComponent(s, i)
|
||||||
|
case strings.HasPrefix(data.CustomID, "projects:"):
|
||||||
|
projects.HandleComponent(s, i)
|
||||||
|
case strings.HasPrefix(data.CustomID, "tickets:"):
|
||||||
|
cmdtickets.HandleComponent(s, i)
|
||||||
|
case strings.HasPrefix(data.CustomID, "security:"):
|
||||||
|
cmdsecurity.HandleComponent(s, i)
|
||||||
|
}
|
||||||
|
case discordgo.InteractionModalSubmit:
|
||||||
|
data := i.ModalSubmitData()
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(data.CustomID, "schedule:"):
|
||||||
|
schedule.HandleModalSubmit(s, i)
|
||||||
|
case strings.HasPrefix(data.CustomID, "projects:"):
|
||||||
|
projects.HandleModalSubmit(s, i)
|
||||||
|
case strings.HasPrefix(data.CustomID, "tickets:"):
|
||||||
|
cmdtickets.HandleModalSubmit(s, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,578 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Config = &discordgo.ApplicationCommand{
|
||||||
|
Name: "config",
|
||||||
|
Description: "Server configuration",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "addstreamer",
|
||||||
|
Description: "Add a Twitch streamer for live notifications",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "username",
|
||||||
|
Description: "Twitch username (without https://)",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "removestreamer",
|
||||||
|
Description: "Remove a Twitch streamer from live notifications",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "username",
|
||||||
|
Description: "Twitch username to remove",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "settwitchnotificationchannel",
|
||||||
|
Description: "Set the channel for Twitch live notifications",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionChannel,
|
||||||
|
Name: "channel",
|
||||||
|
Description: "Text channel for notifications",
|
||||||
|
Required: true,
|
||||||
|
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "setwelcomechannel",
|
||||||
|
Description: "Set the channel for welcome messages",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionChannel,
|
||||||
|
Name: "channel",
|
||||||
|
Description: "Text channel for welcomes",
|
||||||
|
Required: true,
|
||||||
|
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "setwelcomemessage",
|
||||||
|
Description: "Set the welcome message template shown in the server",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "message",
|
||||||
|
Description: "Use {user} where the new member mention should appear",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "setwelcomedm",
|
||||||
|
Description: "Set the welcome DM template sent to new members",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "message",
|
||||||
|
Description: "Use {user} where the new member mention should appear",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "setwelcomegif",
|
||||||
|
Description: "Set the GIF/image URL used in welcome messages",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "url",
|
||||||
|
Description: "Direct image or GIF URL",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "setdefaultrole",
|
||||||
|
Description: "Set the default role given to new members",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionRole,
|
||||||
|
Name: "role",
|
||||||
|
Description: "Role to assign on join",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "setlogchannel",
|
||||||
|
Description: "Set the moderation log channel",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionChannel,
|
||||||
|
Name: "channel",
|
||||||
|
Description: "Text channel for logs",
|
||||||
|
Required: true,
|
||||||
|
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "enablelogging",
|
||||||
|
Description: "Enable server logging events",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "disablelogging",
|
||||||
|
Description: "Disable server logging events",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func ConfigHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if i.GuildID == "" {
|
||||||
|
respondEphemeral(s, i, "This command can only be used in a server.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i.Member == nil || (i.Member.Permissions&discordgo.PermissionManageGuild) == 0 {
|
||||||
|
respondEphemeral(s, i, "You need the **Manage Server** permission to use this.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if services.Global == nil {
|
||||||
|
respondEphemeral(s, i, "Configuration services are not available.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
respondEphemeral(s, i, "Missing subcommand.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch data.Options[0].Name {
|
||||||
|
case "addstreamer":
|
||||||
|
log.Printf("twitch: /config addstreamer invoked by %s in guild %s", i.Member.User.ID, i.GuildID)
|
||||||
|
handleAddStreamer(s, i)
|
||||||
|
case "removestreamer":
|
||||||
|
log.Printf("twitch: /config removestreamer invoked by %s in guild %s", i.Member.User.ID, i.GuildID)
|
||||||
|
handleRemoveStreamer(s, i)
|
||||||
|
case "settwitchnotificationchannel":
|
||||||
|
log.Printf("twitch: /config settwitchnotificationchannel invoked by %s in guild %s", i.Member.User.ID, i.GuildID)
|
||||||
|
handleSetChannel(s, i)
|
||||||
|
case "setwelcomechannel":
|
||||||
|
log.Printf("welcome: /config setwelcomechannel invoked by %s in guild %s", i.Member.User.ID, i.GuildID)
|
||||||
|
handleSetWelcomeChannel(s, i)
|
||||||
|
case "setwelcomemessage":
|
||||||
|
log.Printf("welcome: /config setwelcomemessage invoked by %s in guild %s", i.Member.User.ID, i.GuildID)
|
||||||
|
handleSetWelcomeMessage(s, i)
|
||||||
|
case "setwelcomedm":
|
||||||
|
log.Printf("welcome: /config setwelcomedm invoked by %s in guild %s", i.Member.User.ID, i.GuildID)
|
||||||
|
handleSetWelcomeDM(s, i)
|
||||||
|
case "setwelcomegif":
|
||||||
|
log.Printf("welcome: /config setwelcomegif invoked by %s in guild %s", i.Member.User.ID, i.GuildID)
|
||||||
|
handleSetWelcomeGIF(s, i)
|
||||||
|
case "setdefaultrole":
|
||||||
|
log.Printf("defaultrole: /config setdefaultrole invoked by %s in guild %s", i.Member.User.ID, i.GuildID)
|
||||||
|
handleSetDefaultRole(s, i)
|
||||||
|
case "setlogchannel":
|
||||||
|
log.Printf("log: /config setlogchannel invoked by %s in guild %s", i.Member.User.ID, i.GuildID)
|
||||||
|
handleSetLogChannel(s, i)
|
||||||
|
case "enablelogging":
|
||||||
|
log.Printf("log: /config enablelogging invoked by %s in guild %s", i.Member.User.ID, i.GuildID)
|
||||||
|
handleSetLoggingEnabled(s, i, true)
|
||||||
|
case "disablelogging":
|
||||||
|
log.Printf("log: /config disablelogging invoked by %s in guild %s", i.Member.User.ID, i.GuildID)
|
||||||
|
handleSetLoggingEnabled(s, i, false)
|
||||||
|
default:
|
||||||
|
respondEphemeral(s, i, "Unknown subcommand.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleAddStreamer(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var username string
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
if o.Name == "username" {
|
||||||
|
username = strings.TrimSpace(o.StringValue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
username = strings.TrimPrefix(username, "https://www.twitch.tv/")
|
||||||
|
username = strings.TrimPrefix(username, "http://www.twitch.tv/")
|
||||||
|
username = strings.TrimPrefix(username, "twitch.tv/")
|
||||||
|
username = strings.TrimSpace(username)
|
||||||
|
|
||||||
|
if username == "" {
|
||||||
|
respondEphemeral(s, i, "Username cannot be empty.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.Twitch.UpsertStreamer(context.Background(), guildID, username); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to add streamer.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondEphemeral(s, i, "Added `"+username+"` for Twitch live notifications.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleRemoveStreamer(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var username string
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
if o.Name == "username" {
|
||||||
|
username = strings.TrimSpace(o.StringValue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
username = strings.TrimSpace(username)
|
||||||
|
if username == "" {
|
||||||
|
respondEphemeral(s, i, "Username cannot be empty.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.Twitch.RemoveStreamer(context.Background(), guildID, username); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to remove streamer.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondEphemeral(s, i, "Removed `"+username+"` from Twitch live notifications.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetChannel(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var chOpt *discordgo.ApplicationCommandInteractionDataOption
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
if o.Name == "channel" {
|
||||||
|
chOpt = o
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if chOpt == nil {
|
||||||
|
respondEphemeral(s, i, "Missing channel option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := chOpt.ChannelValue(s)
|
||||||
|
if ch == nil {
|
||||||
|
respondEphemeral(s, i, "Invalid channel.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
chID, err := strconv.ParseInt(ch.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid channel ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.Twitch.SetNotificationChannel(context.Background(), guildID, chID); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to set notification channel.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondEphemeral(s, i, "Twitch notifications will be sent to "+ch.Mention()+".")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetWelcomeChannel(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if services.Global.Welcome == nil {
|
||||||
|
respondEphemeral(s, i, "Welcome configuration is not available.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var chOpt *discordgo.ApplicationCommandInteractionDataOption
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
if o.Name == "channel" {
|
||||||
|
chOpt = o
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if chOpt == nil {
|
||||||
|
respondEphemeral(s, i, "Missing channel option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := chOpt.ChannelValue(s)
|
||||||
|
if ch == nil {
|
||||||
|
respondEphemeral(s, i, "Invalid channel.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
chID, err := strconv.ParseInt(ch.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid channel ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.Welcome.SetChannel(context.Background(), guildID, chID); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to set welcome channel.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondEphemeral(s, i, "Welcome messages will be sent to "+ch.Mention()+".")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetWelcomeMessage(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if services.Global.Welcome == nil {
|
||||||
|
respondEphemeral(s, i, "Welcome configuration is not available.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var msg string
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
if o.Name == "message" {
|
||||||
|
msg = strings.TrimSpace(o.StringValue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if msg == "" {
|
||||||
|
respondEphemeral(s, i, "Message cannot be empty.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.Welcome.SetMessage(context.Background(), guildID, msg); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to set welcome message.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondEphemeral(s, i, "Welcome message updated.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetWelcomeDM(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if services.Global.Welcome == nil {
|
||||||
|
respondEphemeral(s, i, "Welcome configuration is not available.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var msg string
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
if o.Name == "message" {
|
||||||
|
msg = strings.TrimSpace(o.StringValue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if msg == "" {
|
||||||
|
respondEphemeral(s, i, "DM message cannot be empty.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.Welcome.SetDM(context.Background(), guildID, msg); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to set welcome DM.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondEphemeral(s, i, "Welcome DM updated.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetWelcomeGIF(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if services.Global.Welcome == nil {
|
||||||
|
respondEphemeral(s, i, "Welcome configuration is not available.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var url string
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
if o.Name == "url" {
|
||||||
|
url = strings.TrimSpace(o.StringValue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if url == "" {
|
||||||
|
respondEphemeral(s, i, "URL cannot be empty.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.Welcome.SetGIF(context.Background(), guildID, url); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to set welcome GIF.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondEphemeral(s, i, "Welcome GIF updated.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetDefaultRole(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if services.Global.DefaultRole == nil {
|
||||||
|
respondEphemeral(s, i, "Default role configuration is not available.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var roleOpt *discordgo.ApplicationCommandInteractionDataOption
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
if o.Name == "role" {
|
||||||
|
roleOpt = o
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if roleOpt == nil {
|
||||||
|
respondEphemeral(s, i, "Missing role option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
role := roleOpt.RoleValue(s, i.GuildID)
|
||||||
|
if role == nil {
|
||||||
|
respondEphemeral(s, i, "Invalid role.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
roleID, err := strconv.ParseInt(role.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid role ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.DefaultRole.SetRole(context.Background(), guildID, roleID); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to set default role.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondEphemeral(s, i, "Default role set to "+role.Mention()+".")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetLogChannel(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if services.Global.LogSettings == nil {
|
||||||
|
respondEphemeral(s, i, "Log settings service is not available.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var chOpt *discordgo.ApplicationCommandInteractionDataOption
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
if o.Name == "channel" {
|
||||||
|
chOpt = o
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if chOpt == nil {
|
||||||
|
respondEphemeral(s, i, "Missing channel option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := chOpt.ChannelValue(s)
|
||||||
|
if ch == nil {
|
||||||
|
respondEphemeral(s, i, "Invalid channel.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
channelID, err := strconv.ParseInt(ch.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid channel ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.LogSettings.SetChannel(context.Background(), guildID, channelID); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to set log channel.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := services.Global.LogSettings.SetEnabled(context.Background(), guildID, true); err != nil {
|
||||||
|
respondEphemeral(s, i, "Log channel set, but failed to enable logging.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondEphemeral(s, i, "Logging enabled in "+ch.Mention()+".")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetLoggingEnabled(s *discordgo.Session, i *discordgo.InteractionCreate, enabled bool) {
|
||||||
|
if services.Global.LogSettings == nil {
|
||||||
|
respondEphemeral(s, i, "Log settings service is not available.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.LogSettings.SetEnabled(context.Background(), guildID, enabled); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to update logging state.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if enabled {
|
||||||
|
respondEphemeral(s, i, "Logging is now enabled.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respondEphemeral(s, i, "Logging is now disabled.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func respondEphemeral(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: msg,
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"velox-bot/internal/commands/config/public"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Config *discordgo.ApplicationCommand = public.Config
|
||||||
|
)
|
||||||
|
|
||||||
|
func ConfigHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.ConfigHandler(s, i) }
|
||||||
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"velox-bot/internal/commands/fun/shared"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Coinflip = &discordgo.ApplicationCommand{
|
||||||
|
Name: "coinflip",
|
||||||
|
Description: "Flip a coin",
|
||||||
|
}
|
||||||
|
|
||||||
|
func CoinflipHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
num := rand.Intn(2)
|
||||||
|
if num == 0 {
|
||||||
|
shared.Respond(s, i, "Heads!")
|
||||||
|
} else {
|
||||||
|
shared.Respond(s, i, "Tails!")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"velox-bot/internal/commands/fun/shared"
|
||||||
|
"velox-bot/internal/diceexpr"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Dice = &discordgo.ApplicationCommand{
|
||||||
|
Name: "dice",
|
||||||
|
Description: "Roll dice expressions (e.g. d20, 2d20+1, 2#d20+1)",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "expr",
|
||||||
|
Description: "Dice expression to roll",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Contexts: &[]discordgo.InteractionContextType{
|
||||||
|
discordgo.InteractionContextGuild,
|
||||||
|
discordgo.InteractionContextBotDM,
|
||||||
|
discordgo.InteractionContextPrivateChannel,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func DiceHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
expr := ""
|
||||||
|
if opts := i.ApplicationCommandData().Options; len(opts) > 0 {
|
||||||
|
expr = opts[0].StringValue()
|
||||||
|
}
|
||||||
|
expr = strings.TrimSpace(expr)
|
||||||
|
if expr == "" {
|
||||||
|
respondDiceFailure(s, i)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := diceexpr.EvalMany(expr, diceexpr.DefaultLimits())
|
||||||
|
if err != nil {
|
||||||
|
respondDiceFailure(s, i)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
out := diceexpr.FormatResult(res, diceexpr.DefaultFormatOptions())
|
||||||
|
if len(out) > 1900 {
|
||||||
|
// Keep within Discord message limits; trim safely.
|
||||||
|
out = out[:1900] + "…"
|
||||||
|
}
|
||||||
|
shared.Respond(s, i, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func respondDiceFailure(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if i.GuildID != "" {
|
||||||
|
shared.RespondEphemeral(s, i, "Failure!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
shared.Respond(s, i, "Failure!")
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/commands/fun/shared"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Joke = &discordgo.ApplicationCommand{
|
||||||
|
Name: "joke",
|
||||||
|
Description: "Get a random joke",
|
||||||
|
Contexts: &[]discordgo.InteractionContextType{
|
||||||
|
discordgo.InteractionContextGuild,
|
||||||
|
discordgo.InteractionContextBotDM,
|
||||||
|
discordgo.InteractionContextPrivateChannel,
|
||||||
|
},
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "type",
|
||||||
|
Description: "The type of joke to get",
|
||||||
|
Required: false,
|
||||||
|
Choices: []*discordgo.ApplicationCommandOptionChoice{
|
||||||
|
{
|
||||||
|
Name: "Programming",
|
||||||
|
Value: "Programming",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Misc",
|
||||||
|
Value: "Miscellaneous",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Dark",
|
||||||
|
Value: "Dark",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Pun",
|
||||||
|
Value: "Pun",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Spooky",
|
||||||
|
Value: "Spooky",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Christmas",
|
||||||
|
Value: "Christmas",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func JokeHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
jokeType := "Any"
|
||||||
|
if opts := i.ApplicationCommandData().Options; len(opts) > 0 {
|
||||||
|
if v := opts[0].StringValue(); v != "" {
|
||||||
|
jokeType = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !isValidJokeType(jokeType) {
|
||||||
|
jokeType = "Any"
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 6*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
content, err := fetchJoke(ctx, jokeType)
|
||||||
|
if err != nil {
|
||||||
|
if i.GuildID != "" {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to get joke.")
|
||||||
|
} else {
|
||||||
|
shared.Respond(s, i, "Failed to get joke.")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
shared.Respond(s, i, content)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isValidJokeType(t string) bool {
|
||||||
|
switch t {
|
||||||
|
case "Any", "Programming", "Misc", "Dark", "Pun", "Spooky", "Christmas":
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type jokeAPIResponse struct {
|
||||||
|
Error bool `json:"error"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Joke string `json:"joke"`
|
||||||
|
Setup string `json:"setup"`
|
||||||
|
Delivery string `json:"delivery"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchJoke(ctx context.Context, jokeType string) (string, error) {
|
||||||
|
const baseURL = "https://v2.jokeapi.dev/joke"
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/%s", baseURL, jokeType), nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
return "", fmt.Errorf("joke api status: %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
var data jokeAPIResponse
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if data.Error {
|
||||||
|
if data.Message != "" {
|
||||||
|
return "", errors.New(data.Message)
|
||||||
|
}
|
||||||
|
return "", errors.New("joke api returned error")
|
||||||
|
}
|
||||||
|
|
||||||
|
switch data.Type {
|
||||||
|
case "single":
|
||||||
|
if data.Joke == "" {
|
||||||
|
return "", errors.New("empty joke")
|
||||||
|
}
|
||||||
|
return data.Joke, nil
|
||||||
|
case "twopart":
|
||||||
|
if data.Setup == "" && data.Delivery == "" {
|
||||||
|
return "", errors.New("empty joke")
|
||||||
|
}
|
||||||
|
if data.Setup == "" {
|
||||||
|
return data.Delivery, nil
|
||||||
|
}
|
||||||
|
if data.Delivery == "" {
|
||||||
|
return data.Setup, nil
|
||||||
|
}
|
||||||
|
return data.Setup + "\n" + data.Delivery, nil
|
||||||
|
default:
|
||||||
|
return "", fmt.Errorf("unknown joke type: %q", data.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package fun
|
package public
|
||||||
|
|
||||||
import "github.com/bwmarrin/discordgo"
|
import "github.com/bwmarrin/discordgo"
|
||||||
|
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"velox-bot/internal/commands/fun/shared"
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
rpssvc "velox-bot/internal/db/services/rps"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Rps = &discordgo.ApplicationCommand{
|
||||||
|
Name: "rps",
|
||||||
|
Description: "Plays Rock Paper Scissors with you",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "hand",
|
||||||
|
Description: "Choose between ✌️, ✋ or 🤜",
|
||||||
|
Required: true,
|
||||||
|
Choices: []*discordgo.ApplicationCommandOptionChoice{
|
||||||
|
{Name: "✌️ - Scissors", Value: string(rpssvc.HandScissors)},
|
||||||
|
{Name: "✋ - Paper", Value: string(rpssvc.HandPaper)},
|
||||||
|
{Name: "🤜 - Rock", Value: string(rpssvc.HandRock)},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Contexts: &[]discordgo.InteractionContextType{
|
||||||
|
discordgo.InteractionContextGuild,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func RpsHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if i.GuildID == "" {
|
||||||
|
shared.Respond(s, i, "This command can only be used in a server.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if services.Global == nil || services.Global.RPS == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "RPS service is not available.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid hand! Please choose between ✌️, ✋ or 🤜")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
handStr := data.Options[0].StringValue()
|
||||||
|
userHand, ok := rpssvc.ParseHand(handStr)
|
||||||
|
if !ok {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid hand! Please choose between ✌️, ✋ or 🤜")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid guild.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user := i.User
|
||||||
|
if i.Member != nil && i.Member.User != nil {
|
||||||
|
user = i.Member.User
|
||||||
|
}
|
||||||
|
if user == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, err := strconv.ParseInt(user.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid user.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := services.Global.RPS.Play(context.Background(), guildID, userID, userHand)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to play RPS.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
color := 0xEF4444
|
||||||
|
switch out.Result {
|
||||||
|
case rpssvc.ResultWin:
|
||||||
|
color = 0x22C55E
|
||||||
|
case rpssvc.ResultTie:
|
||||||
|
color = 0xF59E0B
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: string(out.Result),
|
||||||
|
Description: user.Mention() + " vs bot",
|
||||||
|
Color: color,
|
||||||
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
|
{Name: "Your hand", Value: out.UserHand.String(), Inline: true},
|
||||||
|
{Name: "Bot hand", Value: out.BotHand.String(), Inline: true},
|
||||||
|
{Name: "Score", Value: strconv.Itoa(out.Score), Inline: true},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Embeds: []*discordgo.MessageEmbed{embed},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"velox-bot/internal/commands/fun/shared"
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var RpsLeaderboard = &discordgo.ApplicationCommand{
|
||||||
|
Name: "rpsleaderboard",
|
||||||
|
Description: "Shows the RPS leaderboard",
|
||||||
|
Contexts: &[]discordgo.InteractionContextType{
|
||||||
|
discordgo.InteractionContextGuild,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func RpsLeaderboardHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if i.GuildID == "" {
|
||||||
|
shared.Respond(s, i, "This command can only be used in a server.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if services.Global == nil || services.Global.RPS == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "RPS service is not available.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid guild.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
top, err := services.Global.RPS.TopScores(context.Background(), guildID, 10)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to load leaderboard.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(top) == 0 {
|
||||||
|
shared.RespondEphemeral(s, i, "No leaderboard data yet.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := make([]*discordgo.MessageEmbedField, 0, len(top))
|
||||||
|
for idx, entry := range top {
|
||||||
|
display := fmt.Sprintf("<@%d>", entry.UserID)
|
||||||
|
if u, err := s.User(strconv.FormatInt(entry.UserID, 10)); err == nil && u != nil && u.Username != "" {
|
||||||
|
display = u.Username
|
||||||
|
}
|
||||||
|
fields = append(fields, &discordgo.MessageEmbedField{
|
||||||
|
Name: fmt.Sprintf("%d. %s", idx+1, display),
|
||||||
|
Value: fmt.Sprintf("Score: **%d**", entry.Score),
|
||||||
|
Inline: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "RPS Leaderboard",
|
||||||
|
Description: fmt.Sprintf("Top %d players in this server", len(top)),
|
||||||
|
Color: 0xA855F7,
|
||||||
|
Fields: fields,
|
||||||
|
}
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Embeds: []*discordgo.MessageEmbed{embed},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"velox-bot/internal/commands/fun/shared"
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var RpsStats = &discordgo.ApplicationCommand{
|
||||||
|
Name: "rpsstats",
|
||||||
|
Description: "Shows your RPS stats",
|
||||||
|
Contexts: &[]discordgo.InteractionContextType{
|
||||||
|
discordgo.InteractionContextGuild,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func RpsStatsHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if i.GuildID == "" {
|
||||||
|
shared.Respond(s, i, "This command can only be used in a server.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if services.Global == nil || services.Global.RPS == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "RPS service is not available.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid guild.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user := i.User
|
||||||
|
if i.Member != nil && i.Member.User != nil {
|
||||||
|
user = i.Member.User
|
||||||
|
}
|
||||||
|
if user == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, err := strconv.ParseInt(user.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid user.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
score, ok, err := services.Global.RPS.GetScore(context.Background(), guildID, userID)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to load stats.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
shared.RespondEphemeral(s, i, "You haven't played RPS yet!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "RPS Stats",
|
||||||
|
Description: user.Mention(),
|
||||||
|
Color: 0x3B82F6,
|
||||||
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
|
{Name: "Score", Value: strconv.Itoa(score), Inline: true},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Embeds: []*discordgo.MessageEmbed{embed},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package fun
|
||||||
|
|
||||||
|
import (
|
||||||
|
"velox-bot/internal/commands/fun/public"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Commands
|
||||||
|
var (
|
||||||
|
Ping *discordgo.ApplicationCommand = public.Ping
|
||||||
|
Joke *discordgo.ApplicationCommand = public.Joke
|
||||||
|
Coinflip *discordgo.ApplicationCommand = public.Coinflip
|
||||||
|
Dice *discordgo.ApplicationCommand = public.Dice
|
||||||
|
Rps *discordgo.ApplicationCommand = public.Rps
|
||||||
|
RpsStats *discordgo.ApplicationCommand = public.RpsStats
|
||||||
|
RpsLeaderboard *discordgo.ApplicationCommand = public.RpsLeaderboard
|
||||||
|
)
|
||||||
|
|
||||||
|
// Handlers
|
||||||
|
func PingHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.PingHandler(s, i) }
|
||||||
|
func JokeHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.JokeHandler(s, i) }
|
||||||
|
func CoinflipHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.CoinflipHandler(s, i)
|
||||||
|
}
|
||||||
|
func DiceHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.DiceHandler(s, i) }
|
||||||
|
func RpsHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.RpsHandler(s, i) }
|
||||||
|
func RpsStatsHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.RpsStatsHandler(s, i)
|
||||||
|
}
|
||||||
|
func RpsLeaderboardHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.RpsLeaderboardHandler(s, i)
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package shared
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Respond(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: msg,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RespondEphemeral(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: msg,
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
+107
-13
@@ -42,32 +42,126 @@ func HelpHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|||||||
|
|
||||||
embed := &discordgo.MessageEmbed{
|
embed := &discordgo.MessageEmbed{
|
||||||
Title: "Velox Bot Help",
|
Title: "Velox Bot Help",
|
||||||
Description: "Here is the help for the bot.",
|
Description: "Command reference. Some commands require server permissions or roles.",
|
||||||
Color: 0xFFA500,
|
Color: 0xFFA500,
|
||||||
Fields: []*discordgo.MessageEmbedField{
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
{
|
{
|
||||||
Name: "Fun Commands",
|
Name: "Fun",
|
||||||
Value: "Here are the fun commands for the bot.",
|
Value: "" +
|
||||||
|
"`/ping` — Pong!\n" +
|
||||||
|
"`/joke [type]` — Random joke (optional category)\n" +
|
||||||
|
"`/coinflip` — Flip a coin\n" +
|
||||||
|
"`/dice <expr>` — Roll dice (e.g. `d20`, `2d20+1`, `2#d20+1`)\n" +
|
||||||
|
"`/rps <hand>` — Rock Paper Scissors (server only)\n" +
|
||||||
|
"`/rpsstats` — Your RPS score (server only)\n" +
|
||||||
|
"`/rpsleaderboard` — Top RPS scores (server only)",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "/ping",
|
Name: "Music (requires **DJ** role)",
|
||||||
Value: "Pong!",
|
Value: "" +
|
||||||
|
"`/play <query>` — Play a song (search or URL)\n" +
|
||||||
|
"`/queue` — Show the current queue\n" +
|
||||||
|
"`/volume <0-150>` — Set playback volume",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "/help",
|
Name: "Scheduling (1:1 sessions)",
|
||||||
Value: "Get help with the bot.",
|
Value: "" +
|
||||||
|
"`/schedule create user:@someone datetime:\"YYYY-MM-DD HH:MM\" [description]` — Create a session\n" +
|
||||||
|
"`/schedule list` — List your upcoming sessions\n" +
|
||||||
|
"`/schedule reschedule id:<id> datetime:\"YYYY-MM-DD HH:MM\"` — Request a new time\n\n" +
|
||||||
|
"Invites are sent via DM; react with ✅ / ❌ / 🔁 to respond.",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "Leveling System",
|
Name: "Timezone",
|
||||||
Value: `The leveling system is a system that allows users to gain XP and level up. The system is based on the amount of messages sent in a server. The more messages you send, the more XP you gain. The more XP you gain, the higher your level will be. The leveling system is disabled by default, however you can enable/disable it by using the /slvl toggle command. You can also configure the leveling system by using the /slvl command.`,
|
Value: "" +
|
||||||
|
"`/timezone set <IANA>` — Set your timezone (e.g. `Europe/Lisbon`)\n" +
|
||||||
|
"`/timezone show` — Show your timezone",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "/slvl toggle",
|
Name: "Projects",
|
||||||
Value: "Toggle the leveling system.",
|
Value: "" +
|
||||||
|
"`/projects list` — List active projects\n" +
|
||||||
|
"`/projects create name:\"...\" [description]` — Create a project (**Manage Server**)\n" +
|
||||||
|
"`/projects add-helper project-id:<id> user:@someone` — Add helper (**Manage Server**)",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "/slvl set",
|
Name: "Tickets",
|
||||||
Value: "Set the level of a user.",
|
Value: "" +
|
||||||
|
"Open a ticket from the panel: you get a private thread with the staff.\n" +
|
||||||
|
"`/ticket add user:@someone` — Add someone to your ticket\n" +
|
||||||
|
"`/ticket close [reason]` — Close the ticket (opener or staff)\n" +
|
||||||
|
"`/ticket claim`, `/ticket remove user:@someone` — Staff only\n" +
|
||||||
|
"`/tickets setup`, `/tickets category add|remove|list`, `/tickets panel` — Set up (**Manage Server**)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Security (anti-raid & anti-nuke)",
|
||||||
|
Value: "" +
|
||||||
|
"`/security status` — What's on and who is trusted\n" +
|
||||||
|
"`/security toggle` — Join Gate, raid mode, anti-nuke on/off (off: owner or trusted)\n" +
|
||||||
|
"`/security alerts`, `/security quarantine-role`, `/security release`, `/security end-raid` — Owner or trusted\n" +
|
||||||
|
"`/security trust add|remove` — Owner only\n" +
|
||||||
|
"`/security snapshot` — Save roles and channels; `/security restore` — Rebuild what a nuke deleted (owner or trusted)\n" +
|
||||||
|
"`/lockdown channel|server|joins`, `/unlock …` (unlock: owner or trusted)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Meetings (**Manage Server**)",
|
||||||
|
Value: "" +
|
||||||
|
"`/meeting set-lobby channel:#voice` — Set voice lobby used to create meetings\n" +
|
||||||
|
"`/meeting lock` — Lock your meeting room\n" +
|
||||||
|
"`/meeting lock-private` — Lock + hide your meeting room\n" +
|
||||||
|
"`/meeting unlock` — Unlock your meeting room\n" +
|
||||||
|
"`/meeting invite user:@someone` — Allow a user to join\n" +
|
||||||
|
"`/meeting uninvite user:@someone` — Remove a user's permission",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Moderation (focused tools)",
|
||||||
|
Value: "" +
|
||||||
|
"`/moderation purge amount:<1-100>` — Delete recent messages (**Manage Messages**)\n" +
|
||||||
|
"`/moderation timeout user:@someone duration:<10m|1h|...> [reason]` — Timeout member (**Moderate Members**)\n" +
|
||||||
|
"`/moderation kick user:@someone [reason]` — Kick member (**Kick Members**)\n" +
|
||||||
|
"`/moderation ban user:@someone [delete_days] [reason]` — Ban member (**Ban Members**)\n" +
|
||||||
|
"`/moderation unban user_id:<id> [reason]` — Unban by user ID (**Ban Members**)\n" +
|
||||||
|
"`/moderation untimeout user:@someone [reason]` — Remove timeout (**Moderate Members**)\n" +
|
||||||
|
"`/moderation slowmode channel:#channel seconds:<0-21600>` — Set slowmode (**Manage Channels**)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Leveling",
|
||||||
|
Value: "" +
|
||||||
|
"`/slvl toggle` — Enable/disable leveling (**Manage Server**)\n" +
|
||||||
|
"`/slvl set user:@someone level:<n>` — Set user level (**Manage Server**)\n" +
|
||||||
|
"`/slvl set-channel channel:#channel` — Level-up message channel (**Manage Server**)\n" +
|
||||||
|
"`/slvl set-message message:\"...\"` — Custom level-up message (**Manage Server**)\n" +
|
||||||
|
"`/slvl set-reward level:<n> role:@role` — Role rewards (**Manage Server**)\n" +
|
||||||
|
"`/rank [user]` — Your rank card (server only)\n" +
|
||||||
|
"`/leaderboard` — Top levels (server only)\n" +
|
||||||
|
"`/rewards` — List configured rewards (server only)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Twitch live notifications (**Manage Server**)",
|
||||||
|
Value: "" +
|
||||||
|
"`/config addstreamer username:<twitch-name>` — Add a streamer\n" +
|
||||||
|
"`/config removestreamer username:<twitch-name>` — Remove a streamer\n" +
|
||||||
|
"`/config settwitchnotificationchannel channel:#channel` — Set notification channel",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Welcome / onboarding (**Manage Server**)",
|
||||||
|
Value: "" +
|
||||||
|
"`/config setwelcomechannel channel:#channel` — Channel for welcome messages\n" +
|
||||||
|
"`/config setwelcomemessage message:\"...\"` — Welcome text (supports `{user}`, `{server}`)\n" +
|
||||||
|
"`/config setwelcomedm message:\"...\"` — DM template (supports `{user}`, `{server}`)\n" +
|
||||||
|
"`/config setwelcomegif url:<image-url>` — GIF/image used in welcome embed\n" +
|
||||||
|
"`/config setdefaultrole role:@role` — Role granted to new members",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Logging (**Manage Server**)",
|
||||||
|
Value: "" +
|
||||||
|
"`/config setlogchannel channel:#channel` — Set log output channel\n" +
|
||||||
|
"`/config enablelogging` — Enable event/moderation logging\n" +
|
||||||
|
"`/config disablelogging` — Disable event/moderation logging",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Other",
|
||||||
|
Value: "`/help` — Show this help in DMs",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Author: &author,
|
Author: &author,
|
||||||
|
|||||||
@@ -17,7 +17,10 @@ var (
|
|||||||
|
|
||||||
// Handlers
|
// Handlers
|
||||||
func RankHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.RankHandler(s, i) }
|
func RankHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.RankHandler(s, i) }
|
||||||
func LeaderboardHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.LeaderboardHandler(s, i) }
|
func LeaderboardHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
func RewardsHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.RewardsHandler(s, i) }
|
public.LeaderboardHandler(s, i)
|
||||||
|
}
|
||||||
|
func RewardsHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.RewardsHandler(s, i)
|
||||||
|
}
|
||||||
func SlvlHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { config.SlvlHandler(s, i) }
|
func SlvlHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { config.SlvlHandler(s, i) }
|
||||||
|
|
||||||
@@ -0,0 +1,332 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"velox-bot/internal/commands/meeting/shared"
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Meeting = &discordgo.ApplicationCommand{
|
||||||
|
Name: "meeting",
|
||||||
|
Description: "Meeting room settings",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "set-lobby",
|
||||||
|
Description: "Set the voice lobby channel used to create meetings",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionChannel,
|
||||||
|
Name: "channel",
|
||||||
|
Description: "Voice channel lobby",
|
||||||
|
Required: true,
|
||||||
|
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildVoice},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "lock",
|
||||||
|
Description: "Lock your current meeting room (block others from joining)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "lock-private",
|
||||||
|
Description: "Lock and hide your meeting room (block joining and visibility)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "unlock",
|
||||||
|
Description: "Unlock your current meeting room (allow others to join)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "invite",
|
||||||
|
Description: "Allow a user to join your locked meeting room",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionUser,
|
||||||
|
Name: "user",
|
||||||
|
Description: "User to allow",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "uninvite",
|
||||||
|
Description: "Remove a previously invited user's permission to join",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionUser,
|
||||||
|
Name: "user",
|
||||||
|
Description: "User to remove",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func MeetingHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireGuild(s, i) || !shared.RequireManageGuild(s, i) || !shared.RequireMeetingService(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing subcommand.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch data.Options[0].Name {
|
||||||
|
case "set-lobby":
|
||||||
|
handleSetLobby(s, i)
|
||||||
|
case "lock":
|
||||||
|
handleLock(s, i)
|
||||||
|
case "lock-private":
|
||||||
|
handleLockPrivate(s, i)
|
||||||
|
case "unlock":
|
||||||
|
handleUnlock(s, i)
|
||||||
|
case "invite":
|
||||||
|
handleInvite(s, i)
|
||||||
|
case "uninvite":
|
||||||
|
handleUninvite(s, i)
|
||||||
|
default:
|
||||||
|
shared.RespondEphemeral(s, i, "Unknown subcommand.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetLobby(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
guildID, ok := shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
optMap := shared.SubcommandOptionMap(i)
|
||||||
|
channelOpt, ok := optMap["channel"]
|
||||||
|
if !ok {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing channel option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := channelOpt.ChannelValue(s)
|
||||||
|
if ch == nil || ch.Type != discordgo.ChannelTypeGuildVoice {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid voice channel.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
channelID, err := strconv.ParseInt(ch.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid channel ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.Meeting.SetLobbyChannel(context.Background(), guildID, channelID); err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to set meeting lobby.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
shared.RespondEphemeral(s, i, "Meeting lobby set to "+ch.Mention()+".")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleLock(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
ch, chID64, guildID64, ok := requireOwnTempVoiceChannel(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deny CONNECT for @everyone (guild ID).
|
||||||
|
if err := s.ChannelPermissionSet(
|
||||||
|
ch.ID,
|
||||||
|
i.GuildID,
|
||||||
|
discordgo.PermissionOverwriteTypeRole,
|
||||||
|
0,
|
||||||
|
discordgo.PermissionVoiceConnect,
|
||||||
|
); err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to lock the meeting room.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
shared.RespondEphemeral(s, i, "Locked "+ch.Mention()+". Use `/meeting invite user:@someone` to allow specific people.")
|
||||||
|
|
||||||
|
_ = chID64
|
||||||
|
_ = guildID64
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleLockPrivate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
ch, _, _, ok := requireOwnTempVoiceChannel(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deny CONNECT and VIEW_CHANNEL for @everyone (guild ID).
|
||||||
|
deny := int64(discordgo.PermissionVoiceConnect | discordgo.PermissionViewChannel)
|
||||||
|
if err := s.ChannelPermissionSet(
|
||||||
|
ch.ID,
|
||||||
|
i.GuildID,
|
||||||
|
discordgo.PermissionOverwriteTypeRole,
|
||||||
|
0,
|
||||||
|
deny,
|
||||||
|
); err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to lock the meeting room.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
shared.RespondEphemeral(s, i, "Locked (private) "+ch.Mention()+". Use `/meeting invite user:@someone` to allow specific people.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleUnlock(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
ch, _, _, ok := requireOwnTempVoiceChannel(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the @everyone overwrite so defaults apply again.
|
||||||
|
if err := s.ChannelPermissionDelete(ch.ID, i.GuildID); err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to unlock the meeting room.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
shared.RespondEphemeral(s, i, "Unlocked "+ch.Mention()+".")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleInvite(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
ch, _, _, ok := requireOwnTempVoiceChannel(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
optMap := shared.SubcommandOptionMap(i)
|
||||||
|
userOpt, ok := optMap["user"]
|
||||||
|
if !ok {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing user option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u := userOpt.UserValue(s)
|
||||||
|
if u == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid user.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow CONNECT for the invited user.
|
||||||
|
if err := s.ChannelPermissionSet(
|
||||||
|
ch.ID,
|
||||||
|
u.ID,
|
||||||
|
discordgo.PermissionOverwriteTypeMember,
|
||||||
|
discordgo.PermissionVoiceConnect,
|
||||||
|
0,
|
||||||
|
); err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to invite user.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
shared.RespondEphemeral(s, i, "Invited "+u.Mention()+" to "+ch.Mention()+".")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleUninvite(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
ch, _, _, ok := requireOwnTempVoiceChannel(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
optMap := shared.SubcommandOptionMap(i)
|
||||||
|
userOpt, ok := optMap["user"]
|
||||||
|
if !ok {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing user option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u := userOpt.UserValue(s)
|
||||||
|
if u == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid user.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove any member-specific overwrite for this user.
|
||||||
|
if err := s.ChannelPermissionDelete(ch.ID, u.ID); err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to remove invite.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
shared.RespondEphemeral(s, i, "Removed "+u.Mention()+" from "+ch.Mention()+".")
|
||||||
|
}
|
||||||
|
|
||||||
|
func requireOwnTempVoiceChannel(s *discordgo.Session, i *discordgo.InteractionCreate) (channel *discordgo.Channel, channelID64 int64, guildID64 int64, ok bool) {
|
||||||
|
guildID64, ok = shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return nil, 0, 0, false
|
||||||
|
}
|
||||||
|
if i.Member == nil || i.Member.User == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing member info.")
|
||||||
|
return nil, 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
voiceChannelID, found := findMemberVoiceChannelID(s, i.GuildID, i.Member.User.ID)
|
||||||
|
if !found || voiceChannelID == "" {
|
||||||
|
shared.RespondEphemeral(s, i, "You must be in your meeting voice channel to use this.")
|
||||||
|
return nil, 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
ch, err := s.Channel(voiceChannelID)
|
||||||
|
if err != nil || ch == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to load your voice channel.")
|
||||||
|
return nil, 0, 0, false
|
||||||
|
}
|
||||||
|
if ch.Type != discordgo.ChannelTypeGuildVoice {
|
||||||
|
shared.RespondEphemeral(s, i, "This only works in a voice channel.")
|
||||||
|
return nil, 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
channelID64, err = strconv.ParseInt(ch.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid channel ID.")
|
||||||
|
return nil, 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
isTemp, err := services.Global.Meeting.IsTempChannel(context.Background(), guildID64, channelID64)
|
||||||
|
if err != nil || !isTemp {
|
||||||
|
shared.RespondEphemeral(s, i, "This is not a bot-created meeting room.")
|
||||||
|
return nil, 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
ownerID, ownerOK, err := services.Global.Meeting.GetTempChannelOwner(context.Background(), guildID64, channelID64)
|
||||||
|
if err != nil || !ownerOK {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to verify meeting room owner.")
|
||||||
|
return nil, 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
invokerID64, _ := strconv.ParseInt(i.Member.User.ID, 10, 64)
|
||||||
|
if invokerID64 == 0 || ownerID != invokerID64 {
|
||||||
|
shared.RespondEphemeral(s, i, fmt.Sprintf("Only the room creator can do this. (creator: <@%d>)", ownerID))
|
||||||
|
return nil, 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return ch, channelID64, guildID64, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func findMemberVoiceChannelID(s *discordgo.Session, guildID, userID string) (string, bool) {
|
||||||
|
// Prefer session state cache (discordgo tracks voice states there when IntentsGuildVoiceStates is enabled).
|
||||||
|
if s != nil && s.State != nil {
|
||||||
|
if vs, err := s.State.VoiceState(guildID, userID); err == nil && vs != nil {
|
||||||
|
if vs.ChannelID != "" {
|
||||||
|
return vs.ChannelID, true
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
// Fallback to cached guild object (State, not REST).
|
||||||
|
if g, err := s.State.Guild(guildID); err == nil && g != nil {
|
||||||
|
for _, st := range g.VoiceStates {
|
||||||
|
if st != nil && st.UserID == userID {
|
||||||
|
return st.ChannelID, st.ChannelID != ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package meeting
|
||||||
|
|
||||||
|
import (
|
||||||
|
"velox-bot/internal/commands/meeting/public"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Meeting *discordgo.ApplicationCommand = public.Meeting
|
||||||
|
)
|
||||||
|
|
||||||
|
func MeetingHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.MeetingHandler(s, i) }
|
||||||
|
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package shared
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RespondEphemeral(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: msg,
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequireGuild(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
if i.GuildID == "" {
|
||||||
|
RespondEphemeral(s, i, "This command can only be used in a server.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequireManageGuild(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
if i.Member == nil || (i.Member.Permissions&discordgo.PermissionManageGuild) == 0 {
|
||||||
|
RespondEphemeral(s, i, "You don't have permission to manage meeting settings.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequireMeetingService(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
if services.Global == nil || services.Global.Meeting == nil {
|
||||||
|
RespondEphemeral(s, i, "Meeting service is not available.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseGuildID(s *discordgo.Session, i *discordgo.InteractionCreate) (int64, bool) {
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
RespondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return guildID, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func SubcommandOptionMap(i *discordgo.InteractionCreate) map[string]*discordgo.ApplicationCommandInteractionDataOption {
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
return map[string]*discordgo.ApplicationCommandInteractionDataOption{}
|
||||||
|
}
|
||||||
|
opts := data.Options[0].Options
|
||||||
|
m := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(opts))
|
||||||
|
for _, opt := range opts {
|
||||||
|
m[opt.Name] = opt
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,578 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
"velox-bot/internal/modlog"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Moderation = &discordgo.ApplicationCommand{
|
||||||
|
Name: "moderation",
|
||||||
|
Description: "High-value moderation tools",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "purge",
|
||||||
|
Description: "Delete recent messages in this channel (max 100)",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionInteger,
|
||||||
|
Name: "amount",
|
||||||
|
Description: "How many recent messages to delete (1-100)",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "timeout",
|
||||||
|
Description: "Temporarily timeout a member",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionUser,
|
||||||
|
Name: "user",
|
||||||
|
Description: "User to timeout",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "duration",
|
||||||
|
Description: "Duration like 10m, 1h, 2h30m (max 28d)",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "reason",
|
||||||
|
Description: "Optional reason",
|
||||||
|
Required: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "kick",
|
||||||
|
Description: "Kick a member from the server",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionUser,
|
||||||
|
Name: "user",
|
||||||
|
Description: "User to kick",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "reason",
|
||||||
|
Description: "Optional reason",
|
||||||
|
Required: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "ban",
|
||||||
|
Description: "Ban a member from the server",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionUser,
|
||||||
|
Name: "user",
|
||||||
|
Description: "User to ban",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionInteger,
|
||||||
|
Name: "delete_days",
|
||||||
|
Description: "Delete message history (0-7 days)",
|
||||||
|
Required: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "reason",
|
||||||
|
Description: "Optional reason",
|
||||||
|
Required: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "unban",
|
||||||
|
Description: "Unban a user by ID",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "user_id",
|
||||||
|
Description: "User ID to unban",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "reason",
|
||||||
|
Description: "Optional reason",
|
||||||
|
Required: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "untimeout",
|
||||||
|
Description: "Remove timeout from a member",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionUser,
|
||||||
|
Name: "user",
|
||||||
|
Description: "User to untimeout",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "reason",
|
||||||
|
Description: "Optional reason",
|
||||||
|
Required: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "slowmode",
|
||||||
|
Description: "Set channel slowmode (0 disables)",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionChannel,
|
||||||
|
Name: "channel",
|
||||||
|
Description: "Text channel to update",
|
||||||
|
Required: true,
|
||||||
|
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionInteger,
|
||||||
|
Name: "seconds",
|
||||||
|
Description: "Slowmode delay in seconds (0-21600)",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func ModerationHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if i.GuildID == "" {
|
||||||
|
respondEphemeral(s, i, "This command can only be used in a server.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
respondEphemeral(s, i, "Missing subcommand.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch data.Options[0].Name {
|
||||||
|
case "purge":
|
||||||
|
handlePurge(s, i)
|
||||||
|
case "timeout":
|
||||||
|
handleTimeout(s, i)
|
||||||
|
case "kick":
|
||||||
|
handleKick(s, i)
|
||||||
|
case "ban":
|
||||||
|
handleBan(s, i)
|
||||||
|
case "unban":
|
||||||
|
handleUnban(s, i)
|
||||||
|
case "untimeout":
|
||||||
|
handleUntimeout(s, i)
|
||||||
|
case "slowmode":
|
||||||
|
handleSlowmode(s, i)
|
||||||
|
default:
|
||||||
|
respondEphemeral(s, i, "Unknown subcommand.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlePurge(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !hasPerm(i, discordgo.PermissionManageMessages) {
|
||||||
|
respondEphemeral(s, i, "You need **Manage Messages** for this command.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var amount int64
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
if o.Name == "amount" {
|
||||||
|
amount = o.IntValue()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if amount < 1 || amount > 100 {
|
||||||
|
respondEphemeral(s, i, "Amount must be between 1 and 100.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
msgs, err := s.ChannelMessages(i.ChannelID, int(amount), "", "", "")
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to fetch messages.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(msgs) == 0 {
|
||||||
|
respondEphemeral(s, i, "No messages found to delete.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tooOldCutoff := time.Now().Add(-14 * 24 * time.Hour)
|
||||||
|
ids := make([]string, 0, len(msgs))
|
||||||
|
for _, m := range msgs {
|
||||||
|
if m == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if m.Timestamp.Before(tooOldCutoff) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ids = append(ids, m.ID)
|
||||||
|
}
|
||||||
|
if len(ids) == 0 {
|
||||||
|
respondEphemeral(s, i, "No messages can be bulk deleted (older than 14 days).")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ids) == 1 {
|
||||||
|
if err := s.ChannelMessageDelete(i.ChannelID, ids[0]); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to delete message.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respondEphemeral(s, i, "Deleted 1 message.")
|
||||||
|
sendModerationActionLog(s, i, "Purge", fmt.Sprintf("Deleted 1 message in <#%s>.", i.ChannelID), "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.ChannelMessagesBulkDelete(i.ChannelID, ids); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to bulk delete messages.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respondEphemeral(s, i, fmt.Sprintf("Deleted %d messages.", len(ids)))
|
||||||
|
sendModerationActionLog(s, i, "Purge", fmt.Sprintf("Deleted %d messages in <#%s>.", len(ids), i.ChannelID), "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleTimeout(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !hasPerm(i, discordgo.PermissionModerateMembers) {
|
||||||
|
respondEphemeral(s, i, "You need **Moderate Members** for this command.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var userOpt *discordgo.ApplicationCommandInteractionDataOption
|
||||||
|
var durationInput string
|
||||||
|
var reason string
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
switch o.Name {
|
||||||
|
case "user":
|
||||||
|
userOpt = o
|
||||||
|
case "duration":
|
||||||
|
durationInput = strings.TrimSpace(o.StringValue())
|
||||||
|
case "reason":
|
||||||
|
reason = strings.TrimSpace(o.StringValue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if userOpt == nil {
|
||||||
|
respondEphemeral(s, i, "Missing user option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
target := userOpt.UserValue(s)
|
||||||
|
if target == nil {
|
||||||
|
respondEphemeral(s, i, "Invalid user.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dur, err := time.ParseDuration(durationInput)
|
||||||
|
if err != nil || dur <= 0 {
|
||||||
|
respondEphemeral(s, i, "Invalid duration. Examples: `10m`, `1h`, `2h30m`.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
maxTimeout := 28 * 24 * time.Hour
|
||||||
|
if dur > maxTimeout {
|
||||||
|
respondEphemeral(s, i, "Duration cannot exceed 28 days.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
until := time.Now().UTC().Add(dur)
|
||||||
|
if err := s.GuildMemberTimeout(i.GuildID, target.ID, &until); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to timeout user. Check role hierarchy and permissions.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if reason == "" {
|
||||||
|
respondEphemeral(s, i, fmt.Sprintf("Timed out %s for `%s`.", target.Mention(), dur.String()))
|
||||||
|
sendModerationActionLog(s, i, "Timeout", fmt.Sprintf("Target: %s\nDuration: `%s`", target.Mention(), dur.String()), "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respondEphemeral(s, i, fmt.Sprintf("Timed out %s for `%s`. Reason: %s", target.Mention(), dur.String(), reason))
|
||||||
|
sendModerationActionLog(s, i, "Timeout", fmt.Sprintf("Target: %s\nDuration: `%s`", target.Mention(), dur.String()), reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleKick(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !hasPerm(i, discordgo.PermissionKickMembers) {
|
||||||
|
respondEphemeral(s, i, "You need **Kick Members** for this command.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var userOpt *discordgo.ApplicationCommandInteractionDataOption
|
||||||
|
var reason string
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
switch o.Name {
|
||||||
|
case "user":
|
||||||
|
userOpt = o
|
||||||
|
case "reason":
|
||||||
|
reason = strings.TrimSpace(o.StringValue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if userOpt == nil {
|
||||||
|
respondEphemeral(s, i, "Missing user option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
target := userOpt.UserValue(s)
|
||||||
|
if target == nil {
|
||||||
|
respondEphemeral(s, i, "Invalid user.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i.Member != nil && i.Member.User != nil && target.ID == i.Member.User.ID {
|
||||||
|
respondEphemeral(s, i, "You cannot kick yourself.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s != nil && s.State != nil && s.State.User != nil && target.ID == s.State.User.ID {
|
||||||
|
respondEphemeral(s, i, "I can't kick myself.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.GuildMemberDeleteWithReason(i.GuildID, target.ID, reason); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to kick user. Check role hierarchy and permissions.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if reason == "" {
|
||||||
|
respondEphemeral(s, i, "Kicked "+target.Mention()+".")
|
||||||
|
sendModerationActionLog(s, i, "Kick", "Target: "+target.Mention(), "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respondEphemeral(s, i, "Kicked "+target.Mention()+". Reason: "+reason)
|
||||||
|
sendModerationActionLog(s, i, "Kick", "Target: "+target.Mention(), reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleBan(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !hasPerm(i, discordgo.PermissionBanMembers) {
|
||||||
|
respondEphemeral(s, i, "You need **Ban Members** for this command.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var userOpt *discordgo.ApplicationCommandInteractionDataOption
|
||||||
|
var deleteDays int64
|
||||||
|
var reason string
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
switch o.Name {
|
||||||
|
case "user":
|
||||||
|
userOpt = o
|
||||||
|
case "delete_days":
|
||||||
|
deleteDays = o.IntValue()
|
||||||
|
case "reason":
|
||||||
|
reason = strings.TrimSpace(o.StringValue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if userOpt == nil {
|
||||||
|
respondEphemeral(s, i, "Missing user option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
target := userOpt.UserValue(s)
|
||||||
|
if target == nil {
|
||||||
|
respondEphemeral(s, i, "Invalid user.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if deleteDays < 0 || deleteDays > 7 {
|
||||||
|
respondEphemeral(s, i, "delete_days must be between 0 and 7.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i.Member != nil && i.Member.User != nil && target.ID == i.Member.User.ID {
|
||||||
|
respondEphemeral(s, i, "You cannot ban yourself.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s != nil && s.State != nil && s.State.User != nil && target.ID == s.State.User.ID {
|
||||||
|
respondEphemeral(s, i, "I can't ban myself.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.GuildBanCreateWithReason(i.GuildID, target.ID, reason, int(deleteDays)); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to ban user. Check role hierarchy and permissions.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if reason == "" {
|
||||||
|
respondEphemeral(s, i, fmt.Sprintf("Banned %s (deleted %d days of messages).", target.Mention(), deleteDays))
|
||||||
|
sendModerationActionLog(s, i, "Ban", fmt.Sprintf("Target: %s\nDeleted message days: %d", target.Mention(), deleteDays), "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respondEphemeral(s, i, fmt.Sprintf("Banned %s (deleted %d days of messages). Reason: %s", target.Mention(), deleteDays, reason))
|
||||||
|
sendModerationActionLog(s, i, "Ban", fmt.Sprintf("Target: %s\nDeleted message days: %d", target.Mention(), deleteDays), reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleUnban(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !hasPerm(i, discordgo.PermissionBanMembers) {
|
||||||
|
respondEphemeral(s, i, "You need **Ban Members** for this command.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var userID string
|
||||||
|
var reason string
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
switch o.Name {
|
||||||
|
case "user_id":
|
||||||
|
userID = strings.TrimSpace(o.StringValue())
|
||||||
|
case "reason":
|
||||||
|
reason = strings.TrimSpace(o.StringValue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if userID == "" {
|
||||||
|
respondEphemeral(s, i, "Missing user ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.GuildBanDelete(i.GuildID, userID); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to unban user. Make sure the user ID is valid and banned.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if reason == "" {
|
||||||
|
respondEphemeral(s, i, "Unbanned user ID `"+userID+"`.")
|
||||||
|
sendModerationActionLog(s, i, "Unban", "Target user ID: `"+userID+"`", "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respondEphemeral(s, i, "Unbanned user ID `"+userID+"`. Reason: "+reason)
|
||||||
|
sendModerationActionLog(s, i, "Unban", "Target user ID: `"+userID+"`", reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleUntimeout(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !hasPerm(i, discordgo.PermissionModerateMembers) {
|
||||||
|
respondEphemeral(s, i, "You need **Moderate Members** for this command.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var userOpt *discordgo.ApplicationCommandInteractionDataOption
|
||||||
|
var reason string
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
switch o.Name {
|
||||||
|
case "user":
|
||||||
|
userOpt = o
|
||||||
|
case "reason":
|
||||||
|
reason = strings.TrimSpace(o.StringValue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if userOpt == nil {
|
||||||
|
respondEphemeral(s, i, "Missing user option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
target := userOpt.UserValue(s)
|
||||||
|
if target == nil {
|
||||||
|
respondEphemeral(s, i, "Invalid user.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.GuildMemberTimeout(i.GuildID, target.ID, nil); err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to remove timeout. Check role hierarchy and permissions.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if reason == "" {
|
||||||
|
respondEphemeral(s, i, "Removed timeout from "+target.Mention()+".")
|
||||||
|
sendModerationActionLog(s, i, "Timeout Removed", "Target: "+target.Mention(), "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respondEphemeral(s, i, "Removed timeout from "+target.Mention()+". Reason: "+reason)
|
||||||
|
sendModerationActionLog(s, i, "Timeout Removed", "Target: "+target.Mention(), reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSlowmode(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !hasPerm(i, discordgo.PermissionManageChannels) {
|
||||||
|
respondEphemeral(s, i, "You need **Manage Channels** for this command.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := i.ApplicationCommandData().Options[0]
|
||||||
|
var chOpt *discordgo.ApplicationCommandInteractionDataOption
|
||||||
|
var seconds int64
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
switch o.Name {
|
||||||
|
case "channel":
|
||||||
|
chOpt = o
|
||||||
|
case "seconds":
|
||||||
|
seconds = o.IntValue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if chOpt == nil {
|
||||||
|
respondEphemeral(s, i, "Missing channel option.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if seconds < 0 || seconds > 21600 {
|
||||||
|
respondEphemeral(s, i, "Seconds must be between 0 and 21600.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ch := chOpt.ChannelValue(s)
|
||||||
|
if ch == nil {
|
||||||
|
respondEphemeral(s, i, "Invalid channel.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
secondsInt := int(seconds)
|
||||||
|
_, err := s.ChannelEditComplex(ch.ID, &discordgo.ChannelEdit{
|
||||||
|
RateLimitPerUser: &secondsInt,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
respondEphemeral(s, i, "Failed to update slowmode.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if seconds == 0 {
|
||||||
|
respondEphemeral(s, i, "Disabled slowmode in "+ch.Mention()+".")
|
||||||
|
sendModerationActionLog(s, i, "Slowmode", "Channel: "+ch.Mention()+"\nSet to: `0s`", "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respondEphemeral(s, i, fmt.Sprintf("Set slowmode in %s to %d seconds.", ch.Mention(), seconds))
|
||||||
|
sendModerationActionLog(s, i, "Slowmode", fmt.Sprintf("Channel: %s\nSet to: `%ds`", ch.Mention(), seconds), "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasPerm(i *discordgo.InteractionCreate, perm int64) bool {
|
||||||
|
return i.Member != nil && (i.Member.Permissions&perm) == perm
|
||||||
|
}
|
||||||
|
|
||||||
|
func respondEphemeral(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: msg,
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendModerationActionLog(s *discordgo.Session, i *discordgo.InteractionCreate, action, details, reason string) {
|
||||||
|
if services.Global == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
moderator := "Unknown"
|
||||||
|
if i != nil && i.Member != nil && i.Member.User != nil {
|
||||||
|
moderator = i.Member.User.Mention()
|
||||||
|
}
|
||||||
|
desc := "Moderator: " + moderator + "\n" + details
|
||||||
|
if strings.TrimSpace(reason) != "" {
|
||||||
|
desc += "\nReason: " + reason
|
||||||
|
}
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Moderation Action: " + action,
|
||||||
|
Color: 0x5865F2,
|
||||||
|
Description: desc,
|
||||||
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
_ = modlog.Send(s, services.Global, i.GuildID, embed)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package moderation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"velox-bot/internal/commands/moderation/public"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Moderation *discordgo.ApplicationCommand = public.Moderation
|
||||||
|
)
|
||||||
|
|
||||||
|
func ModerationHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.ModerationHandler(s, i)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import "github.com/bwmarrin/discordgo"
|
||||||
|
|
||||||
|
func memberHasDJRole(s *discordgo.Session, guildID string, m *discordgo.Member) bool {
|
||||||
|
if s == nil || guildID == "" || m == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if len(m.Roles) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
roles, err := s.GuildRoles(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
djRoleID := ""
|
||||||
|
for _, r := range roles {
|
||||||
|
if r != nil && r.Name == "DJ" {
|
||||||
|
djRoleID = r.ID
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if djRoleID == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rid := range m.Roles {
|
||||||
|
if rid == djRoleID {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"velox-bot/internal/commands/music/shared"
|
||||||
|
"velox-bot/internal/music"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Play = &discordgo.ApplicationCommand{
|
||||||
|
Name: "play",
|
||||||
|
Description: "Play a song via Lavalink",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "query",
|
||||||
|
Description: "Song name or URL",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func PlayHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireMusicEnabled(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
query := data.Options[0].StringValue()
|
||||||
|
|
||||||
|
// Always acknowledge quickly to avoid "application did not respond".
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Searching...",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if !memberHasDJRole(s, i.GuildID, i.Member) {
|
||||||
|
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Content: ptr("You need the **DJ** role to use music commands."),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
vs, err := findUserVoiceState(s, i.GuildID, i.Member.User.ID)
|
||||||
|
if err != nil {
|
||||||
|
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Content: ptr(fmt.Sprintf("Error: %v", err)),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
entry, started, err := music.EnqueueAndPlay(i.GuildID, vs.ChannelID, i.ChannelID, query, i.Member.User.Username, i.Member.User.ID)
|
||||||
|
if err != nil {
|
||||||
|
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Content: ptr(fmt.Sprintf("Error: %v", err)),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
title := entry.Track.Info.Title
|
||||||
|
author := entry.Track.Info.Author
|
||||||
|
length := entry.Track.Info.Length
|
||||||
|
|
||||||
|
if started {
|
||||||
|
// manager will post now-playing message & manage invalidating old buttons
|
||||||
|
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Content: ptr(fmt.Sprintf("Starting: **%s** by **%s** `[%ds]`", title, author, length/1000)),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Content: ptr(fmt.Sprintf("Added to queue: **%s** by **%s** `[%ds]`", title, author, length/1000)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ptr[T any](v T) *T { return new(v) }
|
||||||
|
|
||||||
|
func findUserVoiceState(s *discordgo.Session, guildID, userID string) (*discordgo.VoiceState, error) {
|
||||||
|
g, err := s.State.Guild(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("cannot find guild voice state")
|
||||||
|
}
|
||||||
|
for _, vs := range g.VoiceStates {
|
||||||
|
if vs.UserID == userID {
|
||||||
|
return vs, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("you must be in a voice channel")
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"velox-bot/internal/commands/music/shared"
|
||||||
|
"velox-bot/internal/music"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Queue = &discordgo.ApplicationCommand{
|
||||||
|
Name: "queue",
|
||||||
|
Description: "Show the current music queue",
|
||||||
|
}
|
||||||
|
|
||||||
|
func QueueHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireMusicEnabled(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !memberHasDJRole(s, i.GuildID, i.Member) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "You need the **DJ** role to use music commands.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
current, rest := music.GetQueue(i.GuildID)
|
||||||
|
|
||||||
|
if current == nil {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "The queue is currently empty.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
nowPlaying := fmt.Sprintf("%s — %s", current.Track.Info.Title, current.Track.Info.Author)
|
||||||
|
|
||||||
|
var descBuilder strings.Builder
|
||||||
|
if len(rest) > 0 {
|
||||||
|
limit := len(rest)
|
||||||
|
if limit > 10 {
|
||||||
|
limit = 10
|
||||||
|
}
|
||||||
|
for idx, entry := range rest[:limit] {
|
||||||
|
fmt.Fprintf(&descBuilder, "%d. %s — %s (requested by %s)\n", idx+1, entry.Track.Info.Title, entry.Track.Info.Author, entry.RequestedBy)
|
||||||
|
}
|
||||||
|
if len(rest) > limit {
|
||||||
|
fmt.Fprintf(&descBuilder, "...and %d more.", len(rest)-limit)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
descBuilder.WriteString("Nothing else in the queue.")
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Embeds: []*discordgo.MessageEmbed{
|
||||||
|
{
|
||||||
|
Title: "Music Queue",
|
||||||
|
Description: descBuilder.String(),
|
||||||
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
|
{
|
||||||
|
Name: "Now Playing",
|
||||||
|
Value: nowPlaying,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"velox-bot/internal/commands/music/shared"
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Music = &discordgo.ApplicationCommand{
|
||||||
|
Name: "music",
|
||||||
|
Description: "Music settings",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "toggle",
|
||||||
|
Description: "Turn the music player on or off for this server",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func MusicHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if i.GuildID == "" {
|
||||||
|
shared.RespondEphemeral(s, i, "This command can only be used in a server.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !shared.RequireManageGuild(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !shared.RequireMusicService(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing subcommand.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch data.Options[0].Name {
|
||||||
|
case "toggle":
|
||||||
|
handleToggle(s, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleToggle(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
guildID, ok := shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
enabled, err := services.Global.MusicSettings.ToggleMusic(context.Background(), guildID)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to toggle music playback.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
status := "disabled"
|
||||||
|
if enabled {
|
||||||
|
status = "enabled"
|
||||||
|
}
|
||||||
|
shared.RespondEphemeral(s, i, "Music playback "+status+" for this server.")
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"velox-bot/internal/commands/music/shared"
|
||||||
|
"velox-bot/internal/music"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Volume = &discordgo.ApplicationCommand{
|
||||||
|
Name: "volume",
|
||||||
|
Description: "Set playback volume (0-150)",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionInteger,
|
||||||
|
Name: "value",
|
||||||
|
Description: "Volume percent (0-150)",
|
||||||
|
Required: true,
|
||||||
|
MinValue: ptrFloat(0),
|
||||||
|
MaxValue: 150,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func VolumeHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireMusicEnabled(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Updating volume...",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if !memberHasDJRole(s, i.GuildID, i.Member) {
|
||||||
|
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Content: ptr("You need the **DJ** role to use music commands."),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Content: ptr("Missing volume value."),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
vol := int(data.Options[0].IntValue())
|
||||||
|
if err := music.SetVolume(i.GuildID, vol); err != nil {
|
||||||
|
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Content: ptr(fmt.Sprintf("Error: %v", err)),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Content: ptr(fmt.Sprintf("Volume set to **%d%%**.", vol)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ptrFloat(v float64) *float64 { return &v }
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package music
|
||||||
|
|
||||||
|
import (
|
||||||
|
"velox-bot/internal/commands/music/public"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Play *discordgo.ApplicationCommand = public.Play
|
||||||
|
Queue *discordgo.ApplicationCommand = public.Queue
|
||||||
|
Volume *discordgo.ApplicationCommand = public.Volume
|
||||||
|
Music *discordgo.ApplicationCommand = public.Music
|
||||||
|
)
|
||||||
|
|
||||||
|
func PlayHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.PlayHandler(s, i) }
|
||||||
|
func QueueHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.QueueHandler(s, i)
|
||||||
|
}
|
||||||
|
func VolumeHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.VolumeHandler(s, i) }
|
||||||
|
func MusicHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.MusicHandler(s, i) }
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package shared
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strconv"
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RequireMusicEnabled(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
guildID, ok := ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
enabled, err := services.Global.MusicSettings.IsMusicEnabled(context.Background(), guildID)
|
||||||
|
if err != nil {
|
||||||
|
RespondEphemeral(s, i, "Failed to load music settings.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !enabled {
|
||||||
|
RespondEphemeral(s, i, "Music is disabled on this server. Use /music toggle to enable it.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequireManageGuild(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
if i.Member == nil || (i.Member.Permissions&discordgo.PermissionManageGuild) == 0 {
|
||||||
|
RespondEphemeral(s, i, "You need the **Manage Server** permission to use this.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequireMusicService(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
if services.Global == nil || services.Global.MusicSettings == nil {
|
||||||
|
RespondEphemeral(s, i, "Music service is not available.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseGuildID(s *discordgo.Session, i *discordgo.InteractionCreate) (int64, bool) {
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
RespondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return guildID, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func RespondEphemeral(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: msg,
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,222 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"velox-bot/internal/commands/projects/shared"
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleComponent routes the select menus shown after /projects add-helper
|
||||||
|
// and /projects close.
|
||||||
|
func HandleComponent(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
parts := strings.Split(i.MessageComponentData().CustomID, ":")
|
||||||
|
if len(parts) < 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch parts[1] {
|
||||||
|
case "add_helper_project_select":
|
||||||
|
handleAddHelperProjectSelect(s, i)
|
||||||
|
case "add_helper_user_select":
|
||||||
|
handleAddHelperUserSelect(s, i, parts)
|
||||||
|
case "close_select":
|
||||||
|
handleCloseSelect(s, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleAddHelperProjectSelect is step 1: a project was picked, now show a
|
||||||
|
// native Discord user-select to pick who helps - the actual AddHelper call
|
||||||
|
// happens in handleAddHelperUserSelect once both are known.
|
||||||
|
func handleAddHelperProjectSelect(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireManageGuild(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
values := i.MessageComponentData().Values
|
||||||
|
if len(values) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
projectID := values[0]
|
||||||
|
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseUpdateMessage,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Who should help with this project?",
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.SelectMenu{
|
||||||
|
MenuType: discordgo.UserSelectMenu,
|
||||||
|
CustomID: "projects:add_helper_user_select:" + projectID,
|
||||||
|
Placeholder: "Select a user",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleAddHelperUserSelect is step 2: a user was picked from the native
|
||||||
|
// select menu, so both the project (carried in the CustomID) and the user
|
||||||
|
// (the selected value) are now known - perform the actual AddHelper call.
|
||||||
|
func handleAddHelperUserSelect(s *discordgo.Session, i *discordgo.InteractionCreate, parts []string) {
|
||||||
|
if !shared.RequireManageGuild(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(parts) < 3 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
projectID, err := strconv.ParseInt(parts[2], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, ok := shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
values := i.MessageComponentData().Values
|
||||||
|
if len(values) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID := values[0]
|
||||||
|
userID64, err := strconv.ParseInt(userID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
project, err := services.Global.Projects.GetProject(ctx, guildID, projectID)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to load project.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if project == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "That project no longer exists in this server.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.Projects.AddHelper(ctx, projectID, userID64); err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to add helper to project.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseUpdateMessage,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Added <@" + userID + "> as helper to project **" + project.Name + "**.",
|
||||||
|
Components: []discordgo.MessageComponent{},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCloseSelect(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireManageGuild(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, ok := shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
values := i.MessageComponentData().Values
|
||||||
|
if len(values) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
projectID, err := strconv.ParseInt(values[0], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
project, err := services.Global.Projects.GetProject(ctx, guildID, projectID)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to load project.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if project == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "That project no longer exists in this server.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.Global.Projects.DeactivateProject(ctx, guildID, projectID); err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to close project.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseUpdateMessage,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Closed project **" + project.Name + "**.",
|
||||||
|
Components: []discordgo.MessageComponent{},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleModalSubmit handles the /projects create modal.
|
||||||
|
func HandleModalSubmit(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
parts := strings.Split(i.ModalSubmitData().CustomID, ":")
|
||||||
|
if len(parts) < 2 || parts[1] != "create_modal" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !shared.RequireManageGuild(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guildID, ok := shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i.Member == nil || i.Member.User == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing member info.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
values := modalTextInputValues(i.ModalSubmitData().Components)
|
||||||
|
name := strings.TrimSpace(values["name"])
|
||||||
|
description := strings.TrimSpace(values["description"])
|
||||||
|
if name == "" {
|
||||||
|
shared.RespondEphemeral(s, i, "Project name cannot be empty.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
creatorID, err := strconv.ParseInt(i.Member.User.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid user ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := services.Global.Projects.CreateProject(context.Background(), guildID, creatorID, name, description)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to create project.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
shared.RespondEphemeral(s, i, "Created project **"+name+"** with ID `"+strconv.FormatInt(id, 10)+"`.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func modalTextInputValues(components []discordgo.MessageComponent) map[string]string {
|
||||||
|
values := make(map[string]string)
|
||||||
|
for _, c := range components {
|
||||||
|
row, ok := c.(*discordgo.ActionsRow)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, rc := range row.Components {
|
||||||
|
ti, ok := rc.(*discordgo.TextInput)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
values[ti.CustomID] = ti.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return values
|
||||||
|
}
|
||||||
@@ -0,0 +1,299 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"velox-bot/internal/commands/projects/shared"
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Projects = &discordgo.ApplicationCommand{
|
||||||
|
Name: "projects",
|
||||||
|
Description: "Manage and list ongoing projects",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "list",
|
||||||
|
Description: "List active projects with creators and helpers",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "create",
|
||||||
|
Description: "Create a new active project",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "add-helper",
|
||||||
|
Description: "Add a helper to an existing project",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "close",
|
||||||
|
Description: "Close (deactivate) a project",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProjectsHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireGuild(s, i) || !shared.RequireProjectsService(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
handleList(s, i)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch data.Options[0].Name {
|
||||||
|
case "list":
|
||||||
|
handleList(s, i)
|
||||||
|
case "create":
|
||||||
|
handleCreate(s, i)
|
||||||
|
case "add-helper":
|
||||||
|
handleAddHelper(s, i)
|
||||||
|
case "close":
|
||||||
|
handleClose(s, i)
|
||||||
|
default:
|
||||||
|
shared.RespondEphemeral(s, i, "Unknown subcommand.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleList(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
guildID, ok := shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const limit = 25
|
||||||
|
items, err := services.Global.Projects.ListActiveWithMembers(context.Background(), guildID, limit)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to load projects.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(items) == 0 {
|
||||||
|
shared.RespondEphemeral(s, i, "There are no active projects at the moment.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Active projects",
|
||||||
|
Description: "",
|
||||||
|
Color: 0x00bcd4,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range items {
|
||||||
|
if item == nil || item.Project == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
p := item.Project
|
||||||
|
name := p.Name
|
||||||
|
if name == "" {
|
||||||
|
name = "Unnamed project"
|
||||||
|
}
|
||||||
|
creatorMention := mentionUser(item.Creator)
|
||||||
|
|
||||||
|
var helpersMentions []string
|
||||||
|
for _, h := range item.Helpers {
|
||||||
|
helpersMentions = append(helpersMentions, mentionUser(h))
|
||||||
|
}
|
||||||
|
helpersText := "None"
|
||||||
|
if len(helpersMentions) > 0 {
|
||||||
|
helpersText = strings.Join(helpersMentions, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
|
desc := "**Creator:** " + creatorMention + "\n" +
|
||||||
|
"**Helpers:** " + helpersText
|
||||||
|
if p.Description != "" {
|
||||||
|
desc += "\n" + p.Description
|
||||||
|
}
|
||||||
|
|
||||||
|
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
|
||||||
|
Name: name,
|
||||||
|
Value: desc,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Embeds: []*discordgo.MessageEmbed{embed},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func mentionUser(id int64) string {
|
||||||
|
if id == 0 {
|
||||||
|
return "Unknown"
|
||||||
|
}
|
||||||
|
return "<@" + strconv.FormatInt(id, 10) + ">"
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleCreate just opens the modal - the actual creation happens on
|
||||||
|
// submit, in HandleModalSubmit (components.go).
|
||||||
|
func handleCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireManageGuild(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseModal,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
CustomID: "projects:create_modal",
|
||||||
|
Title: "Create a project",
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.TextInput{
|
||||||
|
CustomID: "name",
|
||||||
|
Label: "Project name",
|
||||||
|
Style: discordgo.TextInputShort,
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.TextInput{
|
||||||
|
CustomID: "description",
|
||||||
|
Label: "Description (optional)",
|
||||||
|
Style: discordgo.TextInputParagraph,
|
||||||
|
Required: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleAddHelper collects the user via Discord's native user-option (a
|
||||||
|
// modal can't hold one of these), then responds with a select menu of the
|
||||||
|
// guild's active projects instead of making the caller remember/type a
|
||||||
|
// numeric project ID - the actual add-helper call happens on selection, in
|
||||||
|
// HandleComponent (components.go).
|
||||||
|
// handleAddHelper is the first of two dropdown steps: pick a project here,
|
||||||
|
// then HandleComponent (components.go) follows up with a native Discord
|
||||||
|
// user-select to pick who helps, and performs the actual AddHelper call
|
||||||
|
// once both are known.
|
||||||
|
func handleAddHelper(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireManageGuild(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, ok := shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
options, err := activeProjectOptions(context.Background(), guildID)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to load projects.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(options) == 0 {
|
||||||
|
shared.RespondEphemeral(s, i, "There are no active projects to add a helper to.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Which project should get a new helper?",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.SelectMenu{
|
||||||
|
CustomID: "projects:add_helper_project_select",
|
||||||
|
Placeholder: "Select a project",
|
||||||
|
Options: options,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleClose responds with a select menu of the guild's active projects -
|
||||||
|
// same reasoning as handleAddHelper, picking from a live list beats typing
|
||||||
|
// an ID. The actual deactivation happens on selection, in HandleComponent.
|
||||||
|
func handleClose(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireManageGuild(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID, ok := shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
options, err := activeProjectOptions(context.Background(), guildID)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to load projects.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(options) == 0 {
|
||||||
|
shared.RespondEphemeral(s, i, "There are no active projects to close.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Which project should be closed?",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.SelectMenu{
|
||||||
|
CustomID: "projects:close_select",
|
||||||
|
Placeholder: "Select a project",
|
||||||
|
Options: options,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// activeProjectOptions builds select-menu options for the guild's active
|
||||||
|
// projects, truncating name/description to Discord's option limits
|
||||||
|
// (label: 100 chars, description: 100 chars).
|
||||||
|
func activeProjectOptions(ctx context.Context, guildID int64) ([]discordgo.SelectMenuOption, error) {
|
||||||
|
const limit = 25 // Discord's own cap on select menu options
|
||||||
|
items, err := services.Global.Projects.ListActive(ctx, guildID, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
options := make([]discordgo.SelectMenuOption, 0, len(items))
|
||||||
|
for _, p := range items {
|
||||||
|
if p == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name := p.Name
|
||||||
|
if name == "" {
|
||||||
|
name = "Unnamed project"
|
||||||
|
}
|
||||||
|
options = append(options, discordgo.SelectMenuOption{
|
||||||
|
Label: truncate(name, 100),
|
||||||
|
Value: strconv.FormatInt(p.ID, 10),
|
||||||
|
Description: truncate(p.Description, 100),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return options, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func truncate(s string, max int) string {
|
||||||
|
if len(s) <= max {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return s[:max]
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package projects
|
||||||
|
|
||||||
|
import (
|
||||||
|
"velox-bot/internal/commands/projects/public"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Projects *discordgo.ApplicationCommand = public.Projects
|
||||||
|
)
|
||||||
|
|
||||||
|
func ProjectsHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.ProjectsHandler(s, i) }
|
||||||
|
|
||||||
|
func HandleComponent(s *discordgo.Session, i *discordgo.InteractionCreate) { public.HandleComponent(s, i) }
|
||||||
|
|
||||||
|
func HandleModalSubmit(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.HandleModalSubmit(s, i)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package shared
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RespondEphemeral(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: msg,
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequireGuild(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
if i.GuildID == "" {
|
||||||
|
RespondEphemeral(s, i, "This command can only be used in a server.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequireProjectsService(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
if services.Global == nil || services.Global.Projects == nil {
|
||||||
|
RespondEphemeral(s, i, "Projects service is not available.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequireManageGuild(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
if i.Member == nil || (i.Member.Permissions&discordgo.PermissionManageGuild) == 0 {
|
||||||
|
RespondEphemeral(s, i, "You don't have permission to manage projects.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseGuildID(s *discordgo.Session, i *discordgo.InteractionCreate) (int64, bool) {
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
RespondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return guildID, true
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,794 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
"velox-bot/internal/commands/schedule/shared"
|
||||||
|
"velox-bot/internal/db/repos/schedulerepo"
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func scheduleActionButtons(scheduleID int64) []discordgo.MessageComponent {
|
||||||
|
|
||||||
|
idStr := strconv.FormatInt(scheduleID, 10)
|
||||||
|
|
||||||
|
return []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.Button{
|
||||||
|
Label: "Accept",
|
||||||
|
Style: discordgo.SuccessButton,
|
||||||
|
CustomID: "schedule:accept:" + idStr,
|
||||||
|
},
|
||||||
|
discordgo.Button{
|
||||||
|
Label: "Decline",
|
||||||
|
Style: discordgo.DangerButton,
|
||||||
|
CustomID: "schedule:decline:" + idStr,
|
||||||
|
},
|
||||||
|
discordgo.Button{
|
||||||
|
Label: "Reschedule",
|
||||||
|
Style: discordgo.SecondaryButton,
|
||||||
|
CustomID: "schedule:reschedule:" + idStr,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func rescheduleProposalButtons(scheduleID int64) []discordgo.MessageComponent {
|
||||||
|
idStr := strconv.FormatInt(scheduleID, 10)
|
||||||
|
|
||||||
|
return []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.Button{
|
||||||
|
Label: "Accept",
|
||||||
|
Style: discordgo.SuccessButton,
|
||||||
|
CustomID: "schedule:accept_reschedule:" + idStr,
|
||||||
|
},
|
||||||
|
discordgo.Button{
|
||||||
|
Label: "Decline",
|
||||||
|
Style: discordgo.DangerButton,
|
||||||
|
CustomID: "schedule:decline_reschedule:" + idStr,
|
||||||
|
},
|
||||||
|
discordgo.Button{
|
||||||
|
Label: "Propose different time",
|
||||||
|
Style: discordgo.SecondaryButton,
|
||||||
|
CustomID: "schedule:reschedule:" + idStr,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const createUserSelectCustomID = "schedule:create_user_select"
|
||||||
|
|
||||||
|
func HandleComponent(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if i.MessageComponentData().CustomID == createUserSelectCustomID {
|
||||||
|
handleCreateUserSelect(s, i)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
action, scheduleID := parseComponentCustomID(i.MessageComponentData().CustomID)
|
||||||
|
|
||||||
|
userIDStr := interactionUserID(i)
|
||||||
|
userID, err := strconv.ParseInt(userIDStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Error parsing user ID.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sch, err := services.Global.Schedule.GetByID(ctx, scheduleID)
|
||||||
|
if err != nil || sch == nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Error retrieving schedule.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var status schedulerepo.ScheduleStatus
|
||||||
|
var message string
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case "accept":
|
||||||
|
if userID != sch.InviteeID {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "You are not authorized to perform this action.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if sch.Status != schedulerepo.StatusPending {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "This schedule is no longer pending.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
status = schedulerepo.StatusAccepted
|
||||||
|
message = "✅ Accepted"
|
||||||
|
case "decline":
|
||||||
|
if userID != sch.InviteeID {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "You are not authorized to perform this action.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if sch.Status != schedulerepo.StatusPending {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "This schedule is no longer pending.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
status = schedulerepo.StatusDeclined
|
||||||
|
message = "❌ Declined"
|
||||||
|
case "reschedule":
|
||||||
|
if userID != sch.InviteeID && userID != sch.RequesterID {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "You are not authorized to perform this action.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if sch.Status != schedulerepo.StatusPending && sch.Status != schedulerepo.StatusAccepted {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "This schedule can't be rescheduled right now.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseModal,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
CustomID: "schedule:reschedule_modal:" + strconv.FormatInt(scheduleID, 10),
|
||||||
|
Title: "Propose a new time",
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.TextInput{
|
||||||
|
CustomID: "date",
|
||||||
|
Label: "Date (YYYY-MM-DD)",
|
||||||
|
Style: discordgo.TextInputShort,
|
||||||
|
Required: true,
|
||||||
|
Placeholder: "2026-01-02",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.TextInput{
|
||||||
|
CustomID: "time",
|
||||||
|
Label: "Time (HH:MM, your timezone)",
|
||||||
|
Style: discordgo.TextInputShort,
|
||||||
|
Required: true,
|
||||||
|
Placeholder: "15:04",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
case "accept_reschedule":
|
||||||
|
if sch.ProposedBy == nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "There is no reschedule proposal to accept.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userID != sch.RequesterID && userID != sch.InviteeID) || userID == *sch.ProposedBy {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "You are not authorized to perform this action.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if sch.Status != schedulerepo.StatusRescheduleRequested {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "There is no reschedule request to accept.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = services.Global.Schedule.AcceptReschedule(ctx, scheduleID)
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Error accepting reschedule.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseUpdateMessage,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "✅ Reschedule accepted",
|
||||||
|
Components: []discordgo.MessageComponent{},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
newTime := *sch.ProposedAt
|
||||||
|
utcStr := newTime.UTC().Format("2006-01-02 15:04")
|
||||||
|
|
||||||
|
loc, zone, _, err := services.Global.UserSettings.GetTimezone(ctx, *sch.ProposedBy)
|
||||||
|
if err != nil {
|
||||||
|
loc = time.UTC
|
||||||
|
zone = "UTC"
|
||||||
|
}
|
||||||
|
localStr := newTime.In(loc).Format("2006-01-02 15:04")
|
||||||
|
|
||||||
|
content := "Your proposed time for the session was accepted: " +
|
||||||
|
utcStr + " UTC (" + localStr + " " + zone + ")."
|
||||||
|
|
||||||
|
dmCh, err := s.UserChannelCreate(strconv.FormatInt(*sch.ProposedBy, 10))
|
||||||
|
if err == nil {
|
||||||
|
if _, err := s.ChannelMessageSend(dmCh.ID, content); err != nil {
|
||||||
|
log.Printf("schedule: failed to DM proposer about accepted reschedule: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case "decline_reschedule":
|
||||||
|
if sch.ProposedBy == nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "There is no reschedule proposal to accept.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userID != sch.RequesterID && userID != sch.InviteeID) || userID == *sch.ProposedBy {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "You are not authorized to perform this action.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if sch.Status != schedulerepo.StatusRescheduleRequested {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "There is no reschedule request to accept.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = services.Global.Schedule.DeclineReschedule(ctx, scheduleID)
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Error declining reschedule.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseUpdateMessage,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "❌ Reschedule declined",
|
||||||
|
Components: []discordgo.MessageComponent{},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
utcStr := sch.ScheduledAt.UTC().Format("2006-01-02 15:04")
|
||||||
|
|
||||||
|
loc, zone, _, err := services.Global.UserSettings.GetTimezone(ctx, *sch.ProposedBy)
|
||||||
|
if err != nil {
|
||||||
|
loc = time.UTC
|
||||||
|
zone = "UTC"
|
||||||
|
}
|
||||||
|
localStr := sch.ScheduledAt.In(loc).Format("2006-01-02 15:04")
|
||||||
|
|
||||||
|
content := "Your proposed new time was declined. The session remains scheduled for " +
|
||||||
|
utcStr + " UTC (" + localStr + " " + zone + ")."
|
||||||
|
|
||||||
|
dmCh, err := s.UserChannelCreate(strconv.FormatInt(*sch.ProposedBy, 10))
|
||||||
|
if err == nil {
|
||||||
|
if _, err := s.ChannelMessageSend(dmCh.ID, content); err != nil {
|
||||||
|
log.Printf("schedule: failed to DM proposer about declined reschedule: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Unknown action.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = services.Global.Schedule.UpdateStatus(ctx, scheduleID, status)
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Error updating schedule.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseUpdateMessage,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: message,
|
||||||
|
Components: []discordgo.MessageComponent{},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
statusText := map[schedulerepo.ScheduleStatus]string{
|
||||||
|
schedulerepo.StatusAccepted: "accepted ✅",
|
||||||
|
schedulerepo.StatusDeclined: "declined ❌",
|
||||||
|
schedulerepo.StatusRescheduleRequested: "requested rescheduling 🔁",
|
||||||
|
}[status]
|
||||||
|
|
||||||
|
utcStr := sch.ScheduledAt.UTC().Format("2006-01-02 15:04")
|
||||||
|
|
||||||
|
// Notify requester with local time if configured.
|
||||||
|
if services.Global.UserSettings != nil {
|
||||||
|
loc, zone, _, err := services.Global.UserSettings.GetTimezone(ctx, sch.RequesterID)
|
||||||
|
if err != nil {
|
||||||
|
loc = sch.ScheduledAt.UTC().Location()
|
||||||
|
zone = "UTC"
|
||||||
|
}
|
||||||
|
localStr := sch.ScheduledAt.In(loc).Format("2006-01-02 15:04")
|
||||||
|
content := "Your session request with <@" + strconv.FormatInt(sch.InviteeID, 10) + "> for " +
|
||||||
|
utcStr + " UTC (" + localStr + " " + zone + ") has been " + statusText + "."
|
||||||
|
|
||||||
|
requesterID := strconv.FormatInt(sch.RequesterID, 10)
|
||||||
|
dmCh, err := s.UserChannelCreate(requesterID)
|
||||||
|
if err == nil {
|
||||||
|
if _, err := s.ChannelMessageSend(dmCh.ID, content); err != nil {
|
||||||
|
log.Printf("schedule: failed to DM requester about status change: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCreateUserSelect(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
values := i.MessageComponentData().Values
|
||||||
|
if len(values) == 0 {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "No user selected.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
targetUserID := values[0]
|
||||||
|
|
||||||
|
if targetUserID == interactionUserID(i) {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "You cannot schedule a session with yourself.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseModal,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
CustomID: "schedule:create_modal:" + targetUserID,
|
||||||
|
Title: "Schedule a session",
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.TextInput{
|
||||||
|
CustomID: "date",
|
||||||
|
Label: "Date (YYYY-MM-DD)",
|
||||||
|
Style: discordgo.TextInputShort,
|
||||||
|
Required: true,
|
||||||
|
Placeholder: "2026-01-02",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.TextInput{
|
||||||
|
CustomID: "time",
|
||||||
|
Label: "Time (HH:MM, your timezone)",
|
||||||
|
Style: discordgo.TextInputShort,
|
||||||
|
Required: true,
|
||||||
|
Placeholder: "15:04",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.TextInput{
|
||||||
|
CustomID: "description",
|
||||||
|
Label: "Description (optional)",
|
||||||
|
Style: discordgo.TextInputShort,
|
||||||
|
Required: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func modalTextInputValues(components []discordgo.MessageComponent) map[string]string {
|
||||||
|
values := make(map[string]string)
|
||||||
|
for _, c := range components {
|
||||||
|
row, ok := c.(*discordgo.ActionsRow)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, rc := range row.Components {
|
||||||
|
ti, ok := rc.(*discordgo.TextInput)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
values[ti.CustomID] = ti.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCreateModalSubmit(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate, inviteeID int64) {
|
||||||
|
guildID, ok := shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
requesterID, err := strconv.ParseInt(interactionUserID(i), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Invalid user ID.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if requesterID == inviteeID {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "You cannot schedule a session with yourself.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
values := modalTextInputValues(i.ModalSubmitData().Components)
|
||||||
|
dateStr := strings.TrimSpace(values["date"])
|
||||||
|
timeStr := strings.TrimSpace(values["time"])
|
||||||
|
desc := strings.TrimSpace(values["description"])
|
||||||
|
|
||||||
|
loc, zone, _, err := services.Global.UserSettings.GetTimezone(ctx, requesterID)
|
||||||
|
if err != nil {
|
||||||
|
loc = time.UTC
|
||||||
|
zone = "UTC"
|
||||||
|
}
|
||||||
|
|
||||||
|
when, err := shared.ParseScheduleDatetime(dateStr+" "+timeStr, loc)
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Invalid date/time. Use YYYY-MM-DD for the date and HH:MM for the time, in your timezone (" + zone + ").",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
when = when.In(time.UTC)
|
||||||
|
|
||||||
|
scheduleID, err := services.Global.Schedule.CreateSchedule(ctx, guildID, requesterID, inviteeID, when, desc)
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Failed to create schedule.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dmCh, err := s.UserChannelCreate(strconv.FormatInt(inviteeID, 10))
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Session created, but failed to DM the invited user (are DMs disabled?).",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "New session request",
|
||||||
|
Description: fmt.Sprintf("You have been invited to a session by <@%d>.", requesterID),
|
||||||
|
Color: 0x4caf50,
|
||||||
|
}
|
||||||
|
utcStr := when.Format("2006-01-02 15:04")
|
||||||
|
locInv, zoneInv, _, err := services.Global.UserSettings.GetTimezone(ctx, inviteeID)
|
||||||
|
if err != nil {
|
||||||
|
locInv = time.UTC
|
||||||
|
zoneInv = "UTC"
|
||||||
|
}
|
||||||
|
localInvStr := when.In(locInv).Format("2006-01-02 15:04")
|
||||||
|
embed.Fields = []*discordgo.MessageEmbedField{
|
||||||
|
{Name: "When (UTC)", Value: utcStr},
|
||||||
|
{Name: "When (" + zoneInv + ")", Value: localInvStr},
|
||||||
|
}
|
||||||
|
if desc != "" {
|
||||||
|
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
|
||||||
|
Name: "Description",
|
||||||
|
Value: desc,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = s.ChannelMessageSendComplex(dmCh.ID, &discordgo.MessageSend{
|
||||||
|
Content: "Respond to this session request:",
|
||||||
|
Embed: embed,
|
||||||
|
Components: scheduleActionButtons(scheduleID),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Session created, but failed to DM the invited user.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Session request created and sent to <@" + strconv.FormatInt(inviteeID, 10) + ">.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleModalSubmit(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
ctx := context.Background()
|
||||||
|
action, idOrUserID := parseComponentCustomID(i.ModalSubmitData().CustomID)
|
||||||
|
|
||||||
|
if action == "create_modal" {
|
||||||
|
handleCreateModalSubmit(ctx, s, i, idOrUserID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduleID := idOrUserID
|
||||||
|
if scheduleID == 0 {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Invalid schedule ID.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
modalValues := modalTextInputValues(i.ModalSubmitData().Components)
|
||||||
|
datetimeInput := strings.TrimSpace(modalValues["date"]) + " " + strings.TrimSpace(modalValues["time"])
|
||||||
|
sch, err := services.Global.Schedule.GetByID(ctx, scheduleID)
|
||||||
|
if err != nil || sch == nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Error retrieving schedule.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userIDStr := interactionUserID(i)
|
||||||
|
userID, err := strconv.ParseInt(userIDStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Error parsing user ID.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if userID != sch.InviteeID && userID != sch.RequesterID {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "You are not authorized to perform this action.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
loc, _, _, err := services.Global.UserSettings.GetTimezone(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
loc = sch.ScheduledAt.UTC().Location()
|
||||||
|
}
|
||||||
|
|
||||||
|
newTime, err := shared.ParseScheduleDatetime(datetimeInput, loc)
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Invalid date/time. Use YYYY-MM-DD for the date and HH:MM for the time.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//Convert time to UTC before storing in the database
|
||||||
|
newTime = newTime.UTC()
|
||||||
|
|
||||||
|
err = services.Global.Schedule.ProposeReschedule(ctx, scheduleID, newTime, userID)
|
||||||
|
if err != nil {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Error proposing reschedule.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
otherID := sch.RequesterID
|
||||||
|
if userID == sch.RequesterID {
|
||||||
|
otherID = sch.InviteeID
|
||||||
|
}
|
||||||
|
|
||||||
|
dmCh, err := s.UserChannelCreate(strconv.FormatInt(otherID, 10))
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to DM the invited user (are DMs disabled?).")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Reschedule Proposal",
|
||||||
|
Description: fmt.Sprintf("<@%d> has proposed a new time for your session request. Please respond.", userID),
|
||||||
|
Color: 0x4caf50,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show both UTC and invitee-local time if available.
|
||||||
|
utcStr := newTime.Format("2006-01-02 15:04")
|
||||||
|
locInv, zoneInv, _, err := services.Global.UserSettings.GetTimezone(context.Background(), otherID)
|
||||||
|
if err != nil {
|
||||||
|
locInv = time.UTC
|
||||||
|
zoneInv = "UTC"
|
||||||
|
}
|
||||||
|
localInvStr := newTime.In(locInv).Format("2006-01-02 15:04")
|
||||||
|
embed.Fields = []*discordgo.MessageEmbedField{
|
||||||
|
{
|
||||||
|
Name: "When (UTC)",
|
||||||
|
Value: utcStr,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "When (" + zoneInv + ")",
|
||||||
|
Value: localInvStr,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = s.ChannelMessageSendComplex(dmCh.ID, &discordgo.MessageSend{
|
||||||
|
Content: "A new time has been proposed for your session:",
|
||||||
|
Embed: embed,
|
||||||
|
Components: rescheduleProposalButtons(scheduleID),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to DM the other participant.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
shared.RespondEphemeral(s, i, "Proposed new time sent to <@"+strconv.FormatInt(otherID, 10)+">.")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseComponentCustomID(customID string) (string, int64) {
|
||||||
|
parts := strings.Split(customID, ":")
|
||||||
|
if len(parts) != 3 {
|
||||||
|
return "", 0
|
||||||
|
}
|
||||||
|
|
||||||
|
action := parts[1]
|
||||||
|
scheduleID, err := strconv.ParseInt(parts[2], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return "", 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return action, scheduleID
|
||||||
|
}
|
||||||
|
|
||||||
|
func interactionUserID(i *discordgo.InteractionCreate) string {
|
||||||
|
if i.Member != nil && i.Member.User != nil {
|
||||||
|
return i.Member.User.ID
|
||||||
|
}
|
||||||
|
if i.User != nil {
|
||||||
|
return i.User.ID
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
@@ -0,0 +1,289 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/commands/schedule/shared"
|
||||||
|
"velox-bot/internal/db/repos/schedulerepo"
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Schedule = &discordgo.ApplicationCommand{
|
||||||
|
Name: "schedule",
|
||||||
|
Description: "Schedule 1:1 sessions",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "create",
|
||||||
|
Description: "Create a new session with a user",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "list",
|
||||||
|
Description: "List your upcoming sessions",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "reschedule",
|
||||||
|
Description: "Reschedule an existing session",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionInteger,
|
||||||
|
Name: "id",
|
||||||
|
Description: "ID of the session (from /schedule list)",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "datetime",
|
||||||
|
Description: "New datetime (UTC), format: 2006-01-02 15:04",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func ScheduleHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireGuild(s, i) || !shared.RequireScheduleService(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
handleList(s, i)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch data.Options[0].Name {
|
||||||
|
case "create":
|
||||||
|
handleCreate(s, i)
|
||||||
|
case "list":
|
||||||
|
handleList(s, i)
|
||||||
|
case "reschedule":
|
||||||
|
handleReschedule(s, i)
|
||||||
|
default:
|
||||||
|
shared.RespondEphemeral(s, i, "Unknown subcommand.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if _, ok := shared.ParseGuildID(s, i); !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i.Member == nil || i.Member.User == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing member info.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Who do you want to schedule a session with?",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.SelectMenu{
|
||||||
|
MenuType: discordgo.UserSelectMenu,
|
||||||
|
CustomID: createUserSelectCustomID,
|
||||||
|
Placeholder: "Select a user",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleList(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
guildID, ok := shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i.Member == nil || i.Member.User == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing member info.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userID, err := strconv.ParseInt(i.Member.User.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid user ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const limit = 10
|
||||||
|
items, err := services.Global.Schedule.ListForUser(context.Background(), guildID, userID, limit)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to load your sessions.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(items) == 0 {
|
||||||
|
shared.RespondEphemeral(s, i, "You have no upcoming sessions.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
for _, sch := range items {
|
||||||
|
role := "Requester"
|
||||||
|
otherID := sch.InviteeID
|
||||||
|
if sch.InviteeID == userID {
|
||||||
|
role = "Invitee"
|
||||||
|
otherID = sch.RequesterID
|
||||||
|
}
|
||||||
|
status := string(sch.Status)
|
||||||
|
whenStr := sch.ScheduledAt.UTC().Format("2006-01-02 15:04")
|
||||||
|
line := fmt.Sprintf("ID `%d` • %s with <@%d> at %s UTC (%s)\n", sch.ID, role, otherID, whenStr, status)
|
||||||
|
if sch.Description != "" {
|
||||||
|
line += " " + sch.Description + "\n"
|
||||||
|
}
|
||||||
|
b.WriteString(line)
|
||||||
|
}
|
||||||
|
|
||||||
|
shared.RespondEphemeral(s, i, b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleReschedule(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
guildID, ok := shared.ParseGuildID(s, i)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i.Member == nil || i.Member.User == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing member info.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing options.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
opt := data.Options[0]
|
||||||
|
|
||||||
|
var (
|
||||||
|
idVal int64
|
||||||
|
whenStr string
|
||||||
|
)
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
switch o.Name {
|
||||||
|
case "id":
|
||||||
|
idVal = o.IntValue()
|
||||||
|
case "datetime":
|
||||||
|
whenStr = o.StringValue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if idVal <= 0 {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid session ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
whenStr = strings.TrimSpace(whenStr)
|
||||||
|
if whenStr == "" {
|
||||||
|
shared.RespondEphemeral(s, i, "Datetime cannot be empty.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userID, err := strconv.ParseInt(i.Member.User.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid user ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
sch, err := services.Global.Schedule.GetByID(ctx, idVal)
|
||||||
|
if err != nil || sch == nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Session not found.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if sch.GuildID != guildID {
|
||||||
|
shared.RespondEphemeral(s, i, "This session belongs to another server.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if userID != sch.RequesterID && userID != sch.InviteeID {
|
||||||
|
shared.RespondEphemeral(s, i, "You are not part of this session.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if sch.Status != schedulerepo.StatusPending && sch.Status != schedulerepo.StatusAccepted {
|
||||||
|
shared.RespondEphemeral(s, i, "This session can't be rescheduled right now.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use rescheduler's timezone for parsing.
|
||||||
|
loc, zone, _, err := services.Global.UserSettings.GetTimezone(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
loc = time.UTC
|
||||||
|
zone = "UTC"
|
||||||
|
}
|
||||||
|
// Expect explicit layout "2006-01-02 15:04" in user's timezone.
|
||||||
|
when, err := shared.ParseScheduleDatetime(whenStr, loc)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, err.Error()+" (use `2006-01-02 15:04` in your timezone: "+zone+")")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
when = when.In(time.UTC)
|
||||||
|
|
||||||
|
if err := services.Global.Schedule.ProposeReschedule(ctx, sch.ID, when, userID); err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to propose reschedule.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
whenFmt := when.Format("2006-01-02 15:04")
|
||||||
|
|
||||||
|
otherID := sch.InviteeID
|
||||||
|
if userID == sch.InviteeID {
|
||||||
|
otherID = sch.RequesterID
|
||||||
|
}
|
||||||
|
|
||||||
|
dmCh, err := s.UserChannelCreate(strconv.FormatInt(otherID, 10))
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Proposal saved, but failed to DM the other participant (are DMs disabled?).")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Reschedule proposed",
|
||||||
|
Description: fmt.Sprintf("<@%s> has proposed a new time for your session.", i.Member.User.ID),
|
||||||
|
Color: 0x4caf50,
|
||||||
|
}
|
||||||
|
// Show both UTC and the other participant's local time if available.
|
||||||
|
locOther, zoneOther, _, errTZ := services.Global.UserSettings.GetTimezone(ctx, otherID)
|
||||||
|
if errTZ != nil {
|
||||||
|
locOther = time.UTC
|
||||||
|
zoneOther = "UTC"
|
||||||
|
}
|
||||||
|
localOtherStr := when.In(locOther).Format("2006-01-02 15:04")
|
||||||
|
embed.Fields = []*discordgo.MessageEmbedField{
|
||||||
|
{
|
||||||
|
Name: "When (UTC)",
|
||||||
|
Value: whenFmt,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "When (" + zoneOther + ")",
|
||||||
|
Value: localOtherStr,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if sch.Description != "" {
|
||||||
|
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
|
||||||
|
Name: "Description",
|
||||||
|
Value: sch.Description,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = s.ChannelMessageSendComplex(dmCh.ID, &discordgo.MessageSend{
|
||||||
|
Content: "A new time has been proposed for your session:",
|
||||||
|
Embed: embed,
|
||||||
|
Components: rescheduleProposalButtons(sch.ID),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Proposal saved, but failed to DM the other participant.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
shared.RespondEphemeral(s, i, "Proposed new time sent to <@"+strconv.FormatInt(otherID, 10)+">.")
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package schedule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"velox-bot/internal/commands/schedule/public"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Schedule *discordgo.ApplicationCommand = public.Schedule
|
||||||
|
)
|
||||||
|
|
||||||
|
func ScheduleHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.ScheduleHandler(s, i)
|
||||||
|
}
|
||||||
|
func HandleComponent(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.HandleComponent(s, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleModalSubmit(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.HandleModalSubmit(s, i)
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package shared
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var scheduleDatetimeLayouts = []string{
|
||||||
|
"2006-01-02 15:04",
|
||||||
|
"2006-01-02T15:04",
|
||||||
|
"2006-01-02 15:04:05",
|
||||||
|
"01/02/2006 15:04",
|
||||||
|
}
|
||||||
|
|
||||||
|
func RespondEphemeral(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: msg,
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseScheduleDatetime(input string, loc *time.Location) (time.Time, error) {
|
||||||
|
for _, layout := range scheduleDatetimeLayouts {
|
||||||
|
if t, err := time.ParseInLocation(layout, input, loc); err == nil {
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return time.Time{}, fmt.Errorf("invalid schedule datetime format")
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequireGuild(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
if i.GuildID == "" {
|
||||||
|
RespondEphemeral(s, i, "This command can only be used in a server.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequireScheduleService(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
if services.Global == nil || services.Global.Schedule == nil {
|
||||||
|
RespondEphemeral(s, i, "Scheduling service is not available.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseGuildID(s *discordgo.Session, i *discordgo.InteractionCreate) (int64, bool) {
|
||||||
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
RespondEphemeral(s, i, "Invalid guild ID.")
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return guildID, true
|
||||||
|
}
|
||||||
@@ -0,0 +1,575 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
"velox-bot/internal/security"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Every handler re-checks permissions through the security engine. The
|
||||||
|
// DefaultMemberPermissions below only hide commands from the menu; server
|
||||||
|
// admins can change them, so they are never what keeps anyone out.
|
||||||
|
|
||||||
|
var (
|
||||||
|
manageGuild = int64(discordgo.PermissionManageGuild)
|
||||||
|
manageChannel = int64(discordgo.PermissionManageChannels)
|
||||||
|
guildOnly = &[]discordgo.InteractionContextType{discordgo.InteractionContextGuild}
|
||||||
|
textChannels = []discordgo.ChannelType{discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews}
|
||||||
|
chatChannels = []discordgo.ChannelType{
|
||||||
|
discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews, discordgo.ChannelTypeGuildForum,
|
||||||
|
discordgo.ChannelTypeGuildVoice, discordgo.ChannelTypeGuildStageVoice,
|
||||||
|
}
|
||||||
|
opTimeout = 3 * time.Minute
|
||||||
|
genericFailed = "Something went wrong. Check that Velox's role is high enough and has the permissions it needs, then try again."
|
||||||
|
)
|
||||||
|
|
||||||
|
var featureChoices = []*discordgo.ApplicationCommandOptionChoice{
|
||||||
|
{Name: "Join Gate", Value: securityrepo.FeatureJoinGate},
|
||||||
|
{Name: "Raid mode", Value: securityrepo.FeatureAntiRaid},
|
||||||
|
{Name: "Anti-nuke", Value: securityrepo.FeatureAntiNuke},
|
||||||
|
{Name: "Heat (anti-spam)", Value: securityrepo.FeatureHeat},
|
||||||
|
}
|
||||||
|
|
||||||
|
var Security = &discordgo.ApplicationCommand{
|
||||||
|
Name: "security",
|
||||||
|
Description: "Anti-raid and anti-nuke protection",
|
||||||
|
DefaultMemberPermissions: &manageGuild,
|
||||||
|
Contexts: guildOnly,
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{Type: discordgo.ApplicationCommandOptionSubCommand, Name: "status", Description: "Show what's on and who is trusted"},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand, Name: "toggle", Description: "Turn a protection on or off",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{Type: discordgo.ApplicationCommandOptionString, Name: "feature", Description: "Which protection", Required: true, Choices: featureChoices},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionBoolean, Name: "on", Description: "On or off", Required: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand, Name: "alerts", Description: "Where alerts go and which role they ping (owner or trusted)",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{Type: discordgo.ApplicationCommandOptionChannel, Name: "channel", Description: "Alert channel", Required: true, ChannelTypes: textChannels},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionRole, Name: "role", Description: "Role to ping for raids and nukes"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand, Name: "quarantine-role", Description: "Role given to quarantined members (owner or trusted)",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{Type: discordgo.ApplicationCommandOptionRole, Name: "role", Description: "Leave empty to use none"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommandGroup, Name: "trust", Description: "Who is exempt from anti-nuke (owner only)",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand, Name: "add", Description: "Trust a user or a role",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{Type: discordgo.ApplicationCommandOptionUser, Name: "user", Description: "A user"},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionRole, Name: "role", Description: "A role"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand, Name: "remove", Description: "Stop trusting a user or a role",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{Type: discordgo.ApplicationCommandOptionUser, Name: "user", Description: "A user"},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionRole, Name: "role", Description: "A role"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand, Name: "release", Description: "Release a quarantined member (owner or trusted)",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{Type: discordgo.ApplicationCommandOptionUser, Name: "user", Description: "Who to release", Required: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionSubCommand, Name: "quarantined", Description: "List quarantined members"},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionSubCommand, Name: "end-raid", Description: "End raid mode now (owner or trusted)"},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionSubCommand, Name: "snapshot", Description: "Save a snapshot of roles and channels to restore from"},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionSubCommand, Name: "restore", Description: "Recreate deleted roles and channels from the latest snapshot (owner or trusted)"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func lockOptions(unlock bool) []*discordgo.ApplicationCommandOption {
|
||||||
|
verb := "Lock"
|
||||||
|
if unlock {
|
||||||
|
verb = "Unlock"
|
||||||
|
}
|
||||||
|
joins := &discordgo.ApplicationCommandOption{Type: discordgo.ApplicationCommandOptionSubCommand, Name: "joins", Description: verb + " joins"}
|
||||||
|
if !unlock {
|
||||||
|
joins.Description = "Kick or ban everyone who joins until unlocked"
|
||||||
|
joins.Options = []*discordgo.ApplicationCommandOption{{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString, Name: "action", Description: "What happens to new members", Required: true,
|
||||||
|
Choices: []*discordgo.ApplicationCommandOptionChoice{{Name: "Kick", Value: securityrepo.ActionKick}, {Name: "Ban", Value: securityrepo.ActionBan}},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
return []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand, Name: "channel", Description: verb + " one channel",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{{Type: discordgo.ApplicationCommandOptionChannel, Name: "channel", Description: "Defaults to this channel", ChannelTypes: chatChannels}},
|
||||||
|
},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionSubCommand, Name: "server", Description: verb + " every channel"},
|
||||||
|
joins,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var Lockdown = &discordgo.ApplicationCommand{
|
||||||
|
Name: "lockdown", Description: "Stop members from talking, or from joining", DefaultMemberPermissions: &manageChannel,
|
||||||
|
Contexts: guildOnly, Options: lockOptions(false),
|
||||||
|
}
|
||||||
|
|
||||||
|
var Unlock = &discordgo.ApplicationCommand{
|
||||||
|
Name: "unlock", Description: "Undo a lockdown (owner or trusted)", DefaultMemberPermissions: &manageChannel,
|
||||||
|
Contexts: guildOnly, Options: lockOptions(true),
|
||||||
|
}
|
||||||
|
|
||||||
|
func reply(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{Content: msg, Flags: discordgo.MessageFlagsEphemeral, AllowedMentions: &discordgo.MessageAllowedMentions{}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func deferReply(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
return s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{Flags: discordgo.MessageFlagsEphemeral},
|
||||||
|
}) == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func edit(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Content: &msg, AllowedMentions: &discordgo.MessageAllowedMentions{}})
|
||||||
|
}
|
||||||
|
|
||||||
|
func message(where string, err error) string {
|
||||||
|
var ue security.UserError
|
||||||
|
if errors.As(err, &ue) {
|
||||||
|
return string(ue)
|
||||||
|
}
|
||||||
|
log.Printf("security command %s: %v", where, err)
|
||||||
|
return genericFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
func opts(o []*discordgo.ApplicationCommandInteractionDataOption) map[string]*discordgo.ApplicationCommandInteractionDataOption {
|
||||||
|
m := map[string]*discordgo.ApplicationCommandInteractionDataOption{}
|
||||||
|
for _, x := range o {
|
||||||
|
m[x.Name] = x
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func perms(i *discordgo.InteractionCreate) int64 {
|
||||||
|
if i.Member == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return i.Member.Permissions
|
||||||
|
}
|
||||||
|
|
||||||
|
func userID(i *discordgo.InteractionCreate) string {
|
||||||
|
if i.Member == nil || i.Member.User == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return i.Member.User.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
func engine(s *discordgo.Session, i *discordgo.InteractionCreate) *security.Engine {
|
||||||
|
e := security.Default()
|
||||||
|
if e == nil || i.GuildID == "" || i.Member == nil {
|
||||||
|
reply(s, i, "Security isn't available right now.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
// allowedOrTrusted lets an action through when the member has one of the
|
||||||
|
// permissions, or is the owner or trusted.
|
||||||
|
func allowedOrTrusted(ctx context.Context, e *security.Engine, i *discordgo.InteractionCreate, need int64) bool {
|
||||||
|
if perms(i)&(need|discordgo.PermissionAdministrator) != 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return e.RequireTrusted(ctx, i.GuildID, i.Member) == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SecurityHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
e := engine(s, i)
|
||||||
|
if e == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sub := data.Options[0]
|
||||||
|
args := opts(sub.Options)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
switch sub.Name {
|
||||||
|
case "status":
|
||||||
|
if !allowedOrTrusted(ctx, e, i, discordgo.PermissionManageGuild) {
|
||||||
|
reply(s, i, "You need Manage Server to see the security settings.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reply(s, i, status(ctx, e, i.GuildID))
|
||||||
|
case "toggle":
|
||||||
|
feature, on := args["feature"].StringValue(), args["on"].BoolValue()
|
||||||
|
if err := e.SetFeature(ctx, i.GuildID, i.Member, perms(i), feature, on); err != nil {
|
||||||
|
reply(s, i, message("toggle", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
state := "off"
|
||||||
|
if on {
|
||||||
|
state = "on"
|
||||||
|
}
|
||||||
|
reply(s, i, fmt.Sprintf("%s is now **%s**.", featureName(feature), state))
|
||||||
|
case "alerts":
|
||||||
|
ch := args["channel"].ChannelValue(nil).ID
|
||||||
|
role := ""
|
||||||
|
if r, ok := args["role"]; ok {
|
||||||
|
role = r.RoleValue(nil, i.GuildID).ID
|
||||||
|
}
|
||||||
|
if err := e.SetAlerts(ctx, i.GuildID, i.Member, ch, role); err != nil {
|
||||||
|
reply(s, i, message("alerts", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reply(s, i, fmt.Sprintf("Alerts will go to <#%s>.", ch))
|
||||||
|
case "quarantine-role":
|
||||||
|
role := ""
|
||||||
|
if r, ok := args["role"]; ok {
|
||||||
|
role = r.RoleValue(nil, i.GuildID).ID
|
||||||
|
}
|
||||||
|
if err := e.SetQuarantineRole(ctx, i.GuildID, i.Member, role); err != nil {
|
||||||
|
reply(s, i, message("quarantine-role", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if role == "" {
|
||||||
|
reply(s, i, "Quarantined members won't get a role; they still lose their roles and are timed out.")
|
||||||
|
} else {
|
||||||
|
reply(s, i, fmt.Sprintf("Quarantined members will get <@&%s>.", role))
|
||||||
|
}
|
||||||
|
case "trust":
|
||||||
|
if len(sub.Options) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
handleTrust(ctx, s, i, e, sub.Options[0])
|
||||||
|
case "release":
|
||||||
|
if err := e.RequireTrusted(ctx, i.GuildID, i.Member); err != nil {
|
||||||
|
reply(s, i, message("release", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
target := args["user"].UserValue(nil)
|
||||||
|
if !deferReply(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := e.Release(ctx, i.GuildID, target.ID, userID(i)); err != nil {
|
||||||
|
edit(s, i, message("release", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
edit(s, i, fmt.Sprintf("<@%s> was released and got their roles back.", target.ID))
|
||||||
|
case "quarantined":
|
||||||
|
if !allowedOrTrusted(ctx, e, i, discordgo.PermissionManageGuild) {
|
||||||
|
reply(s, i, "You need Manage Server to see this.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
qs, err := e.Quarantines(ctx, i.GuildID)
|
||||||
|
if err != nil {
|
||||||
|
reply(s, i, message("quarantined", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(qs) == 0 {
|
||||||
|
reply(s, i, "Nobody is quarantined.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var b strings.Builder
|
||||||
|
for _, q := range qs {
|
||||||
|
fmt.Fprintf(&b, "• <@%d>, <t:%d:R>: %s\n", q.UserID, q.CreatedAt.Unix(), q.Reason)
|
||||||
|
}
|
||||||
|
reply(s, i, clip(b.String(), 1900))
|
||||||
|
case "end-raid":
|
||||||
|
endRaid(ctx, s, i, e)
|
||||||
|
case "snapshot":
|
||||||
|
if !allowedOrTrusted(ctx, e, i, discordgo.PermissionManageGuild) {
|
||||||
|
reply(s, i, "You need Manage Server to take a snapshot.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !deferReply(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
roles, channels, err := e.TakeSnapshot(ctx, i.GuildID, userID(i))
|
||||||
|
if err != nil {
|
||||||
|
edit(s, i, message("snapshot", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
edit(s, i, fmt.Sprintf("Snapshot saved: %d roles and %d channels. The last three are kept.", roles, channels))
|
||||||
|
case "restore":
|
||||||
|
if err := e.RequireTrusted(ctx, i.GuildID, i.Member); err != nil {
|
||||||
|
reply(s, i, message("restore", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !deferReply(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Recreating many channels takes a while under Discord's rate limits.
|
||||||
|
res, err := e.Restore(ctx, i.GuildID, userID(i))
|
||||||
|
if err != nil {
|
||||||
|
edit(s, i, message("restore", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
edit(s, i, fmt.Sprintf("Restored from the snapshot of <t:%d:f>: %d roles and %d channels recreated, %d roles had dangerous permissions reverted, %d steps failed.",
|
||||||
|
res.SnapshotAt.Unix(), res.RolesCreated, res.ChannelsCreated, res.RolesReverted, res.Failed))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleTrust(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate, e *security.Engine, sub *discordgo.ApplicationCommandInteractionDataOption) {
|
||||||
|
args := opts(sub.Options)
|
||||||
|
kind, target := "", ""
|
||||||
|
if u, ok := args["user"]; ok {
|
||||||
|
kind, target = "user", u.UserValue(nil).ID
|
||||||
|
}
|
||||||
|
if r, ok := args["role"]; ok {
|
||||||
|
if target != "" {
|
||||||
|
reply(s, i, "Pick a user or a role, not both.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
kind, target = "role", r.RoleValue(nil, i.GuildID).ID
|
||||||
|
}
|
||||||
|
if target == "" {
|
||||||
|
reply(s, i, "Pick a user or a role.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mention := "<@" + target + ">"
|
||||||
|
if kind == "role" {
|
||||||
|
mention = "<@&" + target + ">"
|
||||||
|
}
|
||||||
|
switch sub.Name {
|
||||||
|
case "add":
|
||||||
|
if err := e.Trust(ctx, i.GuildID, i.Member, kind, target); err != nil {
|
||||||
|
reply(s, i, message("trust add", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reply(s, i, mention+" is now trusted: exempt from anti-nuke and able to change security settings.")
|
||||||
|
case "remove":
|
||||||
|
if err := e.Untrust(ctx, i.GuildID, i.Member, target); err != nil {
|
||||||
|
reply(s, i, message("trust remove", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reply(s, i, mention+" is no longer trusted.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func endRaid(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate, e *security.Engine) {
|
||||||
|
if err := e.RequireTrusted(ctx, i.GuildID, i.Member); err != nil {
|
||||||
|
reply(s, i, message("end raid", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !deferReply(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := e.EndRaid(ctx, i.GuildID, userID(i)); err != nil {
|
||||||
|
edit(s, i, message("end raid", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
edit(s, i, "Raid mode is off.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func featureName(f string) string {
|
||||||
|
switch f {
|
||||||
|
case securityrepo.FeatureJoinGate:
|
||||||
|
return "Join Gate"
|
||||||
|
case securityrepo.FeatureAntiRaid:
|
||||||
|
return "Raid mode"
|
||||||
|
case securityrepo.FeatureAntiNuke:
|
||||||
|
return "Anti-nuke"
|
||||||
|
case securityrepo.FeatureHeat:
|
||||||
|
return "Heat"
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func onOff(b bool) string {
|
||||||
|
if b {
|
||||||
|
return "✅ on"
|
||||||
|
}
|
||||||
|
return "⬜ off"
|
||||||
|
}
|
||||||
|
|
||||||
|
func status(ctx context.Context, e *security.Engine, guildID string) string {
|
||||||
|
st, trusted, err := e.Settings(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
return message("status", err)
|
||||||
|
}
|
||||||
|
var b strings.Builder
|
||||||
|
fmt.Fprintf(&b, "**Join Gate:** %s\n**Raid mode:** %s (%d joins in %ds → %s)\n**Anti-nuke:** %s (auto restore %s)\n**Heat:** %s (%s)\n",
|
||||||
|
onOff(st.JoinGateEnabled), onOff(st.AntiRaidEnabled), st.RaidJoinCount, st.RaidJoinSeconds, st.RaidAction,
|
||||||
|
onOff(st.AntiNukeEnabled), onOff(st.AntiNukeAutoRestore), onOff(st.HeatEnabled), st.HeatStrictness)
|
||||||
|
if e.RaidActive(guildID) {
|
||||||
|
b.WriteString("🚨 **A raid is in progress.**\n")
|
||||||
|
}
|
||||||
|
if st.JoinLock != securityrepo.ActionOff {
|
||||||
|
fmt.Fprintf(&b, "🔒 Joins are locked (%s).\n", st.JoinLock)
|
||||||
|
}
|
||||||
|
if st.AlertChannelID != 0 {
|
||||||
|
fmt.Fprintf(&b, "**Alerts:** <#%d>", st.AlertChannelID)
|
||||||
|
if st.AlertRoleID != 0 {
|
||||||
|
fmt.Fprintf(&b, ", pinging <@&%d>", st.AlertRoleID)
|
||||||
|
}
|
||||||
|
b.WriteString("\n")
|
||||||
|
} else {
|
||||||
|
b.WriteString("**Alerts:** not set. Use `/security alerts`.\n")
|
||||||
|
}
|
||||||
|
if owner, err := e.OwnerID(guildID); err == nil {
|
||||||
|
fmt.Fprintf(&b, "**Trusted:** <@%s> (owner)", owner)
|
||||||
|
}
|
||||||
|
for _, t := range trusted {
|
||||||
|
if t.Kind == "role" {
|
||||||
|
fmt.Fprintf(&b, ", <@&%d>", t.TargetID)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(&b, ", <@%d>", t.TargetID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.WriteString("\nFine-tune filters, limits and actions on the dashboard.")
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func clip(s string, n int) string {
|
||||||
|
if r := []rune(s); len(r) > n {
|
||||||
|
return string(r[:n-1]) + "…"
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func LockdownHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
e := engine(s, i)
|
||||||
|
if e == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sub := data.Options[0]
|
||||||
|
args := opts(sub.Options)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
switch sub.Name {
|
||||||
|
case "channel":
|
||||||
|
if !allowedOrTrusted(ctx, e, i, discordgo.PermissionManageChannels) {
|
||||||
|
reply(s, i, "You need Manage Channels to lock a channel.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ch := i.ChannelID
|
||||||
|
if c, ok := args["channel"]; ok {
|
||||||
|
ch = c.ChannelValue(nil).ID
|
||||||
|
}
|
||||||
|
if err := e.LockChannel(ctx, i.GuildID, ch, userID(i), false); err != nil {
|
||||||
|
reply(s, i, message("lock channel", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reply(s, i, fmt.Sprintf("🔒 <#%s> is locked. `/unlock channel` puts it back as it was.", ch))
|
||||||
|
case "server":
|
||||||
|
if !allowedOrTrusted(ctx, e, i, discordgo.PermissionManageGuild) {
|
||||||
|
reply(s, i, "You need Manage Server to lock the server.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !deferReply(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
locked, failed, err := e.LockServer(ctx, i.GuildID, userID(i))
|
||||||
|
if err != nil {
|
||||||
|
edit(s, i, message("lock server", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
msg := fmt.Sprintf("🔒 Locked %d channels.", locked)
|
||||||
|
if failed > 0 {
|
||||||
|
msg += fmt.Sprintf(" %d couldn't be locked (Velox needs Manage Channels there).", failed)
|
||||||
|
}
|
||||||
|
edit(s, i, msg+" `/unlock server` puts them back as they were.")
|
||||||
|
case "joins":
|
||||||
|
if !allowedOrTrusted(ctx, e, i, discordgo.PermissionManageGuild) {
|
||||||
|
reply(s, i, "You need Manage Server to lock joins.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mode := args["action"].StringValue()
|
||||||
|
if err := e.SetJoinLock(ctx, i.GuildID, mode, userID(i)); err != nil {
|
||||||
|
reply(s, i, message("lock joins", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reply(s, i, fmt.Sprintf("🔒 Everyone who joins will be %s until `/unlock joins`.", map[string]string{"kick": "kicked", "ban": "banned"}[mode]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnlockHandler relaxes protection, so it is owner or trusted only.
|
||||||
|
func UnlockHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
e := engine(s, i)
|
||||||
|
if e == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sub := data.Options[0]
|
||||||
|
args := opts(sub.Options)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
if err := e.RequireTrusted(ctx, i.GuildID, i.Member); err != nil {
|
||||||
|
reply(s, i, message("unlock", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch sub.Name {
|
||||||
|
case "channel":
|
||||||
|
ch := i.ChannelID
|
||||||
|
if c, ok := args["channel"]; ok {
|
||||||
|
ch = c.ChannelValue(nil).ID
|
||||||
|
}
|
||||||
|
if err := e.UnlockChannel(ctx, i.GuildID, ch, userID(i)); err != nil {
|
||||||
|
reply(s, i, message("unlock channel", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reply(s, i, fmt.Sprintf("🔓 <#%s> is unlocked.", ch))
|
||||||
|
case "server":
|
||||||
|
if !deferReply(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
unlocked, failed, err := e.UnlockServer(ctx, i.GuildID, userID(i))
|
||||||
|
if err != nil {
|
||||||
|
edit(s, i, message("unlock server", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
msg := fmt.Sprintf("🔓 Unlocked %d channels.", unlocked)
|
||||||
|
if failed > 0 {
|
||||||
|
msg += fmt.Sprintf(" %d failed; try `/unlock server` again.", failed)
|
||||||
|
}
|
||||||
|
edit(s, i, msg)
|
||||||
|
case "joins":
|
||||||
|
if err := e.SetJoinLock(ctx, i.GuildID, securityrepo.ActionOff, userID(i)); err != nil {
|
||||||
|
reply(s, i, message("unlock joins", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reply(s, i, "🔓 Joins are open again.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleComponent handles the alert's "End raid" button. Anyone in the alert
|
||||||
|
// channel can press it, so it checks trust like the command.
|
||||||
|
func HandleComponent(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
e := engine(s, i)
|
||||||
|
if e == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i.MessageComponentData().CustomID != security.CustomIDEndRaid {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
endRaid(ctx, s, i, e)
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"velox-bot/internal/commands/security/public"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Security *discordgo.ApplicationCommand = public.Security
|
||||||
|
Lockdown *discordgo.ApplicationCommand = public.Lockdown
|
||||||
|
Unlock *discordgo.ApplicationCommand = public.Unlock
|
||||||
|
)
|
||||||
|
|
||||||
|
func SecurityHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.SecurityHandler(s, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LockdownHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.LockdownHandler(s, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnlockHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.UnlockHandler(s, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleComponent(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.HandleComponent(s, i)
|
||||||
|
}
|
||||||
@@ -0,0 +1,542 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/ticketrepo"
|
||||||
|
"velox-bot/internal/tickets"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The session must provide everything the ticket flows call.
|
||||||
|
var _ tickets.Discord = (*discordgo.Session)(nil)
|
||||||
|
|
||||||
|
// Repo is the full ticket store, including the admin calls that only the
|
||||||
|
// slash commands make. Set by Init.
|
||||||
|
var Repo *ticketrepo.Repo
|
||||||
|
|
||||||
|
func Init(r *ticketrepo.Repo) {
|
||||||
|
Repo = r
|
||||||
|
tickets.Init(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
manageGuild = int64(discordgo.PermissionManageGuild)
|
||||||
|
guildOnly = &[]discordgo.InteractionContextType{discordgo.InteractionContextGuild}
|
||||||
|
textChannels = []discordgo.ChannelType{discordgo.ChannelTypeGuildText}
|
||||||
|
opTimeout = 2 * time.Minute
|
||||||
|
genericFailed = "Something went wrong. Try again in a moment, and tell an admin if it keeps happening."
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tickets is the admin command for setting the system up.
|
||||||
|
var Tickets = &discordgo.ApplicationCommand{
|
||||||
|
Name: "tickets",
|
||||||
|
Description: "Set up support tickets",
|
||||||
|
DefaultMemberPermissions: &manageGuild,
|
||||||
|
Contexts: guildOnly,
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "setup",
|
||||||
|
Description: "Choose where tickets are opened and where closed tickets are logged",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{Type: discordgo.ApplicationCommandOptionChannel, Name: "channel", Description: "Where the panel goes and ticket threads are created", Required: true, ChannelTypes: textChannels},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionChannel, Name: "log-channel", Description: "Where closed tickets and their transcripts are posted", ChannelTypes: textChannels},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionRole, Name: "staff-role", Description: "Creates a Support category handled by this role, if there are no categories yet"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
|
||||||
|
Name: "category",
|
||||||
|
Description: "Manage ticket categories",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "add",
|
||||||
|
Description: "Add a category (an option in the panel's dropdown)",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{Type: discordgo.ApplicationCommandOptionString, Name: "name", Description: "e.g. Support, Report, Appeal", Required: true, MaxLength: 40},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionRole, Name: "staff-role", Description: "Who handles these tickets"},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionString, Name: "emoji", Description: "Shown on the button", MaxLength: 64},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionString, Name: "description", Description: "Shown on the panel", MaxLength: 200},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "remove",
|
||||||
|
Description: "Remove a category",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{Type: discordgo.ApplicationCommandOptionString, Name: "name", Description: "The category's name", Required: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionSubCommand, Name: "list", Description: "Show the categories"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "panel",
|
||||||
|
Description: "Post the ticket panel, or update it after changing categories",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ticket is used inside a ticket thread.
|
||||||
|
var Ticket = &discordgo.ApplicationCommand{
|
||||||
|
Name: "ticket",
|
||||||
|
Description: "Manage the ticket you're in",
|
||||||
|
Contexts: guildOnly,
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand, Name: "add", Description: "Add someone to this ticket",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{{Type: discordgo.ApplicationCommandOptionUser, Name: "user", Description: "Who to add", Required: true}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand, Name: "remove", Description: "Remove someone from this ticket (staff only)",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{{Type: discordgo.ApplicationCommandOptionUser, Name: "user", Description: "Who to remove", Required: true}},
|
||||||
|
},
|
||||||
|
{Type: discordgo.ApplicationCommandOptionSubCommand, Name: "claim", Description: "Take this ticket (staff only)"},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand, Name: "close", Description: "Close this ticket",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{{Type: discordgo.ApplicationCommandOptionString, Name: "reason", Description: "Why it's being closed", MaxLength: 300}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func reply(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{Content: msg, Flags: discordgo.MessageFlagsEphemeral, AllowedMentions: &discordgo.MessageAllowedMentions{}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func deferReply(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{Flags: discordgo.MessageFlagsEphemeral},
|
||||||
|
})
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func edit(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_, _ = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Content: &msg, AllowedMentions: &discordgo.MessageAllowedMentions{}})
|
||||||
|
}
|
||||||
|
|
||||||
|
// message turns an error into what the user sees; unexpected ones are logged.
|
||||||
|
func message(where string, err error) string {
|
||||||
|
var ue tickets.UserError
|
||||||
|
if errors.As(err, &ue) {
|
||||||
|
return string(ue)
|
||||||
|
}
|
||||||
|
log.Printf("tickets: %s: %v", where, err)
|
||||||
|
return genericFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
func actor(i *discordgo.InteractionCreate) tickets.Actor {
|
||||||
|
a := tickets.Actor{Member: i.Member}
|
||||||
|
if i.Member != nil {
|
||||||
|
a.Perms = i.Member.Permissions
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func guildID(i *discordgo.InteractionCreate) int64 {
|
||||||
|
n, _ := strconv.ParseInt(i.GuildID, 10, 64)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func TicketsHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if Repo == nil {
|
||||||
|
reply(s, i, "Tickets aren't available right now.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sub := data.Options[0]
|
||||||
|
switch sub.Name {
|
||||||
|
case "setup":
|
||||||
|
handleSetup(s, i, sub.Options)
|
||||||
|
case "panel":
|
||||||
|
handlePanel(s, i)
|
||||||
|
case "category":
|
||||||
|
if len(sub.Options) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch sub.Options[0].Name {
|
||||||
|
case "add":
|
||||||
|
handleCategoryAdd(s, i, sub.Options[0].Options)
|
||||||
|
case "remove":
|
||||||
|
handleCategoryRemove(s, i, sub.Options[0].Options)
|
||||||
|
case "list":
|
||||||
|
handleCategoryList(s, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func opts(o []*discordgo.ApplicationCommandInteractionDataOption) map[string]*discordgo.ApplicationCommandInteractionDataOption {
|
||||||
|
m := map[string]*discordgo.ApplicationCommandInteractionDataOption{}
|
||||||
|
for _, x := range o {
|
||||||
|
m[x.Name] = x
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
// permissionWarning names what the bot is missing in a channel, or "".
|
||||||
|
func permissionWarning(s *discordgo.Session, channelID string, need int64, what string) string {
|
||||||
|
have, err := s.UserChannelPermissions(s.State.User.ID, channelID)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if missing := tickets.MissingPerms(have, need); len(missing) > 0 {
|
||||||
|
return fmt.Sprintf("\n⚠️ I'm missing these permissions in <#%s> (%s): %s.", channelID, what, strings.Join(missing, ", "))
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetup(s *discordgo.Session, i *discordgo.InteractionCreate, o []*discordgo.ApplicationCommandInteractionDataOption) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
args := opts(o)
|
||||||
|
g := guildID(i)
|
||||||
|
|
||||||
|
ch := args["channel"].ChannelValue(nil).ID
|
||||||
|
var logID int64
|
||||||
|
logMention := "none"
|
||||||
|
if l, ok := args["log-channel"]; ok {
|
||||||
|
lc := l.ChannelValue(nil).ID
|
||||||
|
logID, _ = strconv.ParseInt(lc, 10, 64)
|
||||||
|
logMention = "<#" + lc + ">"
|
||||||
|
}
|
||||||
|
chID, _ := strconv.ParseInt(ch, 10, 64)
|
||||||
|
if err := Repo.SetChannels(ctx, g, chID, logID); err != nil {
|
||||||
|
reply(s, i, message("setup", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cats, err := Repo.ListCategories(ctx, g)
|
||||||
|
if err != nil {
|
||||||
|
reply(s, i, message("setup", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
created := ""
|
||||||
|
if r, ok := args["staff-role"]; ok && len(cats) == 0 {
|
||||||
|
role := r.RoleValue(nil, i.GuildID)
|
||||||
|
roleID, _ := strconv.ParseInt(role.ID, 10, 64)
|
||||||
|
if _, err := Repo.AddCategory(ctx, ticketrepo.Category{GuildID: g, Name: "Support", Emoji: "🎫", StaffRoleID: roleID}); err != nil {
|
||||||
|
reply(s, i, message("setup", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
created = fmt.Sprintf("\nCreated a **Support** category handled by <@&%s>.", role.ID)
|
||||||
|
cats = append(cats, ticketrepo.Category{Name: "Support"})
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := fmt.Sprintf("Tickets will open as private threads in <#%s>. Closed tickets are logged to %s.%s", ch, logMention, created)
|
||||||
|
msg += permissionWarning(s, ch, tickets.NeedTicketChannel, "ticket channel")
|
||||||
|
if logID != 0 {
|
||||||
|
msg += permissionWarning(s, strconv.FormatInt(logID, 10), tickets.NeedLogChannel, "log channel")
|
||||||
|
}
|
||||||
|
if len(cats) == 0 {
|
||||||
|
msg += "\nNext: add a category with `/tickets category add`, then post the panel with `/tickets panel`."
|
||||||
|
} else {
|
||||||
|
msg += "\nNext: post the panel with `/tickets panel`."
|
||||||
|
}
|
||||||
|
msg += "\nTip: give your staff role **Manage Threads** in that channel so they can see every ticket, even ones they weren't pinged in."
|
||||||
|
reply(s, i, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlePanel(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !deferReply(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
msg, err := tickets.PostPanel(ctx, s, Repo, guildID(i))
|
||||||
|
if err != nil {
|
||||||
|
edit(s, i, message("panel", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
edit(s, i, fmt.Sprintf("The ticket panel is up: https://discord.com/channels/%s/%s/%s", i.GuildID, msg.ChannelID, msg.ID))
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCategoryAdd(s *discordgo.Session, i *discordgo.InteractionCreate, o []*discordgo.ApplicationCommandInteractionDataOption) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
args := opts(o)
|
||||||
|
g := guildID(i)
|
||||||
|
|
||||||
|
cats, err := Repo.ListCategories(ctx, g)
|
||||||
|
if err != nil {
|
||||||
|
reply(s, i, message("category add", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
name := strings.TrimSpace(args["name"].StringValue())
|
||||||
|
for _, c := range cats {
|
||||||
|
if strings.EqualFold(c.Name, name) {
|
||||||
|
reply(s, i, fmt.Sprintf("There's already a category called **%s**.", c.Name))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(cats) >= 25 {
|
||||||
|
reply(s, i, "A panel can hold at most 25 categories.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c := ticketrepo.Category{GuildID: g, Name: name}
|
||||||
|
if r, ok := args["staff-role"]; ok {
|
||||||
|
c.StaffRoleID, _ = strconv.ParseInt(r.RoleValue(nil, i.GuildID).ID, 10, 64)
|
||||||
|
}
|
||||||
|
if e, ok := args["emoji"]; ok {
|
||||||
|
c.Emoji = strings.TrimSpace(e.StringValue())
|
||||||
|
}
|
||||||
|
if d, ok := args["description"]; ok {
|
||||||
|
c.Description = strings.TrimSpace(d.StringValue())
|
||||||
|
}
|
||||||
|
if _, err := Repo.AddCategory(ctx, c); err != nil {
|
||||||
|
reply(s, i, message("category add", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reply(s, i, fmt.Sprintf("Added **%s**. Run `/tickets panel` to show it on the panel.", name))
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCategoryRemove(s *discordgo.Session, i *discordgo.InteractionCreate, o []*discordgo.ApplicationCommandInteractionDataOption) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
name := strings.TrimSpace(opts(o)["name"].StringValue())
|
||||||
|
ok, err := Repo.DeleteCategoryByName(ctx, guildID(i), name)
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
reply(s, i, message("category remove", err))
|
||||||
|
case !ok:
|
||||||
|
reply(s, i, fmt.Sprintf("There's no category called **%s**.", name))
|
||||||
|
default:
|
||||||
|
reply(s, i, fmt.Sprintf("Removed **%s**. Open tickets keep working. Run `/tickets panel` to update the panel.", name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCategoryList(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
cats, err := Repo.ListCategories(ctx, guildID(i))
|
||||||
|
if err != nil {
|
||||||
|
reply(s, i, message("category list", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(cats) == 0 {
|
||||||
|
reply(s, i, "No categories yet. Add one with `/tickets category add`.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var b strings.Builder
|
||||||
|
for _, c := range cats {
|
||||||
|
line := "• "
|
||||||
|
if c.Emoji != "" {
|
||||||
|
line += c.Emoji + " "
|
||||||
|
}
|
||||||
|
line += "**" + c.Name + "**"
|
||||||
|
if c.StaffRoleID != 0 {
|
||||||
|
line += fmt.Sprintf(" → <@&%d>", c.StaffRoleID)
|
||||||
|
} else {
|
||||||
|
line += " → no staff role (only Manage Threads)"
|
||||||
|
}
|
||||||
|
if c.Description != "" {
|
||||||
|
line += ": " + c.Description
|
||||||
|
}
|
||||||
|
b.WriteString(line + "\n")
|
||||||
|
}
|
||||||
|
reply(s, i, b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TicketHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if Repo == nil {
|
||||||
|
reply(s, i, "Tickets aren't available right now.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sub := data.Options[0]
|
||||||
|
args := opts(sub.Options)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
switch sub.Name {
|
||||||
|
case "add", "remove":
|
||||||
|
user := args["user"].UserValue(nil)
|
||||||
|
if user == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !deferReply(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
if sub.Name == "add" {
|
||||||
|
err = tickets.AddMember(ctx, s, Repo, i.ChannelID, actor(i), user.ID)
|
||||||
|
} else {
|
||||||
|
err = tickets.RemoveMember(ctx, s, Repo, i.ChannelID, actor(i), user.ID)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
edit(s, i, message("ticket "+sub.Name, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
edit(s, i, "Done.")
|
||||||
|
case "claim":
|
||||||
|
claim(s, i)
|
||||||
|
case "close":
|
||||||
|
reason := ""
|
||||||
|
if r, ok := args["reason"]; ok {
|
||||||
|
reason = r.StringValue()
|
||||||
|
}
|
||||||
|
closeTicket(s, i, reason)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func claim(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !deferReply(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
if err := tickets.Claim(ctx, s, Repo, i.ChannelID, actor(i)); err != nil {
|
||||||
|
edit(s, i, message("claim", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
edit(s, i, "You're handling this ticket now.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func closeTicket(s *discordgo.Session, i *discordgo.InteractionCreate, reason string) {
|
||||||
|
if !deferReply(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
if err := tickets.Close(ctx, s, Repo, i.ChannelID, actor(i), reason); err != nil {
|
||||||
|
edit(s, i, message("close", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
edit(s, i, "Ticket closed.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleComponent handles the panel dropdown and Create ticket button and the Claim and
|
||||||
|
// Close buttons in a ticket.
|
||||||
|
func HandleComponent(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if Repo == nil {
|
||||||
|
reply(s, i, "Tickets aren't available right now.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := i.MessageComponentData()
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(data.CustomID, "tickets:open:"):
|
||||||
|
// Panels posted before the dropdown had one button per category.
|
||||||
|
showOpenForm(s, i, strings.TrimPrefix(data.CustomID, "tickets:open:"))
|
||||||
|
case data.CustomID == "tickets:select" && len(data.Values) > 0:
|
||||||
|
rememberPick(s, i, data.Values[0])
|
||||||
|
case data.CustomID == "tickets:create":
|
||||||
|
createFromPick(s, i)
|
||||||
|
case data.CustomID == "tickets:claim":
|
||||||
|
claim(s, i)
|
||||||
|
case data.CustomID == "tickets:close":
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
if err := tickets.CheckCanClose(ctx, Repo, i.ChannelID, actor(i)); err != nil {
|
||||||
|
reply(s, i, message("close", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = s.InteractionRespond(i.Interaction, tickets.CloseForm())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// rememberPick stores the dropdown choice for the Create ticket button and
|
||||||
|
// acknowledges it without changing the panel, so the choice stays visible.
|
||||||
|
func rememberPick(s *discordgo.Session, i *discordgo.InteractionCreate, rawCategory string) {
|
||||||
|
catID, err := strconv.ParseInt(rawCategory, 10, 64)
|
||||||
|
if err != nil || i.Member == nil || i.Member.User == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, _ := strconv.ParseInt(i.Member.User.ID, 10, 64)
|
||||||
|
tickets.RememberPick(guildID(i), userID, catID)
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{Type: discordgo.InteractionResponseDeferredMessageUpdate})
|
||||||
|
}
|
||||||
|
|
||||||
|
func createFromPick(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if i.Member == nil || i.Member.User == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID, _ := strconv.ParseInt(i.Member.User.ID, 10, 64)
|
||||||
|
catID, ok := tickets.PickedCategory(guildID(i), userID)
|
||||||
|
if !ok {
|
||||||
|
reply(s, i, "Pick a category from the menu first, then press **Create ticket**.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
showOpenForm(s, i, strconv.FormatInt(catID, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
func showOpenForm(s *discordgo.Session, i *discordgo.InteractionCreate, rawCategory string) {
|
||||||
|
catID, err := strconv.ParseInt(rawCategory, 10, 64)
|
||||||
|
if err != nil || i.Member == nil || i.Member.User == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
userID, _ := strconv.ParseInt(i.Member.User.ID, 10, 64)
|
||||||
|
_, cat, err := tickets.CheckCanOpen(ctx, Repo, guildID(i), userID, catID)
|
||||||
|
if err != nil {
|
||||||
|
reply(s, i, message("open", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = s.InteractionRespond(i.Interaction, tickets.OpenForm(cat))
|
||||||
|
}
|
||||||
|
|
||||||
|
func field(data discordgo.ModalSubmitInteractionData, name string) string {
|
||||||
|
for _, row := range data.Components {
|
||||||
|
r, ok := row.(*discordgo.ActionsRow)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, c := range r.Components {
|
||||||
|
if in, ok := c.(*discordgo.TextInput); ok && in.CustomID == name {
|
||||||
|
return strings.TrimSpace(in.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleModalSubmit handles the opening and closing forms.
|
||||||
|
func HandleModalSubmit(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if Repo == nil {
|
||||||
|
reply(s, i, "Tickets aren't available right now.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := i.ModalSubmitData()
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(data.CustomID, "tickets:openform:"):
|
||||||
|
catID, err := strconv.ParseInt(strings.TrimPrefix(data.CustomID, "tickets:openform:"), 10, 64)
|
||||||
|
if err != nil || i.Member == nil || i.Member.User == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !deferReply(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), opTimeout)
|
||||||
|
defer cancel()
|
||||||
|
thread, err := tickets.Open(ctx, s, Repo, tickets.OpenRequest{
|
||||||
|
GuildID: guildID(i), CategoryID: catID, User: i.Member.User,
|
||||||
|
Subject: field(data, "subject"), Details: field(data, "details"),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
edit(s, i, message("open", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
edit(s, i, fmt.Sprintf("Your ticket is open: <#%s>. The staff will be with you there.", thread.ID))
|
||||||
|
case data.CustomID == "tickets:closeform":
|
||||||
|
closeTicket(s, i, field(data, "reason"))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package tickets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"velox-bot/internal/commands/tickets/public"
|
||||||
|
"velox-bot/internal/db/repos/ticketrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Tickets *discordgo.ApplicationCommand = public.Tickets
|
||||||
|
Ticket *discordgo.ApplicationCommand = public.Ticket
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init(r *ticketrepo.Repo) { public.Init(r) }
|
||||||
|
|
||||||
|
func TicketsHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.TicketsHandler(s, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TicketHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.TicketHandler(s, i) }
|
||||||
|
|
||||||
|
func HandleComponent(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.HandleComponent(s, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleModalSubmit(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
public.HandleModalSubmit(s, i)
|
||||||
|
}
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
package public
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/commands/timezone/shared"
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Timezone = &discordgo.ApplicationCommand{
|
||||||
|
Name: "timezone",
|
||||||
|
Description: "Configure and view your timezone",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "set",
|
||||||
|
Description: "Set your timezone (IANA name, e.g., Europe/Lisbon)",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "zone",
|
||||||
|
Description: "Timezone (e.g., Europe/Lisbon, America/New_York)",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||||
|
Name: "show",
|
||||||
|
Description: "Show your current timezone",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TimezoneHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
if !shared.RequireUserSettingsService(s, i) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
handleShow(s, i)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch data.Options[0].Name {
|
||||||
|
case "set":
|
||||||
|
handleSet(s, i)
|
||||||
|
case "show":
|
||||||
|
handleShow(s, i)
|
||||||
|
default:
|
||||||
|
shared.RespondEphemeral(s, i, "Unknown subcommand.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSet(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
userID, ok := shared.CurrentUserID(i)
|
||||||
|
if !ok {
|
||||||
|
shared.RespondEphemeral(s, i, "Could not determine your user ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
if len(data.Options) == 0 {
|
||||||
|
shared.RespondEphemeral(s, i, "Missing options.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
opt := data.Options[0]
|
||||||
|
|
||||||
|
var zone string
|
||||||
|
for _, o := range opt.Options {
|
||||||
|
if o.Name == "zone" {
|
||||||
|
zone = o.StringValue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if zone == "" {
|
||||||
|
shared.RespondEphemeral(s, i, "Timezone cannot be empty.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate via service (which uses time.LoadLocation).
|
||||||
|
if err := services.Global.UserSettings.SetTimezone(context.Background(), userID, zone); err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Invalid timezone. Use an IANA name like `Europe/Lisbon`.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
loc, z, _, _ := services.Global.UserSettings.GetTimezone(context.Background(), userID)
|
||||||
|
now := time.Now().In(loc).Format("2006-01-02 15:04")
|
||||||
|
shared.RespondEphemeral(s, i, "Your timezone is now set to `"+z+"` (current local time: "+now+").")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleShow(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
userID, ok := shared.CurrentUserID(i)
|
||||||
|
if !ok {
|
||||||
|
shared.RespondEphemeral(s, i, "Could not determine your user ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
loc, z, userDefined, err := services.Global.UserSettings.GetTimezone(context.Background(), userID)
|
||||||
|
if err != nil {
|
||||||
|
shared.RespondEphemeral(s, i, "Failed to load your timezone.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
now := time.Now().In(loc).Format("2006-01-02 15:04")
|
||||||
|
|
||||||
|
if userDefined {
|
||||||
|
shared.RespondEphemeral(s, i, "Your timezone is `"+z+"` (current local time: "+now+").")
|
||||||
|
} else {
|
||||||
|
shared.RespondEphemeral(s, i, "You don't have a timezone set. Using `UTC` (current time: "+now+"). Use `/timezone set` to configure one.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package timezone
|
||||||
|
|
||||||
|
import (
|
||||||
|
"velox-bot/internal/commands/timezone/public"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Timezone *discordgo.ApplicationCommand = public.Timezone
|
||||||
|
)
|
||||||
|
|
||||||
|
func TimezoneHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { public.TimezoneHandler(s, i) }
|
||||||
|
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package shared
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RespondEphemeral(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: msg,
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequireUserSettingsService(s *discordgo.Session, i *discordgo.InteractionCreate) bool {
|
||||||
|
if services.Global == nil || services.Global.UserSettings == nil {
|
||||||
|
RespondEphemeral(s, i, "User settings service is not available.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func CurrentUserID(i *discordgo.InteractionCreate) (int64, bool) {
|
||||||
|
var idStr string
|
||||||
|
if i.Member != nil && i.Member.User != nil {
|
||||||
|
idStr = i.Member.User.ID
|
||||||
|
} else if i.User != nil {
|
||||||
|
idStr = i.User.ID
|
||||||
|
}
|
||||||
|
if idStr == "" {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
uid, err := strconv.ParseInt(idStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return uid, true
|
||||||
|
}
|
||||||
|
|
||||||
@@ -12,12 +12,14 @@ type Config struct {
|
|||||||
AppID string
|
AppID string
|
||||||
GuildID string
|
GuildID string
|
||||||
DBHost string
|
DBHost string
|
||||||
|
LavalinkHost string
|
||||||
|
LavalinkPass string
|
||||||
|
TwitchClientID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig() (*Config, error) {
|
func LoadConfig() (*Config, error) {
|
||||||
if err := godotenv.Load(); err != nil {
|
// Load .env if present (e.g. local dev). Ignore if missing (e.g. Docker injects env vars).
|
||||||
return nil, err
|
_ = godotenv.Load()
|
||||||
}
|
|
||||||
|
|
||||||
token := os.Getenv("BOT_TOKEN")
|
token := os.Getenv("BOT_TOKEN")
|
||||||
if token == "" {
|
if token == "" {
|
||||||
@@ -30,19 +32,27 @@ func LoadConfig() (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
guildID := os.Getenv("GUILD_ID")
|
guildID := os.Getenv("GUILD_ID")
|
||||||
if guildID == "" {
|
|
||||||
return nil, fmt.Errorf("GUILD_ID is not set")
|
|
||||||
}
|
|
||||||
|
|
||||||
dbHost := os.Getenv("DB_HOST")
|
dbHost := os.Getenv("DB_HOST")
|
||||||
if dbHost == "" {
|
if dbHost == "" {
|
||||||
return nil, fmt.Errorf("DB_HOST is not set")
|
return nil, fmt.Errorf("DB_HOST is not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lavalinkHost := os.Getenv("LAVALINK_HOST")
|
||||||
|
lavalinkPass := os.Getenv("LAVALINK_PASSWORD")
|
||||||
|
|
||||||
|
twitchClientID := os.Getenv("TWITCH_CLIENT_ID")
|
||||||
|
if twitchClientID == "" {
|
||||||
|
return nil, fmt.Errorf("TWITCH_CLIENT_ID is not set")
|
||||||
|
}
|
||||||
|
|
||||||
return &Config{
|
return &Config{
|
||||||
BotToken: token,
|
BotToken: token,
|
||||||
AppID: appID,
|
AppID: appID,
|
||||||
GuildID: guildID,
|
GuildID: guildID,
|
||||||
DBHost: dbHost,
|
DBHost: dbHost,
|
||||||
|
LavalinkHost: lavalinkHost,
|
||||||
|
LavalinkPass: lavalinkPass,
|
||||||
|
TwitchClientID: twitchClientID,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package defaultrolerepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepo(db *sql.DB) *Repo {
|
||||||
|
return &Repo{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRole returns the configured default role ID for a guild, or 0 if none.
|
||||||
|
func (r *Repo) GetRole(ctx context.Context, guildID int64) (int64, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT role_id
|
||||||
|
FROM defaultrole
|
||||||
|
WHERE guild_id = $1
|
||||||
|
`
|
||||||
|
var roleID sql.NullInt64
|
||||||
|
if err := r.db.QueryRowContext(ctx, q, guildID).Scan(&roleID); err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if !roleID.Valid {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
return roleID.Int64, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRole upserts the default role for a guild.
|
||||||
|
func (r *Repo) SetRole(ctx context.Context, guildID, roleID int64) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO defaultrole (guild_id, role_id)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
ON CONFLICT (guild_id)
|
||||||
|
DO UPDATE SET role_id = EXCLUDED.role_id
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, roleID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
package musicrepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type QueueEntry struct {
|
||||||
|
Title string
|
||||||
|
URL string
|
||||||
|
RequestedBy int64
|
||||||
|
|
||||||
|
// ClientID is the track's stable identity (set once by the manager
|
||||||
|
// when a track is first queued), used to upsert this row across
|
||||||
|
// syncs instead of assigning it a fresh DB id every single time.
|
||||||
|
ClientID string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Repo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepo(db *sql.DB) *Repo {
|
||||||
|
return &Repo{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetNowPlaying(ctx context.Context, guildID int64, title, url string, durationSeconds int, startedAt time.Time, paused bool, volume int, repeatSong, repeatQueue bool) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO music_now_playing (guild_id, track_title, track_url, duration_seconds, started_at, paused, volume, repeat_song, repeat_queue)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||||
|
ON CONFLICT (guild_id) DO UPDATE SET
|
||||||
|
track_title = $2, track_url = $3, duration_seconds = $4, started_at = $5,
|
||||||
|
paused = $6, volume = $7, repeat_song = $8, repeat_queue = $9
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, title, url, durationSeconds, startedAt, paused, volume, repeatSong, repeatQueue)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ClearNowPlaying(ctx context.Context, guildID int64) error {
|
||||||
|
const q = `DELETE FROM music_now_playing WHERE guild_id = $1`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceQueue syncs music_queue with the current in-memory queue by
|
||||||
|
// upserting each entry on its stable ClientID, rather than deleting and
|
||||||
|
// recreating every row on every call. That matters because music_queue.id
|
||||||
|
// is only an internal DB identity, remove/reorder commands target a
|
||||||
|
// track by ClientID, and that has to survive a track just sitting in the
|
||||||
|
// queue unchanged across syncs.
|
||||||
|
func (r *Repo) ReplaceQueue(ctx context.Context, guildID int64, entries []QueueEntry) error {
|
||||||
|
tx, err := r.db.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
existing := make(map[string]bool)
|
||||||
|
rows, err := tx.QueryContext(ctx, `SELECT client_id FROM music_queue WHERE guild_id = $1`, guildID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for rows.Next() {
|
||||||
|
var clientID string
|
||||||
|
if err := rows.Scan(&clientID); err != nil {
|
||||||
|
rows.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
existing[clientID] = true
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
rows.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows.Close()
|
||||||
|
|
||||||
|
keep := make(map[string]bool, len(entries))
|
||||||
|
for i, entry := range entries {
|
||||||
|
keep[entry.ClientID] = true
|
||||||
|
const q = `
|
||||||
|
INSERT INTO music_queue (guild_id, position, track_title, track_url, requested_by, client_id)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
|
ON CONFLICT (guild_id, client_id) DO UPDATE SET
|
||||||
|
position = EXCLUDED.position,
|
||||||
|
track_title = EXCLUDED.track_title,
|
||||||
|
track_url = EXCLUDED.track_url,
|
||||||
|
requested_by = EXCLUDED.requested_by
|
||||||
|
`
|
||||||
|
if _, err := tx.ExecContext(ctx, q, guildID, i, entry.Title, entry.URL, entry.RequestedBy, entry.ClientID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for clientID := range existing {
|
||||||
|
if keep[clientID] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const q = `DELETE FROM music_queue WHERE guild_id = $1 AND client_id = $2`
|
||||||
|
if _, err := tx.ExecContext(ctx, q, guildID, clientID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
type MusicCommand struct {
|
||||||
|
ID int64
|
||||||
|
GuildID int64
|
||||||
|
CommandType string
|
||||||
|
Payload []byte
|
||||||
|
RequestedBy int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListUnprocessedCommands(ctx context.Context) ([]MusicCommand, error) {
|
||||||
|
const q = `SELECT id, guild_id, command_type, payload, requested_by FROM music_commands WHERE processed_at IS NULL ORDER BY id ASC`
|
||||||
|
|
||||||
|
rows, err := r.db.QueryContext(ctx, q)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var commands []MusicCommand
|
||||||
|
for rows.Next() {
|
||||||
|
var cmd MusicCommand
|
||||||
|
if err := rows.Scan(&cmd.ID, &cmd.GuildID, &cmd.CommandType, &cmd.Payload, &cmd.RequestedBy); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
commands = append(commands, cmd)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return commands, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) MarkCommandProcessed(ctx context.Context, id int64) error {
|
||||||
|
const q = `UPDATE music_commands SET processed_at = NOW() WHERE id = $1`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,218 @@
|
|||||||
|
package projectsrepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
type Project struct {
|
||||||
|
ID int64
|
||||||
|
GuildID int64
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
IsActive bool
|
||||||
|
CreatedBy int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProjectWithMembers struct {
|
||||||
|
Project *Project
|
||||||
|
Creator int64
|
||||||
|
Helpers []int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepo(db *sql.DB) *Repo {
|
||||||
|
return &Repo{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) CreateProject(ctx context.Context, guildID, creatorID int64, name, description string) (int64, error) {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO projects (guild_id, name, description, created_by)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
RETURNING id
|
||||||
|
`
|
||||||
|
var id int64
|
||||||
|
if err := r.db.QueryRowContext(ctx, q, guildID, name, description, creatorID).Scan(&id); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const qMember = `
|
||||||
|
INSERT INTO project_members (project_id, user_id, is_creator)
|
||||||
|
VALUES ($1, $2, TRUE)
|
||||||
|
ON CONFLICT (project_id, user_id) DO NOTHING
|
||||||
|
`
|
||||||
|
if _, err := r.db.ExecContext(ctx, qMember, id, creatorID); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) AddHelper(ctx context.Context, projectID, userID int64) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO project_members (project_id, user_id, is_creator)
|
||||||
|
VALUES ($1, $2, FALSE)
|
||||||
|
ON CONFLICT (project_id, user_id) DO NOTHING
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, projectID, userID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) DeactivateProject(ctx context.Context, guildID, projectID int64) error {
|
||||||
|
const q = `
|
||||||
|
UPDATE projects
|
||||||
|
SET is_active = FALSE
|
||||||
|
WHERE id = $1 AND guild_id = $2
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, projectID, guildID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProject fetches a single project, scoped to guildID so a project ID
|
||||||
|
// from one server can't be used to reach into another's data.
|
||||||
|
func (r *Repo) GetProject(ctx context.Context, guildID, projectID int64) (*Project, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT id, name, description, created_by
|
||||||
|
FROM projects
|
||||||
|
WHERE id = $1 AND guild_id = $2
|
||||||
|
`
|
||||||
|
row := r.db.QueryRowContext(ctx, q, projectID, guildID)
|
||||||
|
p := &Project{GuildID: guildID, IsActive: true}
|
||||||
|
if err := row.Scan(&p.ID, &p.Name, &p.Description, &p.CreatedBy); err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ProjectExistsInGuild(ctx context.Context, guildID, projectID int64) (bool, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT 1
|
||||||
|
FROM projects
|
||||||
|
WHERE guild_id = $1 AND id = $2
|
||||||
|
`
|
||||||
|
row := r.db.QueryRowContext(ctx, q, guildID, projectID)
|
||||||
|
var one int
|
||||||
|
switch err := row.Scan(&one); err {
|
||||||
|
case nil:
|
||||||
|
return true, nil
|
||||||
|
case sql.ErrNoRows:
|
||||||
|
return false, nil
|
||||||
|
default:
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListActiveProjects is a lighter version of ListActiveProjectsWithMembers,
|
||||||
|
// without the member-join query - for callers (like building a select menu
|
||||||
|
// of projects to pick from) that only need name/id, not creator/helpers.
|
||||||
|
func (r *Repo) ListActiveProjects(ctx context.Context, guildID int64, limit int) ([]*Project, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT id, name, description, created_by
|
||||||
|
FROM projects
|
||||||
|
WHERE guild_id = $1 AND is_active = TRUE
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT $2
|
||||||
|
`
|
||||||
|
rows, err := r.db.QueryContext(ctx, q, guildID, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var out []*Project
|
||||||
|
for rows.Next() {
|
||||||
|
p := &Project{GuildID: guildID, IsActive: true}
|
||||||
|
if err := rows.Scan(&p.ID, &p.Name, &p.Description, &p.CreatedBy); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, p)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListActiveProjectsWithMembers(ctx context.Context, guildID int64, limit int) ([]*ProjectWithMembers, error) {
|
||||||
|
const qProjects = `
|
||||||
|
SELECT id, name, description, created_by
|
||||||
|
FROM projects
|
||||||
|
WHERE guild_id = $1 AND is_active = TRUE
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT $2
|
||||||
|
`
|
||||||
|
rows, err := r.db.QueryContext(ctx, qProjects, guildID, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var out []*ProjectWithMembers
|
||||||
|
for rows.Next() {
|
||||||
|
p := &Project{GuildID: guildID, IsActive: true}
|
||||||
|
if err := rows.Scan(&p.ID, &p.Name, &p.Description, &p.CreatedBy); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, &ProjectWithMembers{
|
||||||
|
Project: p,
|
||||||
|
Creator: p.CreatedBy,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(out) == 0 {
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load members per project.
|
||||||
|
const qMembers = `
|
||||||
|
SELECT project_id, user_id, is_creator
|
||||||
|
FROM project_members
|
||||||
|
WHERE project_id = ANY($1)
|
||||||
|
`
|
||||||
|
ids := make([]int64, len(out))
|
||||||
|
for i, p := range out {
|
||||||
|
ids[i] = p.Project.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsM, err := r.db.QueryContext(ctx, qMembers, ids)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rowsM.Close()
|
||||||
|
|
||||||
|
index := make(map[int64]*ProjectWithMembers, len(out))
|
||||||
|
for _, p := range out {
|
||||||
|
index[p.Project.ID] = p
|
||||||
|
}
|
||||||
|
|
||||||
|
for rowsM.Next() {
|
||||||
|
var pid, uid int64
|
||||||
|
var isCreator bool
|
||||||
|
if err := rowsM.Scan(&pid, &uid, &isCreator); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
item, ok := index[pid]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if isCreator {
|
||||||
|
item.Creator = uid
|
||||||
|
} else {
|
||||||
|
item.Helpers = append(item.Helpers, uid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := rowsM.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package rpsrepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepo(db *sql.DB) *Repo {
|
||||||
|
return &Repo{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) GetScore(ctx context.Context, guildID int64, userID int64) (int, bool, error) {
|
||||||
|
var score int
|
||||||
|
err := r.db.QueryRowContext(ctx, `
|
||||||
|
SELECT score
|
||||||
|
FROM rps
|
||||||
|
WHERE guild_id = $1 AND user_id = $2
|
||||||
|
`, guildID, userID).Scan(&score)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return 0, false, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return 0, false, err
|
||||||
|
}
|
||||||
|
return score, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetScore(ctx context.Context, guildID int64, userID int64, score int) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `
|
||||||
|
INSERT INTO rps (guild_id, user_id, score)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
ON CONFLICT (guild_id, user_id)
|
||||||
|
DO UPDATE SET score = EXCLUDED.score
|
||||||
|
`, guildID, userID, score)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type ScoreEntry struct {
|
||||||
|
UserID int64
|
||||||
|
Score int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) TopScores(ctx context.Context, guildID int64, limit int) ([]ScoreEntry, error) {
|
||||||
|
if limit <= 0 {
|
||||||
|
limit = 10
|
||||||
|
}
|
||||||
|
rows, err := r.db.QueryContext(ctx, `
|
||||||
|
SELECT user_id, score
|
||||||
|
FROM rps
|
||||||
|
WHERE guild_id = $1
|
||||||
|
ORDER BY score DESC, user_id ASC
|
||||||
|
LIMIT $2
|
||||||
|
`, guildID, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
out := make([]ScoreEntry, 0, limit)
|
||||||
|
for rows.Next() {
|
||||||
|
var e ScoreEntry
|
||||||
|
if err := rows.Scan(&e.UserID, &e.Score); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, e)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,202 @@
|
|||||||
|
package schedulerepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
type ScheduleStatus string
|
||||||
|
|
||||||
|
const (
|
||||||
|
StatusPending ScheduleStatus = "pending"
|
||||||
|
StatusAccepted ScheduleStatus = "accepted"
|
||||||
|
StatusDeclined ScheduleStatus = "declined"
|
||||||
|
StatusRescheduleRequested ScheduleStatus = "reschedule_requested"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Schedule struct {
|
||||||
|
ID int64
|
||||||
|
GuildID int64
|
||||||
|
RequesterID int64
|
||||||
|
InviteeID int64
|
||||||
|
ScheduledAt time.Time
|
||||||
|
ProposedAt *time.Time
|
||||||
|
ProposedBy *int64
|
||||||
|
PreRescheduleStatus *ScheduleStatus
|
||||||
|
Status ScheduleStatus
|
||||||
|
Description string
|
||||||
|
ReminderSent bool
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepo(db *sql.DB) *Repo {
|
||||||
|
return &Repo{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) CreateSchedule(ctx context.Context, guildID, requesterID, inviteeID int64, when time.Time, description string) (int64, error) {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO schedules (guild_id, requester_id, invitee_id, scheduled_at, description)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
RETURNING id
|
||||||
|
`
|
||||||
|
var id int64
|
||||||
|
if err := r.db.QueryRowContext(ctx, q, guildID, requesterID, inviteeID, when, description).Scan(&id); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) UpdateStatus(ctx context.Context, scheduleID int64, status ScheduleStatus) error {
|
||||||
|
const q = `
|
||||||
|
UPDATE schedules
|
||||||
|
SET status = $1
|
||||||
|
WHERE id = $2
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, status, scheduleID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListForUser(ctx context.Context, guildID, userID int64, limit int) ([]*Schedule, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT id, guild_id, requester_id, invitee_id, scheduled_at, status, description, reminder_sent, created_at
|
||||||
|
FROM schedules
|
||||||
|
WHERE guild_id = $1
|
||||||
|
AND (requester_id = $2 OR invitee_id = $2)
|
||||||
|
AND status IN ('pending', 'accepted', 'reschedule_requested')
|
||||||
|
ORDER BY scheduled_at ASC
|
||||||
|
LIMIT $3
|
||||||
|
`
|
||||||
|
rows, err := r.db.QueryContext(ctx, q, guildID, userID, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var out []*Schedule
|
||||||
|
for rows.Next() {
|
||||||
|
var sch Schedule
|
||||||
|
if err := rows.Scan(&sch.ID, &sch.GuildID, &sch.RequesterID, &sch.InviteeID, &sch.ScheduledAt, &sch.Status, &sch.Description, &sch.ReminderSent, &sch.CreatedAt); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, &sch)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) GetByID(ctx context.Context, id int64) (*Schedule, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT id, guild_id, requester_id, invitee_id, scheduled_at, proposed_at, proposed_by, pre_reschedule_status, status, description, reminder_sent, created_at
|
||||||
|
FROM schedules
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
row := r.db.QueryRowContext(ctx, q, id)
|
||||||
|
var sch Schedule
|
||||||
|
if err := row.Scan(&sch.ID, &sch.GuildID, &sch.RequesterID, &sch.InviteeID, &sch.ScheduledAt, &sch.ProposedAt, &sch.ProposedBy, &sch.PreRescheduleStatus, &sch.Status, &sch.Description, &sch.ReminderSent, &sch.CreatedAt); err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &sch, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) Reschedule(ctx context.Context, id int64, when time.Time) error {
|
||||||
|
const q = `
|
||||||
|
UPDATE schedules
|
||||||
|
SET scheduled_at = $1,
|
||||||
|
status = 'pending',
|
||||||
|
reminder_sent = FALSE
|
||||||
|
WHERE id = $2
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, when, id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListDueForReminder returns sessions starting between now and now+ahead, not yet reminded.
|
||||||
|
func (r *Repo) ListDueForReminder(ctx context.Context, now time.Time, ahead time.Duration) ([]*Schedule, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT id, guild_id, requester_id, invitee_id, scheduled_at, status, description, reminder_sent, created_at
|
||||||
|
FROM schedules
|
||||||
|
WHERE reminder_sent = FALSE
|
||||||
|
AND status IN ('pending', 'accepted', 'reschedule_requested')
|
||||||
|
AND scheduled_at > $1
|
||||||
|
AND scheduled_at <= $2
|
||||||
|
`
|
||||||
|
rows, err := r.db.QueryContext(ctx, q, now, now.Add(ahead))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var out []*Schedule
|
||||||
|
for rows.Next() {
|
||||||
|
var sch Schedule
|
||||||
|
if err := rows.Scan(&sch.ID, &sch.GuildID, &sch.RequesterID, &sch.InviteeID, &sch.ScheduledAt, &sch.Status, &sch.Description, &sch.ReminderSent, &sch.CreatedAt); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, &sch)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) MarkReminded(ctx context.Context, scheduleID int64) error {
|
||||||
|
const q = `
|
||||||
|
UPDATE schedules
|
||||||
|
SET reminder_sent = TRUE
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, scheduleID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ProposeReschedule(ctx context.Context, scheduleID int64, newTime time.Time, proposedBy int64) error {
|
||||||
|
const q = `
|
||||||
|
UPDATE schedules
|
||||||
|
SET proposed_at = $1,
|
||||||
|
proposed_by = $2,
|
||||||
|
pre_reschedule_status = status,
|
||||||
|
status = 'reschedule_requested'
|
||||||
|
WHERE id = $3
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, newTime, proposedBy, scheduleID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) AcceptReschedule(ctx context.Context, scheduleID int64) error {
|
||||||
|
const q = `
|
||||||
|
UPDATE schedules
|
||||||
|
SET scheduled_at = proposed_at,
|
||||||
|
proposed_at = NULL,
|
||||||
|
proposed_by = NULL,
|
||||||
|
pre_reschedule_status = NULL,
|
||||||
|
status = 'accepted',
|
||||||
|
reminder_sent = FALSE
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, scheduleID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) DeclineReschedule(ctx context.Context, scheduleID int64) error {
|
||||||
|
const q = `
|
||||||
|
UPDATE schedules
|
||||||
|
SET proposed_at = NULL,
|
||||||
|
proposed_by = NULL,
|
||||||
|
status = pre_reschedule_status,
|
||||||
|
pre_reschedule_status = NULL
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, scheduleID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,584 @@
|
|||||||
|
// Package securityrepo stores the anti-raid and anti-nuke settings, the
|
||||||
|
// trusted list, quarantines, locked channels, the event history and the
|
||||||
|
// dashboard's requests. Discord IDs are int64; 0 means "not set".
|
||||||
|
package securityrepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepo(db *sql.DB) *Repo {
|
||||||
|
return &Repo{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter actions.
|
||||||
|
const (
|
||||||
|
ActionOff = "off"
|
||||||
|
ActionAlert = "alert" // raid mode only: alert staff, touch nobody
|
||||||
|
ActionTimeout = "timeout"
|
||||||
|
ActionKick = "kick"
|
||||||
|
ActionBan = "ban"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Anti-nuke limit keys.
|
||||||
|
const (
|
||||||
|
LimitChannelDelete = "channel_delete"
|
||||||
|
LimitChannelCreate = "channel_create"
|
||||||
|
LimitRoleDelete = "role_delete"
|
||||||
|
LimitRoleCreate = "role_create"
|
||||||
|
LimitBan = "ban"
|
||||||
|
LimitKick = "kick"
|
||||||
|
LimitWebhookCreate = "webhook_create"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultLimits mirrors the column default in the migration.
|
||||||
|
func DefaultLimits() map[string]int {
|
||||||
|
return map[string]int{
|
||||||
|
LimitChannelDelete: 2, LimitChannelCreate: 5, LimitRoleDelete: 2, LimitRoleCreate: 5,
|
||||||
|
LimitBan: 3, LimitKick: 5, LimitWebhookCreate: 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Settings struct {
|
||||||
|
GuildID int64
|
||||||
|
AlertChannelID int64
|
||||||
|
AlertRoleID int64
|
||||||
|
AlertDM bool
|
||||||
|
|
||||||
|
JoinGateEnabled bool
|
||||||
|
NoAvatar string
|
||||||
|
NewAccount string
|
||||||
|
MinAccountAgeDays int
|
||||||
|
AdvertisingName string
|
||||||
|
UsernameFilter string
|
||||||
|
UsernamePatterns []string
|
||||||
|
UnverifiedBot string
|
||||||
|
UnauthorizedBot string
|
||||||
|
AntiRaidEnabled bool
|
||||||
|
RaidJoinCount int
|
||||||
|
RaidJoinSeconds int
|
||||||
|
RaidAction string
|
||||||
|
RaidPauseInvites bool
|
||||||
|
RaidQuietMinutes int
|
||||||
|
AntiNukeEnabled bool
|
||||||
|
AntiNukeWindowSeconds int
|
||||||
|
AntiNukeLimits map[string]int
|
||||||
|
AntiNukeLockServer bool
|
||||||
|
QuarantineRoleID int64
|
||||||
|
AntiNukeAutoRestore bool
|
||||||
|
HeatEnabled bool
|
||||||
|
HeatStrictness string
|
||||||
|
HeatAction string
|
||||||
|
HeatTimeoutMinutes int
|
||||||
|
HeatEscalate bool
|
||||||
|
HeatBlockedWords []string
|
||||||
|
HeatPanicMembers int
|
||||||
|
HeatLockdownMentions int
|
||||||
|
JoinLock string
|
||||||
|
|
||||||
|
// Configured is false when the guild has no row yet; the fields then
|
||||||
|
// hold the defaults, with every system off.
|
||||||
|
Configured bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func Defaults(guildID int64) *Settings {
|
||||||
|
return &Settings{
|
||||||
|
GuildID: guildID,
|
||||||
|
AlertDM: true,
|
||||||
|
NoAvatar: ActionOff, NewAccount: ActionOff, MinAccountAgeDays: 7, AdvertisingName: ActionOff,
|
||||||
|
UsernameFilter: ActionOff, UnverifiedBot: ActionOff, UnauthorizedBot: ActionKick,
|
||||||
|
RaidJoinCount: 10, RaidJoinSeconds: 10, RaidAction: ActionKick, RaidPauseInvites: true, RaidQuietMinutes: 10,
|
||||||
|
AntiNukeWindowSeconds: 10, AntiNukeLimits: DefaultLimits(),
|
||||||
|
HeatStrictness: "medium", HeatAction: ActionTimeout, HeatTimeoutMinutes: 10, HeatEscalate: true, HeatPanicMembers: 3,
|
||||||
|
JoinLock: ActionOff,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func nullID(v int64) sql.NullInt64 { return sql.NullInt64{Int64: v, Valid: v != 0} }
|
||||||
|
|
||||||
|
func (r *Repo) GetSettings(ctx context.Context, guildID int64) (*Settings, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT alert_channel_id, alert_role_id, alert_dm,
|
||||||
|
joingate_enabled, joingate_no_avatar, joingate_new_account, joingate_min_account_age_days,
|
||||||
|
joingate_advertising_name, joingate_username_filter, to_json(joingate_username_patterns),
|
||||||
|
joingate_unverified_bot, joingate_unauthorized_bot,
|
||||||
|
antiraid_enabled, raid_join_count, raid_join_seconds, raid_action, raid_pause_invites, raid_quiet_minutes,
|
||||||
|
antinuke_enabled, antinuke_window_seconds, antinuke_limits, antinuke_lock_server, quarantine_role_id,
|
||||||
|
antinuke_auto_restore, heat_enabled, heat_strictness, heat_action, heat_timeout_minutes, heat_escalate,
|
||||||
|
to_json(heat_blocked_words), heat_panic_members, heat_lockdown_mentions,
|
||||||
|
join_lock
|
||||||
|
FROM security_settings WHERE guild_id = $1`
|
||||||
|
s := &Settings{GuildID: guildID, Configured: true}
|
||||||
|
var alertCh, alertRole, qRole sql.NullInt64
|
||||||
|
var patterns, words []byte
|
||||||
|
var limits []byte
|
||||||
|
err := r.db.QueryRowContext(ctx, q, guildID).Scan(
|
||||||
|
&alertCh, &alertRole, &s.AlertDM,
|
||||||
|
&s.JoinGateEnabled, &s.NoAvatar, &s.NewAccount, &s.MinAccountAgeDays,
|
||||||
|
&s.AdvertisingName, &s.UsernameFilter, &patterns,
|
||||||
|
&s.UnverifiedBot, &s.UnauthorizedBot,
|
||||||
|
&s.AntiRaidEnabled, &s.RaidJoinCount, &s.RaidJoinSeconds, &s.RaidAction, &s.RaidPauseInvites, &s.RaidQuietMinutes,
|
||||||
|
&s.AntiNukeEnabled, &s.AntiNukeWindowSeconds, &limits, &s.AntiNukeLockServer, &qRole,
|
||||||
|
&s.AntiNukeAutoRestore, &s.HeatEnabled, &s.HeatStrictness, &s.HeatAction, &s.HeatTimeoutMinutes, &s.HeatEscalate,
|
||||||
|
&words, &s.HeatPanicMembers, &s.HeatLockdownMentions,
|
||||||
|
&s.JoinLock,
|
||||||
|
)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return Defaults(guildID), nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.AlertChannelID, s.AlertRoleID, s.QuarantineRoleID = alertCh.Int64, alertRole.Int64, qRole.Int64
|
||||||
|
_ = json.Unmarshal(patterns, &s.UsernamePatterns)
|
||||||
|
_ = json.Unmarshal(words, &s.HeatBlockedWords)
|
||||||
|
s.AntiNukeLimits = DefaultLimits()
|
||||||
|
if len(limits) > 0 {
|
||||||
|
// Saved values override the defaults key by key, so a limit added
|
||||||
|
// in a later version still has a value for older rows.
|
||||||
|
var saved map[string]int
|
||||||
|
if err := json.Unmarshal(limits, &saved); err == nil {
|
||||||
|
for k, v := range saved {
|
||||||
|
s.AntiNukeLimits[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensureRow makes sure the guild has a settings row so single-column updates
|
||||||
|
// below can be plain UPDATEs.
|
||||||
|
func (r *Repo) ensureRow(ctx context.Context, guildID int64) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `INSERT INTO security_settings (guild_id) VALUES ($1) ON CONFLICT DO NOTHING`, guildID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) update(ctx context.Context, guildID int64, set string, args ...any) error {
|
||||||
|
if err := r.ensureRow(ctx, guildID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err := r.db.ExecContext(ctx, `UPDATE security_settings SET `+set+` WHERE guild_id = $1`, append([]any{guildID}, args...)...)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Feature names accepted by SetEnabled.
|
||||||
|
const (
|
||||||
|
FeatureJoinGate = "joingate"
|
||||||
|
FeatureAntiRaid = "antiraid"
|
||||||
|
FeatureAntiNuke = "antinuke"
|
||||||
|
FeatureHeat = "heat"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (r *Repo) SetEnabled(ctx context.Context, guildID int64, feature string, on bool) error {
|
||||||
|
col := map[string]string{FeatureJoinGate: "joingate_enabled", FeatureAntiRaid: "antiraid_enabled", FeatureAntiNuke: "antinuke_enabled", FeatureHeat: "heat_enabled"}[feature]
|
||||||
|
if col == "" {
|
||||||
|
return errors.New("unknown feature " + feature)
|
||||||
|
}
|
||||||
|
return r.update(ctx, guildID, col+` = $2`, on)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetAlerts(ctx context.Context, guildID, channelID, roleID int64) error {
|
||||||
|
return r.update(ctx, guildID, `alert_channel_id = $2, alert_role_id = $3`, nullID(channelID), nullID(roleID))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetQuarantineRole(ctx context.Context, guildID, roleID int64) error {
|
||||||
|
return r.update(ctx, guildID, `quarantine_role_id = $2`, nullID(roleID))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetJoinLock(ctx context.Context, guildID int64, mode string) error {
|
||||||
|
return r.update(ctx, guildID, `join_lock = $2`, mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Trusted ----
|
||||||
|
|
||||||
|
type Trusted struct {
|
||||||
|
TargetID int64
|
||||||
|
Kind string // "user" or "role"
|
||||||
|
AddedBy int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListTrusted(ctx context.Context, guildID int64) ([]Trusted, error) {
|
||||||
|
rows, err := r.db.QueryContext(ctx, `SELECT target_id, kind, added_by FROM security_trusted WHERE guild_id = $1 ORDER BY created_at`, guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []Trusted
|
||||||
|
for rows.Next() {
|
||||||
|
var t Trusted
|
||||||
|
if err := rows.Scan(&t.TargetID, &t.Kind, &t.AddedBy); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, t)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) AddTrusted(ctx context.Context, guildID int64, t Trusted) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `
|
||||||
|
INSERT INTO security_trusted (guild_id, target_id, kind, added_by) VALUES ($1, $2, $3, $4)
|
||||||
|
ON CONFLICT (guild_id, target_id) DO UPDATE SET kind = EXCLUDED.kind`, guildID, t.TargetID, t.Kind, t.AddedBy)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) RemoveTrusted(ctx context.Context, guildID, targetID int64) (bool, error) {
|
||||||
|
res, err := r.db.ExecContext(ctx, `DELETE FROM security_trusted WHERE guild_id = $1 AND target_id = $2`, guildID, targetID)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
n, err := res.RowsAffected()
|
||||||
|
return n > 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Quarantine ----
|
||||||
|
|
||||||
|
type Quarantine struct {
|
||||||
|
GuildID int64
|
||||||
|
UserID int64
|
||||||
|
PreviousRoleIDs []int64
|
||||||
|
Reason string
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveQuarantine records a quarantine. If the member is already quarantined
|
||||||
|
// the first record is kept, since its roles are the ones to give back.
|
||||||
|
func (r *Repo) SaveQuarantine(ctx context.Context, q Quarantine) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `
|
||||||
|
INSERT INTO security_quarantines (guild_id, user_id, previous_role_ids, reason)
|
||||||
|
VALUES ($1, $2, COALESCE($3::bigint[], '{}'), $4)
|
||||||
|
ON CONFLICT (guild_id, user_id) DO NOTHING`, q.GuildID, q.UserID, q.PreviousRoleIDs, q.Reason)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func scanQuarantine(row interface{ Scan(...any) error }) (Quarantine, error) {
|
||||||
|
var q Quarantine
|
||||||
|
var roles []byte
|
||||||
|
err := row.Scan(&q.GuildID, &q.UserID, &roles, &q.Reason, &q.CreatedAt)
|
||||||
|
if err == nil {
|
||||||
|
err = json.Unmarshal(roles, &q.PreviousRoleIDs)
|
||||||
|
}
|
||||||
|
return q, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQuarantine returns nil when the member isn't quarantined.
|
||||||
|
func (r *Repo) GetQuarantine(ctx context.Context, guildID, userID int64) (*Quarantine, error) {
|
||||||
|
q, err := scanQuarantine(r.db.QueryRowContext(ctx, `
|
||||||
|
SELECT guild_id, user_id, to_json(previous_role_ids), reason, created_at FROM security_quarantines
|
||||||
|
WHERE guild_id = $1 AND user_id = $2`, guildID, userID))
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &q, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListQuarantines(ctx context.Context, guildID int64) ([]Quarantine, error) {
|
||||||
|
rows, err := r.db.QueryContext(ctx, `
|
||||||
|
SELECT guild_id, user_id, to_json(previous_role_ids), reason, created_at FROM security_quarantines
|
||||||
|
WHERE guild_id = $1 ORDER BY created_at DESC`, guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []Quarantine
|
||||||
|
for rows.Next() {
|
||||||
|
q, err := scanQuarantine(rows)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, q)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) DeleteQuarantine(ctx context.Context, guildID, userID int64) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `DELETE FROM security_quarantines WHERE guild_id = $1 AND user_id = $2`, guildID, userID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Lockdowns ----
|
||||||
|
|
||||||
|
type Lockdown struct {
|
||||||
|
GuildID int64
|
||||||
|
ChannelID int64
|
||||||
|
HadOverwrite bool
|
||||||
|
PreviousAllow int64
|
||||||
|
PreviousDeny int64
|
||||||
|
ServerWide bool
|
||||||
|
LockedBy int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveLockdown remembers a channel's @everyone overwrite from before it was
|
||||||
|
// locked. A channel that is already locked keeps its first record, which is
|
||||||
|
// the state to go back to.
|
||||||
|
func (r *Repo) SaveLockdown(ctx context.Context, l Lockdown) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `
|
||||||
|
INSERT INTO security_lockdowns (guild_id, channel_id, had_overwrite, previous_allow, previous_deny, server_wide, locked_by)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||||
|
ON CONFLICT (guild_id, channel_id) DO NOTHING`,
|
||||||
|
l.GuildID, l.ChannelID, l.HadOverwrite, l.PreviousAllow, l.PreviousDeny, l.ServerWide, l.LockedBy)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLockdown returns nil when the channel isn't locked by the bot.
|
||||||
|
func (r *Repo) GetLockdown(ctx context.Context, guildID, channelID int64) (*Lockdown, error) {
|
||||||
|
var l Lockdown
|
||||||
|
err := r.db.QueryRowContext(ctx, `
|
||||||
|
SELECT guild_id, channel_id, had_overwrite, previous_allow, previous_deny, server_wide, locked_by
|
||||||
|
FROM security_lockdowns WHERE guild_id = $1 AND channel_id = $2`, guildID, channelID).
|
||||||
|
Scan(&l.GuildID, &l.ChannelID, &l.HadOverwrite, &l.PreviousAllow, &l.PreviousDeny, &l.ServerWide, &l.LockedBy)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &l, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListLockdowns lists locked channels; serverWideOnly limits it to the ones a
|
||||||
|
// server lockdown locked.
|
||||||
|
func (r *Repo) ListLockdowns(ctx context.Context, guildID int64, serverWideOnly bool) ([]Lockdown, error) {
|
||||||
|
rows, err := r.db.QueryContext(ctx, `
|
||||||
|
SELECT guild_id, channel_id, had_overwrite, previous_allow, previous_deny, server_wide, locked_by
|
||||||
|
FROM security_lockdowns WHERE guild_id = $1 AND (NOT $2 OR server_wide) ORDER BY locked_at`, guildID, serverWideOnly)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []Lockdown
|
||||||
|
for rows.Next() {
|
||||||
|
var l Lockdown
|
||||||
|
if err := rows.Scan(&l.GuildID, &l.ChannelID, &l.HadOverwrite, &l.PreviousAllow, &l.PreviousDeny, &l.ServerWide, &l.LockedBy); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, l)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) DeleteLockdown(ctx context.Context, guildID, channelID int64) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `DELETE FROM security_lockdowns WHERE guild_id = $1 AND channel_id = $2`, guildID, channelID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Events ----
|
||||||
|
|
||||||
|
type Event struct {
|
||||||
|
GuildID int64
|
||||||
|
Kind string
|
||||||
|
ActorID int64
|
||||||
|
TargetID int64
|
||||||
|
Detail string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event kinds.
|
||||||
|
const (
|
||||||
|
EventJoinGate = "joingate"
|
||||||
|
EventRaidStarted = "raid_started"
|
||||||
|
EventRaidEnded = "raid_ended"
|
||||||
|
EventNuke = "nuke"
|
||||||
|
EventQuarantined = "quarantined"
|
||||||
|
EventReleased = "released"
|
||||||
|
EventUnauthorizedBot = "unauthorized_bot"
|
||||||
|
EventLocked = "locked"
|
||||||
|
EventUnlocked = "unlocked"
|
||||||
|
EventHeat = "heat"
|
||||||
|
EventHeatPanic = "heat_panic"
|
||||||
|
EventSnapshot = "snapshot"
|
||||||
|
EventRestored = "restored"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (r *Repo) AddEvent(ctx context.Context, e Event) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `
|
||||||
|
INSERT INTO security_events (guild_id, kind, actor_id, target_id, detail) VALUES ($1, $2, $3, $4, $5)`,
|
||||||
|
e.GuildID, e.Kind, nullID(e.ActorID), nullID(e.TargetID), e.Detail)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Dashboard commands ----
|
||||||
|
|
||||||
|
type Command struct {
|
||||||
|
ID int64
|
||||||
|
GuildID int64
|
||||||
|
CommandType string
|
||||||
|
Payload []byte
|
||||||
|
RequestedBy int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListUnprocessedCommands(ctx context.Context) ([]Command, error) {
|
||||||
|
rows, err := r.db.QueryContext(ctx, `
|
||||||
|
SELECT id, guild_id, command_type, COALESCE(payload, '{}'::jsonb), requested_by
|
||||||
|
FROM security_commands WHERE processed_at IS NULL ORDER BY id`)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []Command
|
||||||
|
for rows.Next() {
|
||||||
|
var c Command
|
||||||
|
if err := rows.Scan(&c.ID, &c.GuildID, &c.CommandType, &c.Payload, &c.RequestedBy); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, c)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) MarkCommandProcessed(ctx context.Context, id int64) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `UPDATE security_commands SET processed_at = NOW() WHERE id = $1`, id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Raids in progress ----
|
||||||
|
|
||||||
|
type Raid struct {
|
||||||
|
GuildID int64
|
||||||
|
StartedAt time.Time
|
||||||
|
LastJoinAt time.Time
|
||||||
|
InvitesPaused bool
|
||||||
|
CaughtTotal int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SaveRaid(ctx context.Context, x Raid) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `
|
||||||
|
INSERT INTO security_raids (guild_id, started_at, last_join_at, invites_paused, caught_total)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
ON CONFLICT (guild_id) DO UPDATE SET
|
||||||
|
last_join_at = EXCLUDED.last_join_at, invites_paused = EXCLUDED.invites_paused, caught_total = EXCLUDED.caught_total`,
|
||||||
|
x.GuildID, x.StartedAt, x.LastJoinAt, x.InvitesPaused, x.CaughtTotal)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListRaids(ctx context.Context) ([]Raid, error) {
|
||||||
|
rows, err := r.db.QueryContext(ctx, `SELECT guild_id, started_at, last_join_at, invites_paused, caught_total FROM security_raids`)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []Raid
|
||||||
|
for rows.Next() {
|
||||||
|
var x Raid
|
||||||
|
if err := rows.Scan(&x.GuildID, &x.StartedAt, &x.LastJoinAt, &x.InvitesPaused, &x.CaughtTotal); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, x)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteRaid ends a raid's record and its caught list.
|
||||||
|
func (r *Repo) DeleteRaid(ctx context.Context, guildID int64) error {
|
||||||
|
if _, err := r.db.ExecContext(ctx, `DELETE FROM security_raid_caught WHERE guild_id = $1`, guildID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err := r.db.ExecContext(ctx, `DELETE FROM security_raids WHERE guild_id = $1`, guildID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type Caught struct {
|
||||||
|
UserID int64
|
||||||
|
Action string
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) AddRaidCaught(ctx context.Context, guildID int64, c Caught) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `
|
||||||
|
INSERT INTO security_raid_caught (guild_id, user_id, action, name) VALUES ($1, $2, $3, $4)
|
||||||
|
ON CONFLICT (guild_id, user_id) DO NOTHING`, guildID, c.UserID, c.Action, c.Name)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListRaidCaught(ctx context.Context, guildID int64, limit int) ([]Caught, error) {
|
||||||
|
rows, err := r.db.QueryContext(ctx, `
|
||||||
|
SELECT user_id, action, name FROM security_raid_caught WHERE guild_id = $1 ORDER BY caught_at LIMIT $2`, guildID, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []Caught
|
||||||
|
for rows.Next() {
|
||||||
|
var c Caught
|
||||||
|
if err := rows.Scan(&c.UserID, &c.Action, &c.Name); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, c)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Snapshots ----
|
||||||
|
|
||||||
|
const keepSnapshots = 3
|
||||||
|
|
||||||
|
type Snapshot struct {
|
||||||
|
ID int64
|
||||||
|
GuildID int64
|
||||||
|
Data []byte
|
||||||
|
Roles int
|
||||||
|
Channels int
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveSnapshot stores a snapshot and drops all but the newest three.
|
||||||
|
func (r *Repo) SaveSnapshot(ctx context.Context, s Snapshot) error {
|
||||||
|
if _, err := r.db.ExecContext(ctx, `
|
||||||
|
INSERT INTO security_snapshots (guild_id, data, roles, channels) VALUES ($1, $2, $3, $4)`,
|
||||||
|
s.GuildID, s.Data, s.Roles, s.Channels); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err := r.db.ExecContext(ctx, `
|
||||||
|
DELETE FROM security_snapshots WHERE guild_id = $1 AND id NOT IN (
|
||||||
|
SELECT id FROM security_snapshots WHERE guild_id = $1 ORDER BY created_at DESC, id DESC LIMIT $2)`,
|
||||||
|
s.GuildID, keepSnapshots)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// LatestSnapshot returns nil when the guild has none.
|
||||||
|
func (r *Repo) LatestSnapshot(ctx context.Context, guildID int64) (*Snapshot, error) {
|
||||||
|
var s Snapshot
|
||||||
|
err := r.db.QueryRowContext(ctx, `
|
||||||
|
SELECT id, guild_id, data, roles, channels, created_at FROM security_snapshots
|
||||||
|
WHERE guild_id = $1 ORDER BY created_at DESC, id DESC LIMIT 1`, guildID).
|
||||||
|
Scan(&s.ID, &s.GuildID, &s.Data, &s.Roles, &s.Channels, &s.CreatedAt)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GuildsDueForSnapshot lists guilds with anti-nuke on whose newest snapshot
|
||||||
|
// is older than maxAge (or that have none).
|
||||||
|
func (r *Repo) GuildsDueForSnapshot(ctx context.Context, maxAge time.Duration) ([]int64, error) {
|
||||||
|
rows, err := r.db.QueryContext(ctx, `
|
||||||
|
SELECT s.guild_id FROM security_settings s
|
||||||
|
WHERE s.antinuke_enabled AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM security_snapshots p WHERE p.guild_id = s.guild_id AND p.created_at > NOW() - make_interval(secs => $1))`,
|
||||||
|
maxAge.Seconds())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []int64
|
||||||
|
for rows.Next() {
|
||||||
|
var g int64
|
||||||
|
if err := rows.Scan(&g); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, g)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
@@ -0,0 +1,231 @@
|
|||||||
|
//go:build integration
|
||||||
|
|
||||||
|
// Runs against a real Postgres with the dashboard API's migrations applied:
|
||||||
|
//
|
||||||
|
// SECURITY_TEST_DB=postgres://... go test -tags integration ./internal/db/repos/securityrepo
|
||||||
|
package securityrepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
_ "github.com/jackc/pgx/v5/stdlib"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testRepo(t *testing.T) (*Repo, int64) {
|
||||||
|
t.Helper()
|
||||||
|
dsn := os.Getenv("SECURITY_TEST_DB")
|
||||||
|
if dsn == "" {
|
||||||
|
t.Skip("SECURITY_TEST_DB not set")
|
||||||
|
}
|
||||||
|
db, err := sql.Open("pgx", dsn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { db.Close() })
|
||||||
|
const g = 777
|
||||||
|
for _, tbl := range []string{"security_settings", "security_trusted", "security_quarantines", "security_lockdowns", "security_events", "security_commands"} {
|
||||||
|
if _, err := db.Exec(`DELETE FROM `+tbl+` WHERE guild_id = $1`, g); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NewRepo(db), g
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSettings(t *testing.T) {
|
||||||
|
r, g := testRepo(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
s, err := r.GetSettings(ctx, g)
|
||||||
|
if err != nil || s.Configured || s.JoinGateEnabled || s.AntiNukeEnabled || s.UnauthorizedBot != ActionKick || s.AntiNukeLimits[LimitChannelDelete] != 2 || !s.AlertDM {
|
||||||
|
t.Fatalf("defaults wrong: %+v %v", s, err)
|
||||||
|
}
|
||||||
|
for _, f := range []string{FeatureJoinGate, FeatureAntiRaid, FeatureAntiNuke} {
|
||||||
|
if err := r.SetEnabled(ctx, g, f, true); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := r.SetEnabled(ctx, g, "drop table", true); err == nil {
|
||||||
|
t.Error("unknown features must be refused, never put into SQL")
|
||||||
|
}
|
||||||
|
_ = r.SetAlerts(ctx, g, 10, 20)
|
||||||
|
if _, err := r.db.Exec(`UPDATE security_settings SET alert_dm = false WHERE guild_id = $1`, g); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_ = r.SetQuarantineRole(ctx, g, 30)
|
||||||
|
_ = r.SetJoinLock(ctx, g, ActionBan)
|
||||||
|
if _, err := r.db.Exec(`UPDATE security_settings SET joingate_username_patterns = '{nitro,free}', antinuke_limits = '{"ban": 0}' WHERE guild_id = $1`, g); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err = r.GetSettings(ctx, g)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !s.Configured || s.AlertDM || !s.JoinGateEnabled || !s.AntiRaidEnabled || !s.AntiNukeEnabled || s.AlertChannelID != 10 || s.AlertRoleID != 20 || s.QuarantineRoleID != 30 || s.JoinLock != ActionBan {
|
||||||
|
t.Errorf("saved settings wrong: %+v", s)
|
||||||
|
}
|
||||||
|
if len(s.UsernamePatterns) != 2 || s.AntiNukeLimits[LimitBan] != 0 || s.AntiNukeLimits[LimitKick] != 5 {
|
||||||
|
t.Errorf("patterns or limits wrong (saved limits override defaults key by key): %+v %+v", s.UsernamePatterns, s.AntiNukeLimits)
|
||||||
|
}
|
||||||
|
if err := r.SetJoinLock(ctx, g, "everyone"); err == nil {
|
||||||
|
t.Error("the database must refuse unknown join lock modes")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrustedQuarantineLockdown(t *testing.T) {
|
||||||
|
r, g := testRepo(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
_ = r.AddTrusted(ctx, g, Trusted{TargetID: 5, Kind: "user", AddedBy: 1})
|
||||||
|
_ = r.AddTrusted(ctx, g, Trusted{TargetID: 6, Kind: "role", AddedBy: 1})
|
||||||
|
if err := r.AddTrusted(ctx, g, Trusted{TargetID: 7, Kind: "everyone", AddedBy: 1}); err == nil {
|
||||||
|
t.Error("unknown kinds must be refused")
|
||||||
|
}
|
||||||
|
list, _ := r.ListTrusted(ctx, g)
|
||||||
|
if len(list) != 2 {
|
||||||
|
t.Fatalf("trusted = %+v", list)
|
||||||
|
}
|
||||||
|
if ok, _ := r.RemoveTrusted(ctx, g, 5); !ok {
|
||||||
|
t.Error("remove should report true")
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = r.SaveQuarantine(ctx, Quarantine{GuildID: g, UserID: 9, PreviousRoleIDs: []int64{1, 2}, Reason: "first"})
|
||||||
|
_ = r.SaveQuarantine(ctx, Quarantine{GuildID: g, UserID: 9, PreviousRoleIDs: nil, Reason: "second"})
|
||||||
|
q, _ := r.GetQuarantine(ctx, g, 9)
|
||||||
|
if q == nil || len(q.PreviousRoleIDs) != 2 || q.Reason != "first" {
|
||||||
|
t.Errorf("the first quarantine record must be kept: %+v", q)
|
||||||
|
}
|
||||||
|
qs, _ := r.ListQuarantines(ctx, g)
|
||||||
|
if len(qs) != 1 {
|
||||||
|
t.Errorf("list = %+v", qs)
|
||||||
|
}
|
||||||
|
_ = r.DeleteQuarantine(ctx, g, 9)
|
||||||
|
if q, _ := r.GetQuarantine(ctx, g, 9); q != nil {
|
||||||
|
t.Error("deleted")
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = r.SaveLockdown(ctx, Lockdown{GuildID: g, ChannelID: 50, HadOverwrite: true, PreviousAllow: 1, PreviousDeny: 2, ServerWide: true, LockedBy: 1})
|
||||||
|
_ = r.SaveLockdown(ctx, Lockdown{GuildID: g, ChannelID: 50, HadOverwrite: false, PreviousAllow: 9, PreviousDeny: 9, LockedBy: 2})
|
||||||
|
_ = r.SaveLockdown(ctx, Lockdown{GuildID: g, ChannelID: 51, LockedBy: 1})
|
||||||
|
l, _ := r.GetLockdown(ctx, g, 50)
|
||||||
|
if l == nil || !l.HadOverwrite || l.PreviousAllow != 1 || !l.ServerWide {
|
||||||
|
t.Errorf("the first lockdown record must be kept: %+v", l)
|
||||||
|
}
|
||||||
|
if all, _ := r.ListLockdowns(ctx, g, false); len(all) != 2 {
|
||||||
|
t.Errorf("all = %+v", all)
|
||||||
|
}
|
||||||
|
if server, _ := r.ListLockdowns(ctx, g, true); len(server) != 1 {
|
||||||
|
t.Errorf("server-wide = %+v", server)
|
||||||
|
}
|
||||||
|
_ = r.DeleteLockdown(ctx, g, 50)
|
||||||
|
if l, _ := r.GetLockdown(ctx, g, 50); l != nil {
|
||||||
|
t.Error("deleted")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.AddEvent(ctx, Event{GuildID: g, Kind: EventNuke, ActorID: 3, Detail: "x"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := r.db.Exec(`INSERT INTO security_commands (guild_id, command_type, payload, requested_by) VALUES ($1, 'release_quarantine', '{"user_id":"9"}', 1)`, g); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := r.db.Exec(`INSERT INTO security_commands (guild_id, command_type, requested_by) VALUES ($1, 'grant_admin', 1)`, g); err == nil {
|
||||||
|
t.Error("unknown commands must be refused")
|
||||||
|
}
|
||||||
|
cmds, _ := r.ListUnprocessedCommands(ctx)
|
||||||
|
var mine *Command
|
||||||
|
for i := range cmds {
|
||||||
|
if cmds[i].GuildID == g {
|
||||||
|
mine = &cmds[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if mine == nil || string(mine.Payload) != `{"user_id": "9"}` {
|
||||||
|
t.Fatalf("command = %+v", mine)
|
||||||
|
}
|
||||||
|
_ = r.MarkCommandProcessed(ctx, mine.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRaidsSnapshotsAndHeatSettings(t *testing.T) {
|
||||||
|
r, g := testRepo(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
if _, err := r.db.Exec(`DELETE FROM security_raids WHERE guild_id = $1`, g); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_, _ = r.db.Exec(`DELETE FROM security_raid_caught WHERE guild_id = $1`, g)
|
||||||
|
_, _ = r.db.Exec(`DELETE FROM security_snapshots WHERE guild_id = $1`, g)
|
||||||
|
|
||||||
|
s, _ := r.GetSettings(ctx, g)
|
||||||
|
if s.HeatEnabled || s.HeatStrictness != "medium" || s.HeatTimeoutMinutes != 10 || !s.HeatEscalate || s.HeatPanicMembers != 3 {
|
||||||
|
t.Errorf("heat defaults wrong: %+v", s)
|
||||||
|
}
|
||||||
|
_ = r.SetEnabled(ctx, g, FeatureHeat, true)
|
||||||
|
if _, err := r.db.Exec(`UPDATE security_settings SET heat_blocked_words = '{scam}', antinuke_auto_restore = true WHERE guild_id = $1`, g); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
s, _ = r.GetSettings(ctx, g)
|
||||||
|
if !s.HeatEnabled || !s.AntiNukeAutoRestore || len(s.HeatBlockedWords) != 1 {
|
||||||
|
t.Errorf("heat settings not read back: %+v", s)
|
||||||
|
}
|
||||||
|
if _, err := r.db.Exec(`UPDATE security_settings SET heat_strictness = 'insane' WHERE guild_id = $1`, g); err == nil {
|
||||||
|
t.Error("unknown strictness must be refused by the database")
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now().Add(-time.Minute).UTC().Truncate(time.Millisecond)
|
||||||
|
_ = r.SaveRaid(ctx, Raid{GuildID: g, StartedAt: start, LastJoinAt: start, InvitesPaused: true, CaughtTotal: 1})
|
||||||
|
_ = r.SaveRaid(ctx, Raid{GuildID: g, StartedAt: time.Now(), LastJoinAt: start.Add(time.Second), InvitesPaused: true, CaughtTotal: 2})
|
||||||
|
_ = r.AddRaidCaught(ctx, g, Caught{UserID: 1, Action: "kick", Name: "a"})
|
||||||
|
_ = r.AddRaidCaught(ctx, g, Caught{UserID: 1, Action: "ban", Name: "a"})
|
||||||
|
_ = r.AddRaidCaught(ctx, g, Caught{UserID: 2, Action: "kick", Name: "b"})
|
||||||
|
raids, _ := r.ListRaids(ctx)
|
||||||
|
var mine *Raid
|
||||||
|
for i := range raids {
|
||||||
|
if raids[i].GuildID == g {
|
||||||
|
mine = &raids[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if mine == nil || !mine.StartedAt.Equal(start) || mine.CaughtTotal != 2 {
|
||||||
|
t.Fatalf("raid = %+v (the start time is kept on update)", mine)
|
||||||
|
}
|
||||||
|
caught, _ := r.ListRaidCaught(ctx, g, 10)
|
||||||
|
if len(caught) != 2 || caught[0].Action != "kick" {
|
||||||
|
t.Errorf("caught = %+v (one row per raider)", caught)
|
||||||
|
}
|
||||||
|
_ = r.DeleteRaid(ctx, g)
|
||||||
|
if c, _ := r.ListRaidCaught(ctx, g, 10); len(c) != 0 {
|
||||||
|
t.Error("deleting a raid clears its list")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
if err := r.SaveSnapshot(ctx, Snapshot{GuildID: g, Data: []byte(`{"roles":[],"channels":[]}`), Roles: i}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var n int
|
||||||
|
_ = r.db.QueryRow(`SELECT COUNT(*) FROM security_snapshots WHERE guild_id = $1`, g).Scan(&n)
|
||||||
|
if n != 3 {
|
||||||
|
t.Errorf("only the last three snapshots are kept, have %d", n)
|
||||||
|
}
|
||||||
|
latest, _ := r.LatestSnapshot(ctx, g)
|
||||||
|
if latest == nil || latest.Roles != 4 {
|
||||||
|
t.Errorf("latest = %+v", latest)
|
||||||
|
}
|
||||||
|
_ = r.SetEnabled(ctx, g, FeatureAntiNuke, true)
|
||||||
|
due, _ := r.GuildsDueForSnapshot(ctx, time.Hour)
|
||||||
|
for _, d := range due {
|
||||||
|
if d == g {
|
||||||
|
t.Error("a guild with a fresh snapshot is not due")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
due, _ = r.GuildsDueForSnapshot(ctx, 0)
|
||||||
|
found := false
|
||||||
|
for _, d := range due {
|
||||||
|
found = found || d == g
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Error("with a zero max age every anti-nuke guild is due")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,10 +15,106 @@ type LevelReward struct {
|
|||||||
RoleID int64
|
RoleID int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LogSettings struct {
|
||||||
|
GuildID int64
|
||||||
|
ChannelID int64
|
||||||
|
IsEnabled bool
|
||||||
|
}
|
||||||
|
|
||||||
func NewRepo(db *sql.DB) *Repo {
|
func NewRepo(db *sql.DB) *Repo {
|
||||||
return &Repo{db: db}
|
return &Repo{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetMeetingLobbyChannel(ctx context.Context, guildID int64, lobbyChannelID int64) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO meeting_settings (guild_id, lobby_channel_id)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
ON CONFLICT (guild_id)
|
||||||
|
DO UPDATE SET lobby_channel_id = EXCLUDED.lobby_channel_id
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, lobbyChannelID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) GetMeetingLobbyChannel(ctx context.Context, guildID int64) (channelID int64, ok bool, err error) {
|
||||||
|
const q = `
|
||||||
|
SELECT lobby_channel_id
|
||||||
|
FROM meeting_settings
|
||||||
|
WHERE guild_id = $1
|
||||||
|
`
|
||||||
|
row := r.db.QueryRowContext(ctx, q, guildID)
|
||||||
|
var ch sql.NullInt64
|
||||||
|
switch err := row.Scan(&ch); err {
|
||||||
|
case nil:
|
||||||
|
if !ch.Valid {
|
||||||
|
return 0, false, nil
|
||||||
|
}
|
||||||
|
return ch.Int64, true, nil
|
||||||
|
case sql.ErrNoRows:
|
||||||
|
return 0, false, nil
|
||||||
|
default:
|
||||||
|
return 0, false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) AddMeetingTempChannel(ctx context.Context, guildID, channelID, createdBy int64) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO meeting_temp_channels (guild_id, channel_id, created_by)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
ON CONFLICT (guild_id, channel_id) DO NOTHING
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, channelID, createdBy)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) RemoveMeetingTempChannel(ctx context.Context, guildID, channelID int64) error {
|
||||||
|
const q = `
|
||||||
|
DELETE FROM meeting_temp_channels
|
||||||
|
WHERE guild_id = $1 AND channel_id = $2
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, channelID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) IsMeetingTempChannel(ctx context.Context, guildID, channelID int64) (bool, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT 1
|
||||||
|
FROM meeting_temp_channels
|
||||||
|
WHERE guild_id = $1 AND channel_id = $2
|
||||||
|
`
|
||||||
|
row := r.db.QueryRowContext(ctx, q, guildID, channelID)
|
||||||
|
var one int
|
||||||
|
switch err := row.Scan(&one); err {
|
||||||
|
case nil:
|
||||||
|
return true, nil
|
||||||
|
case sql.ErrNoRows:
|
||||||
|
return false, nil
|
||||||
|
default:
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) GetMeetingTempChannelOwner(ctx context.Context, guildID, channelID int64) (createdBy int64, ok bool, err error) {
|
||||||
|
const q = `
|
||||||
|
SELECT created_by
|
||||||
|
FROM meeting_temp_channels
|
||||||
|
WHERE guild_id = $1 AND channel_id = $2
|
||||||
|
`
|
||||||
|
row := r.db.QueryRowContext(ctx, q, guildID, channelID)
|
||||||
|
var cb sql.NullInt64
|
||||||
|
switch err := row.Scan(&cb); err {
|
||||||
|
case nil:
|
||||||
|
if !cb.Valid {
|
||||||
|
return 0, false, nil
|
||||||
|
}
|
||||||
|
return cb.Int64, true, nil
|
||||||
|
case sql.ErrNoRows:
|
||||||
|
return 0, false, nil
|
||||||
|
default:
|
||||||
|
return 0, false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Repo) SetLevelSystemEnabled(ctx context.Context, guildID int64, enabled bool) error {
|
func (r *Repo) SetLevelSystemEnabled(ctx context.Context, guildID int64, enabled bool) error {
|
||||||
const q = `
|
const q = `
|
||||||
INSERT INTO levelsettings (guild_id, levelsys)
|
INSERT INTO levelsettings (guild_id, levelsys)
|
||||||
@@ -150,6 +246,80 @@ func (r *Repo) SetLevelUpMessage(ctx context.Context, guildID int64, message str
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetLogChannel(ctx context.Context, guildID int64, channelID int64) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO logsettings (guild_id, log_channel_id, is_enabled)
|
||||||
|
VALUES ($1, $2, TRUE)
|
||||||
|
ON CONFLICT (guild_id)
|
||||||
|
DO UPDATE SET log_channel_id = EXCLUDED.log_channel_id
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, channelID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetLogEnabled(ctx context.Context, guildID int64, enabled bool) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO logsettings (guild_id, is_enabled)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
ON CONFLICT (guild_id)
|
||||||
|
DO UPDATE SET is_enabled = EXCLUDED.is_enabled
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, enabled)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) GetLogSettings(ctx context.Context, guildID int64) (*LogSettings, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT log_channel_id, is_enabled
|
||||||
|
FROM logsettings
|
||||||
|
WHERE guild_id = $1
|
||||||
|
`
|
||||||
|
row := r.db.QueryRowContext(ctx, q, guildID)
|
||||||
|
var channelID sql.NullInt64
|
||||||
|
var enabled sql.NullBool
|
||||||
|
switch err := row.Scan(&channelID, &enabled); err {
|
||||||
|
case nil:
|
||||||
|
out := &LogSettings{
|
||||||
|
GuildID: guildID,
|
||||||
|
ChannelID: 0,
|
||||||
|
IsEnabled: false,
|
||||||
|
}
|
||||||
|
if channelID.Valid {
|
||||||
|
out.ChannelID = channelID.Int64
|
||||||
|
}
|
||||||
|
if enabled.Valid {
|
||||||
|
out.IsEnabled = enabled.Bool
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
case sql.ErrNoRows:
|
||||||
|
return &LogSettings{
|
||||||
|
GuildID: guildID,
|
||||||
|
ChannelID: 0,
|
||||||
|
IsEnabled: false,
|
||||||
|
}, nil
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) HasLogSettings(ctx context.Context, guildID int64) (bool, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT 1
|
||||||
|
FROM logsettings
|
||||||
|
WHERE guild_id = $1
|
||||||
|
`
|
||||||
|
row := r.db.QueryRowContext(ctx, q, guildID)
|
||||||
|
var one int
|
||||||
|
switch err := row.Scan(&one); err {
|
||||||
|
case nil:
|
||||||
|
return true, nil
|
||||||
|
case sql.ErrNoRows:
|
||||||
|
return false, nil
|
||||||
|
default:
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Repo) GetLevelUpMessage(ctx context.Context, guildID int64) (string, error) {
|
func (r *Repo) GetLevelUpMessage(ctx context.Context, guildID int64) (string, error) {
|
||||||
const q = `
|
const q = `
|
||||||
SELECT message
|
SELECT message
|
||||||
@@ -170,3 +340,22 @@ func (r *Repo) GetLevelUpMessage(ctx context.Context, guildID int64) (string, er
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Repo) IsMusicEnabled(ctx context.Context, guildID int64) (bool, error) {
|
||||||
|
const q = `SELECT is_enabled FROM music_settings WHERE guild_id = $1`
|
||||||
|
var enabled bool
|
||||||
|
err := r.db.QueryRowContext(ctx, q, guildID).Scan(&enabled)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return enabled, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetMusicEnabled(ctx context.Context, guildID int64, enabled bool) error {
|
||||||
|
const q = `INSERT INTO music_settings (guild_id, is_enabled) VALUES ($1, $2) ON CONFLICT (guild_id) DO UPDATE SET is_enabled = $2`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, enabled)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,303 @@
|
|||||||
|
// Package ticketrepo stores the ticket system's settings, categories, tickets
|
||||||
|
// and the dashboard's requests to the bot. Discord IDs are int64; 0 means
|
||||||
|
// "not set" for optional ones.
|
||||||
|
package ticketrepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepo(db *sql.DB) *Repo {
|
||||||
|
return &Repo{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
DefaultPanelTitle = "Need help?"
|
||||||
|
DefaultPanelDescription = "Pick a category below to open a private ticket with the staff team."
|
||||||
|
DefaultMaxOpenPerUser = 1
|
||||||
|
|
||||||
|
StatusOpen = "open"
|
||||||
|
StatusClosed = "closed"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Settings struct {
|
||||||
|
GuildID int64
|
||||||
|
Enabled bool
|
||||||
|
ChannelID int64
|
||||||
|
LogChannelID int64
|
||||||
|
PanelMessageID int64
|
||||||
|
PanelTitle string
|
||||||
|
PanelDescription string
|
||||||
|
MaxOpenPerUser int
|
||||||
|
// Configured is false when the guild has no settings row yet; the other
|
||||||
|
// fields then hold the defaults.
|
||||||
|
Configured bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type Category struct {
|
||||||
|
ID int64
|
||||||
|
GuildID int64
|
||||||
|
Name string
|
||||||
|
Emoji string
|
||||||
|
Description string
|
||||||
|
StaffRoleID int64
|
||||||
|
Position int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Ticket struct {
|
||||||
|
ID int64
|
||||||
|
GuildID int64
|
||||||
|
Number int
|
||||||
|
CategoryID int64
|
||||||
|
CategoryName string
|
||||||
|
ThreadID int64
|
||||||
|
IntroMessage int64
|
||||||
|
OpenerID int64
|
||||||
|
Subject string
|
||||||
|
Status string
|
||||||
|
ClaimedBy int64
|
||||||
|
ClosedBy int64
|
||||||
|
CloseReason string
|
||||||
|
CreatedAt time.Time
|
||||||
|
ClosedAt time.Time // zero while open
|
||||||
|
}
|
||||||
|
|
||||||
|
type Command struct {
|
||||||
|
ID int64
|
||||||
|
GuildID int64
|
||||||
|
CommandType string
|
||||||
|
RequestedBy int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func nullID(v int64) sql.NullInt64 {
|
||||||
|
return sql.NullInt64{Int64: v, Valid: v != 0}
|
||||||
|
}
|
||||||
|
|
||||||
|
func nullText(v string) sql.NullString {
|
||||||
|
return sql.NullString{String: v, Valid: v != ""}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) GetSettings(ctx context.Context, guildID int64) (*Settings, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT enabled, channel_id, log_channel_id, panel_message_id, panel_title, panel_description, max_open_per_user
|
||||||
|
FROM ticket_settings WHERE guild_id = $1`
|
||||||
|
var ch, logCh, panel sql.NullInt64
|
||||||
|
s := &Settings{GuildID: guildID}
|
||||||
|
err := r.db.QueryRowContext(ctx, q, guildID).Scan(&s.Enabled, &ch, &logCh, &panel, &s.PanelTitle, &s.PanelDescription, &s.MaxOpenPerUser)
|
||||||
|
switch {
|
||||||
|
case errors.Is(err, sql.ErrNoRows):
|
||||||
|
// Modules start off until the server sets them up.
|
||||||
|
return &Settings{
|
||||||
|
GuildID: guildID,
|
||||||
|
Enabled: false,
|
||||||
|
PanelTitle: DefaultPanelTitle,
|
||||||
|
PanelDescription: DefaultPanelDescription,
|
||||||
|
MaxOpenPerUser: DefaultMaxOpenPerUser,
|
||||||
|
}, nil
|
||||||
|
case err != nil:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.ChannelID, s.LogChannelID, s.PanelMessageID = ch.Int64, logCh.Int64, panel.Int64
|
||||||
|
s.Configured = true
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetChannels saves where tickets live and where transcripts go. A zero log
|
||||||
|
// channel clears it. Setting tickets up for the first time turns them on
|
||||||
|
// (they start off); after that, the on/off switch is left alone.
|
||||||
|
func (r *Repo) SetChannels(ctx context.Context, guildID, channelID, logChannelID int64) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO ticket_settings (guild_id, channel_id, log_channel_id, enabled)
|
||||||
|
VALUES ($1, $2, $3, TRUE)
|
||||||
|
ON CONFLICT (guild_id) DO UPDATE SET
|
||||||
|
channel_id = EXCLUDED.channel_id,
|
||||||
|
log_channel_id = EXCLUDED.log_channel_id`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, nullID(channelID), nullID(logChannelID))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetPanelMessage(ctx context.Context, guildID, messageID int64) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO ticket_settings (guild_id, panel_message_id) VALUES ($1, $2)
|
||||||
|
ON CONFLICT (guild_id) DO UPDATE SET panel_message_id = EXCLUDED.panel_message_id`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, nullID(messageID))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// NextNumber hands out the guild's next ticket number. It is a single
|
||||||
|
// statement, so two tickets opened at the same moment never share a number.
|
||||||
|
func (r *Repo) NextNumber(ctx context.Context, guildID int64) (int, error) {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO ticket_settings (guild_id, next_number) VALUES ($1, 2)
|
||||||
|
ON CONFLICT (guild_id) DO UPDATE SET next_number = ticket_settings.next_number + 1
|
||||||
|
RETURNING next_number - 1`
|
||||||
|
var n int
|
||||||
|
err := r.db.QueryRowContext(ctx, q, guildID).Scan(&n)
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const categoryCols = `id, guild_id, name, COALESCE(emoji, ''), description, COALESCE(staff_role_id, 0), position`
|
||||||
|
|
||||||
|
func scanCategory(row interface{ Scan(...any) error }) (Category, error) {
|
||||||
|
var c Category
|
||||||
|
err := row.Scan(&c.ID, &c.GuildID, &c.Name, &c.Emoji, &c.Description, &c.StaffRoleID, &c.Position)
|
||||||
|
return c, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListCategories(ctx context.Context, guildID int64) ([]Category, error) {
|
||||||
|
q := `SELECT ` + categoryCols + ` FROM ticket_categories WHERE guild_id = $1 ORDER BY position, id`
|
||||||
|
rows, err := r.db.QueryContext(ctx, q, guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []Category
|
||||||
|
for rows.Next() {
|
||||||
|
c, err := scanCategory(rows)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, c)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCategory returns nil when the category does not exist in that guild.
|
||||||
|
func (r *Repo) GetCategory(ctx context.Context, guildID, id int64) (*Category, error) {
|
||||||
|
q := `SELECT ` + categoryCols + ` FROM ticket_categories WHERE guild_id = $1 AND id = $2`
|
||||||
|
c, err := scanCategory(r.db.QueryRowContext(ctx, q, guildID, id))
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddCategory appends a category after the existing ones.
|
||||||
|
func (r *Repo) AddCategory(ctx context.Context, c Category) (*Category, error) {
|
||||||
|
q := `
|
||||||
|
INSERT INTO ticket_categories (guild_id, name, emoji, description, staff_role_id, position)
|
||||||
|
VALUES ($1, $2, $3, $4, $5,
|
||||||
|
(SELECT COALESCE(MAX(position) + 1, 0) FROM ticket_categories WHERE guild_id = $1))
|
||||||
|
RETURNING ` + categoryCols
|
||||||
|
out, err := scanCategory(r.db.QueryRowContext(ctx, q, c.GuildID, c.Name, nullText(c.Emoji), c.Description, nullID(c.StaffRoleID)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteCategoryByName removes a category (case-insensitively) and reports
|
||||||
|
// whether one was found. Tickets keep their copied category name.
|
||||||
|
func (r *Repo) DeleteCategoryByName(ctx context.Context, guildID int64, name string) (bool, error) {
|
||||||
|
const q = `DELETE FROM ticket_categories WHERE guild_id = $1 AND lower(name) = lower($2)`
|
||||||
|
res, err := r.db.ExecContext(ctx, q, guildID, name)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
n, err := res.RowsAffected()
|
||||||
|
return n > 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) CountOpenByOpener(ctx context.Context, guildID, openerID int64) (int, error) {
|
||||||
|
const q = `SELECT COUNT(*) FROM tickets WHERE guild_id = $1 AND opener_id = $2 AND status = 'open'`
|
||||||
|
var n int
|
||||||
|
err := r.db.QueryRowContext(ctx, q, guildID, openerID).Scan(&n)
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) CreateTicket(ctx context.Context, t Ticket) (int64, error) {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO tickets (guild_id, number, category_id, category_name, thread_id, intro_message_id, opener_id, subject)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
|
RETURNING id`
|
||||||
|
var id int64
|
||||||
|
err := r.db.QueryRowContext(ctx, q, t.GuildID, t.Number, nullID(t.CategoryID), t.CategoryName, t.ThreadID, nullID(t.IntroMessage), t.OpenerID, t.Subject).Scan(&id)
|
||||||
|
return id, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const ticketCols = `id, guild_id, number, COALESCE(category_id, 0), category_name, thread_id, COALESCE(intro_message_id, 0), opener_id, subject, status,
|
||||||
|
COALESCE(claimed_by, 0), COALESCE(closed_by, 0), COALESCE(close_reason, ''), created_at, closed_at`
|
||||||
|
|
||||||
|
func scanTicket(row interface{ Scan(...any) error }) (Ticket, error) {
|
||||||
|
var t Ticket
|
||||||
|
var closedAt sql.NullTime
|
||||||
|
err := row.Scan(&t.ID, &t.GuildID, &t.Number, &t.CategoryID, &t.CategoryName, &t.ThreadID, &t.IntroMessage, &t.OpenerID, &t.Subject, &t.Status,
|
||||||
|
&t.ClaimedBy, &t.ClosedBy, &t.CloseReason, &t.CreatedAt, &closedAt)
|
||||||
|
t.ClosedAt = closedAt.Time
|
||||||
|
return t, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTicketByThread returns nil when the thread is not a ticket.
|
||||||
|
func (r *Repo) GetTicketByThread(ctx context.Context, threadID int64) (*Ticket, error) {
|
||||||
|
q := `SELECT ` + ticketCols + ` FROM tickets WHERE thread_id = $1`
|
||||||
|
t, err := scanTicket(r.db.QueryRowContext(ctx, q, threadID))
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Claim sets the ticket's handler unless someone already has it, and returns
|
||||||
|
// whoever holds it afterwards (the caller when the claim worked).
|
||||||
|
func (r *Repo) Claim(ctx context.Context, threadID, staffID int64) (int64, error) {
|
||||||
|
const q = `
|
||||||
|
UPDATE tickets SET claimed_by = COALESCE(claimed_by, $2)
|
||||||
|
WHERE thread_id = $1 AND status = 'open'
|
||||||
|
RETURNING claimed_by`
|
||||||
|
var holder int64
|
||||||
|
err := r.db.QueryRowContext(ctx, q, threadID, staffID).Scan(&holder)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
return holder, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close marks an open ticket closed and reports whether this call closed it,
|
||||||
|
// so two people pressing Close at once only produce one transcript.
|
||||||
|
func (r *Repo) Close(ctx context.Context, threadID, closedBy int64, reason string) (bool, error) {
|
||||||
|
const q = `
|
||||||
|
UPDATE tickets SET status = 'closed', closed_by = $2, close_reason = $3, closed_at = NOW()
|
||||||
|
WHERE thread_id = $1 AND status = 'open'`
|
||||||
|
res, err := r.db.ExecContext(ctx, q, threadID, nullID(closedBy), nullText(reason))
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
n, err := res.RowsAffected()
|
||||||
|
return n > 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListUnprocessedCommands(ctx context.Context) ([]Command, error) {
|
||||||
|
const q = `SELECT id, guild_id, command_type, requested_by FROM ticket_commands WHERE processed_at IS NULL ORDER BY id`
|
||||||
|
rows, err := r.db.QueryContext(ctx, q)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var out []Command
|
||||||
|
for rows.Next() {
|
||||||
|
var c Command
|
||||||
|
if err := rows.Scan(&c.ID, &c.GuildID, &c.CommandType, &c.RequestedBy); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, c)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) MarkCommandProcessed(ctx context.Context, id int64) error {
|
||||||
|
_, err := r.db.ExecContext(ctx, `UPDATE ticket_commands SET processed_at = NOW() WHERE id = $1`, id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,206 @@
|
|||||||
|
//go:build integration
|
||||||
|
|
||||||
|
// Runs against a real Postgres with the dashboard API's migrations applied:
|
||||||
|
//
|
||||||
|
// TICKETS_TEST_DB=postgres://... go test -tags integration ./internal/db/repos/ticketrepo
|
||||||
|
package ticketrepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
_ "github.com/jackc/pgx/v5/stdlib"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testRepo(t *testing.T) (*Repo, int64) {
|
||||||
|
t.Helper()
|
||||||
|
dsn := os.Getenv("TICKETS_TEST_DB")
|
||||||
|
if dsn == "" {
|
||||||
|
t.Skip("TICKETS_TEST_DB not set")
|
||||||
|
}
|
||||||
|
db, err := sql.Open("pgx", dsn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { db.Close() })
|
||||||
|
|
||||||
|
const guild = 424242
|
||||||
|
for _, q := range []string{
|
||||||
|
`DELETE FROM tickets WHERE guild_id = $1`,
|
||||||
|
`DELETE FROM ticket_categories WHERE guild_id = $1`,
|
||||||
|
`DELETE FROM ticket_settings WHERE guild_id = $1`,
|
||||||
|
`DELETE FROM ticket_commands WHERE guild_id = $1`,
|
||||||
|
} {
|
||||||
|
if _, err := db.Exec(q, guild); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NewRepo(db), guild
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSettingsDefaultsAndChannels(t *testing.T) {
|
||||||
|
r, g := testRepo(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
s, err := r.GetSettings(ctx, g)
|
||||||
|
if err != nil || s.Configured || !s.Enabled || s.PanelTitle != DefaultPanelTitle || s.MaxOpenPerUser != 1 {
|
||||||
|
t.Fatalf("defaults wrong: %+v %v", s, err)
|
||||||
|
}
|
||||||
|
if err := r.SetChannels(ctx, g, 111, 222); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := r.SetPanelMessage(ctx, g, 333); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
s, _ = r.GetSettings(ctx, g)
|
||||||
|
if !s.Configured || s.ChannelID != 111 || s.LogChannelID != 222 || s.PanelMessageID != 333 {
|
||||||
|
t.Fatalf("saved settings wrong: %+v", s)
|
||||||
|
}
|
||||||
|
// Clearing the log channel.
|
||||||
|
_ = r.SetChannels(ctx, g, 111, 0)
|
||||||
|
s, _ = r.GetSettings(ctx, g)
|
||||||
|
if s.LogChannelID != 0 {
|
||||||
|
t.Errorf("log channel not cleared: %+v", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNextNumberIsUniqueUnderConcurrency(t *testing.T) {
|
||||||
|
r, g := testRepo(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
var mu sync.Mutex
|
||||||
|
seen := map[int]bool{}
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
n, err := r.NextNumber(ctx, g)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
if seen[n] {
|
||||||
|
t.Errorf("number %d handed out twice", n)
|
||||||
|
}
|
||||||
|
seen[n] = true
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
for n := 1; n <= 20; n++ {
|
||||||
|
if !seen[n] {
|
||||||
|
t.Errorf("number %d missing: numbers should be 1..20", n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCategories(t *testing.T) {
|
||||||
|
r, g := testRepo(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
a, err := r.AddCategory(ctx, Category{GuildID: g, Name: "Support", Emoji: "🛟", StaffRoleID: 9})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
b, _ := r.AddCategory(ctx, Category{GuildID: g, Name: "Appeal", Description: "Ban appeals"})
|
||||||
|
if a.Position != 0 || b.Position != 1 || b.Emoji != "" || b.StaffRoleID != 0 {
|
||||||
|
t.Fatalf("positions or defaults wrong: %+v %+v", a, b)
|
||||||
|
}
|
||||||
|
list, _ := r.ListCategories(ctx, g)
|
||||||
|
if len(list) != 2 || list[0].Name != "Support" || list[1].Description != "Ban appeals" {
|
||||||
|
t.Fatalf("list wrong: %+v", list)
|
||||||
|
}
|
||||||
|
if c, _ := r.GetCategory(ctx, g, a.ID); c == nil || c.Emoji != "🛟" {
|
||||||
|
t.Errorf("get wrong: %+v", c)
|
||||||
|
}
|
||||||
|
if c, _ := r.GetCategory(ctx, g+1, a.ID); c != nil {
|
||||||
|
t.Error("a category must not be readable from another guild")
|
||||||
|
}
|
||||||
|
if ok, _ := r.DeleteCategoryByName(ctx, g, "support"); !ok {
|
||||||
|
t.Error("delete should match case-insensitively")
|
||||||
|
}
|
||||||
|
if ok, _ := r.DeleteCategoryByName(ctx, g, "nope"); ok {
|
||||||
|
t.Error("deleting a missing category should report false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTicketLifecycle(t *testing.T) {
|
||||||
|
r, g := testRepo(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
cat, _ := r.AddCategory(ctx, Category{GuildID: g, Name: "Support"})
|
||||||
|
id, err := r.CreateTicket(ctx, Ticket{GuildID: g, Number: 1, CategoryID: cat.ID, CategoryName: "Support", ThreadID: 5001, IntroMessage: 6001, OpenerID: 77, Subject: "Help"})
|
||||||
|
if err != nil || id == 0 {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if n, _ := r.CountOpenByOpener(ctx, g, 77); n != 1 {
|
||||||
|
t.Errorf("open count = %d", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
if holder, _ := r.Claim(ctx, 5001, 10); holder != 10 {
|
||||||
|
t.Errorf("first claim should win, holder = %d", holder)
|
||||||
|
}
|
||||||
|
if holder, _ := r.Claim(ctx, 5001, 11); holder != 10 {
|
||||||
|
t.Errorf("second claim must not take over, holder = %d", holder)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok, _ := r.Close(ctx, 5001, 10, "solved"); !ok {
|
||||||
|
t.Error("first close should succeed")
|
||||||
|
}
|
||||||
|
if ok, _ := r.Close(ctx, 5001, 11, ""); ok {
|
||||||
|
t.Error("closing twice must report false")
|
||||||
|
}
|
||||||
|
tk, _ := r.GetTicketByThread(ctx, 5001)
|
||||||
|
if tk == nil || tk.Status != StatusClosed || tk.ClosedBy != 10 || tk.CloseReason != "solved" || tk.ClosedAt.IsZero() || tk.ClaimedBy != 10 || tk.IntroMessage != 6001 {
|
||||||
|
t.Fatalf("closed ticket wrong: %+v", tk)
|
||||||
|
}
|
||||||
|
if n, _ := r.CountOpenByOpener(ctx, g, 77); n != 0 {
|
||||||
|
t.Errorf("closed ticket still counted as open")
|
||||||
|
}
|
||||||
|
if holder, _ := r.Claim(ctx, 5001, 12); holder != 0 {
|
||||||
|
t.Error("a closed ticket cannot be claimed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deleting the category keeps the ticket and its copied name.
|
||||||
|
_, _ = r.DeleteCategoryByName(ctx, g, "Support")
|
||||||
|
tk, _ = r.GetTicketByThread(ctx, 5001)
|
||||||
|
if tk.CategoryID != 0 || tk.CategoryName != "Support" {
|
||||||
|
t.Errorf("ticket after category delete: %+v", tk)
|
||||||
|
}
|
||||||
|
if tk, _ := r.GetTicketByThread(ctx, 999); tk != nil {
|
||||||
|
t.Error("unknown thread should return nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCommands(t *testing.T) {
|
||||||
|
r, g := testRepo(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
if _, err := r.db.Exec(`INSERT INTO ticket_commands (guild_id, command_type, requested_by) VALUES ($1, 'post_panel', 5)`, g); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
cmds, err := r.ListUnprocessedCommands(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var mine *Command
|
||||||
|
for i := range cmds {
|
||||||
|
if cmds[i].GuildID == g {
|
||||||
|
mine = &cmds[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if mine == nil || mine.CommandType != "post_panel" || mine.RequestedBy != 5 {
|
||||||
|
t.Fatalf("command not listed: %+v", cmds)
|
||||||
|
}
|
||||||
|
_ = r.MarkCommandProcessed(ctx, mine.ID)
|
||||||
|
cmds, _ = r.ListUnprocessedCommands(ctx)
|
||||||
|
for _, c := range cmds {
|
||||||
|
if c.ID == mine.ID {
|
||||||
|
t.Error("processed command still listed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
package twitchrepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepo(db *sql.DB) *Repo {
|
||||||
|
return &Repo{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListGuilds(ctx context.Context) ([]int64, error) {
|
||||||
|
const q = `SELECT DISTINCT guild_id FROM twitch`
|
||||||
|
rows, err := r.db.QueryContext(ctx, q)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var ids []int64
|
||||||
|
for rows.Next() {
|
||||||
|
var gid int64
|
||||||
|
if err := rows.Scan(&gid); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ids = append(ids, gid)
|
||||||
|
}
|
||||||
|
return ids, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) ListUsersForGuild(ctx context.Context, guildID int64) ([]string, error) {
|
||||||
|
const q = `SELECT twitch_user FROM twitch WHERE guild_id = $1`
|
||||||
|
rows, err := r.db.QueryContext(ctx, q, guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var users []string
|
||||||
|
for rows.Next() {
|
||||||
|
var u string
|
||||||
|
if err := rows.Scan(&u); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
users = append(users, u)
|
||||||
|
}
|
||||||
|
return users, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) GetStatus(ctx context.Context, guildID int64, username string) (string, bool, error) {
|
||||||
|
const q = `SELECT status FROM twitch WHERE guild_id = $1 AND twitch_user = $2`
|
||||||
|
var status string
|
||||||
|
err := r.db.QueryRowContext(ctx, q, guildID, username).Scan(&status)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return "", false, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", false, err
|
||||||
|
}
|
||||||
|
return status, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) UpsertStreamer(ctx context.Context, guildID int64, username string) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO twitch (twitch_user, guild_id, status)
|
||||||
|
VALUES ($1, $2, 'not live')
|
||||||
|
ON CONFLICT (twitch_user, guild_id)
|
||||||
|
DO NOTHING
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, username, guildID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) RemoveStreamer(ctx context.Context, guildID int64, username string) error {
|
||||||
|
const q = `DELETE FROM twitch WHERE guild_id = $1 AND twitch_user = $2`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, username)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) UpdateStatus(ctx context.Context, guildID int64, username, status string) error {
|
||||||
|
const q = `UPDATE twitch SET status = $1 WHERE guild_id = $2 AND twitch_user = $3`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, status, guildID, username)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) GetNotificationChannel(ctx context.Context, guildID int64) (int64, bool, error) {
|
||||||
|
const q = `SELECT twitch_channel_id FROM twitch_config WHERE guild_id = $1`
|
||||||
|
var chID sql.NullInt64
|
||||||
|
if err := r.db.QueryRowContext(ctx, q, guildID).Scan(&chID); err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return 0, false, nil
|
||||||
|
}
|
||||||
|
return 0, false, err
|
||||||
|
}
|
||||||
|
if !chID.Valid {
|
||||||
|
return 0, false, nil
|
||||||
|
}
|
||||||
|
return chID.Int64, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetNotificationChannel sets where live alerts go. Setting it for the first
|
||||||
|
// time turns alerts on (they start off); after that, the on/off switch is
|
||||||
|
// left alone.
|
||||||
|
func (r *Repo) SetNotificationChannel(ctx context.Context, guildID, channelID int64) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO twitch_config (guild_id, twitch_channel_id, enabled)
|
||||||
|
VALUES ($1, $2, TRUE)
|
||||||
|
ON CONFLICT (guild_id)
|
||||||
|
DO UPDATE SET twitch_channel_id = EXCLUDED.twitch_channel_id
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, channelID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsEnabled reports whether Twitch notifications are enabled for guildID.
|
||||||
|
// A guild with no twitch_config row yet is off: modules start off until
|
||||||
|
// the server sets them up.
|
||||||
|
func (r *Repo) IsEnabled(ctx context.Context, guildID int64) (bool, error) {
|
||||||
|
const q = `SELECT enabled FROM twitch_config WHERE guild_id = $1`
|
||||||
|
var enabled bool
|
||||||
|
err := r.db.QueryRowContext(ctx, q, guildID).Scan(&enabled)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return enabled, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetEnabled(ctx context.Context, guildID int64, enabled bool) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO twitch_config (guild_id, enabled)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
ON CONFLICT (guild_id)
|
||||||
|
DO UPDATE SET enabled = EXCLUDED.enabled
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, enabled)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package usersettingsrepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepo(db *sql.DB) *Repo {
|
||||||
|
return &Repo{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) SetTimezone(ctx context.Context, userID int64, zone string) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO user_settings (user_id, timezone)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
ON CONFLICT (user_id)
|
||||||
|
DO UPDATE SET timezone = EXCLUDED.timezone
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, userID, zone)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) GetTimezone(ctx context.Context, userID int64) (string, bool, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT timezone
|
||||||
|
FROM user_settings
|
||||||
|
WHERE user_id = $1
|
||||||
|
`
|
||||||
|
row := r.db.QueryRowContext(ctx, q, userID)
|
||||||
|
var zone string
|
||||||
|
switch err := row.Scan(&zone); err {
|
||||||
|
case nil:
|
||||||
|
return zone, true, nil
|
||||||
|
case sql.ErrNoRows:
|
||||||
|
return "", false, nil
|
||||||
|
default:
|
||||||
|
return "", false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
package welcomerepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
type Settings struct {
|
||||||
|
GuildID int64
|
||||||
|
ChannelID int64
|
||||||
|
WelcomeMessage string
|
||||||
|
WelcomeDM string
|
||||||
|
WelcomeGIFURL string
|
||||||
|
HasConfiguration bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepo(db *sql.DB) *Repo {
|
||||||
|
return &Repo{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) GetSettings(ctx context.Context, guildID int64) (*Settings, error) {
|
||||||
|
const q = `
|
||||||
|
SELECT welcome_channel_id, welcome_message, welcome_dm, welcome_gif_url
|
||||||
|
FROM welcome
|
||||||
|
WHERE guild_id = $1
|
||||||
|
`
|
||||||
|
row := r.db.QueryRowContext(ctx, q, guildID)
|
||||||
|
var ch sql.NullInt64
|
||||||
|
var msg, dm, gif sql.NullString
|
||||||
|
|
||||||
|
switch err := row.Scan(&ch, &msg, &dm, &gif); err {
|
||||||
|
case nil:
|
||||||
|
s := &Settings{
|
||||||
|
GuildID: guildID,
|
||||||
|
ChannelID: 0,
|
||||||
|
WelcomeMessage: "",
|
||||||
|
WelcomeDM: "",
|
||||||
|
WelcomeGIFURL: "",
|
||||||
|
HasConfiguration: true,
|
||||||
|
}
|
||||||
|
if ch.Valid {
|
||||||
|
s.ChannelID = ch.Int64
|
||||||
|
}
|
||||||
|
if msg.Valid {
|
||||||
|
s.WelcomeMessage = msg.String
|
||||||
|
}
|
||||||
|
if dm.Valid {
|
||||||
|
s.WelcomeDM = dm.String
|
||||||
|
}
|
||||||
|
if gif.Valid {
|
||||||
|
s.WelcomeGIFURL = gif.String
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
case sql.ErrNoRows:
|
||||||
|
return &Settings{
|
||||||
|
GuildID: guildID,
|
||||||
|
HasConfiguration: false,
|
||||||
|
}, nil
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) UpsertChannel(ctx context.Context, guildID, channelID int64) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO welcome (guild_id, welcome_channel_id)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
ON CONFLICT (guild_id)
|
||||||
|
DO UPDATE SET welcome_channel_id = EXCLUDED.welcome_channel_id
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, channelID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) UpsertMessage(ctx context.Context, guildID int64, message string) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO welcome (guild_id, welcome_message)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
ON CONFLICT (guild_id)
|
||||||
|
DO UPDATE SET welcome_message = EXCLUDED.welcome_message
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, message)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) UpsertDM(ctx context.Context, guildID int64, dm string) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO welcome (guild_id, welcome_dm)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
ON CONFLICT (guild_id)
|
||||||
|
DO UPDATE SET welcome_dm = EXCLUDED.welcome_dm
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, dm)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repo) UpsertGIF(ctx context.Context, guildID int64, gifURL string) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO welcome (guild_id, welcome_gif_url)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
ON CONFLICT (guild_id)
|
||||||
|
DO UPDATE SET welcome_gif_url = EXCLUDED.welcome_gif_url
|
||||||
|
`
|
||||||
|
_, err := r.db.ExecContext(ctx, q, guildID, gifURL)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package defaultrole
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/defaultrolerepo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
repo *defaultrolerepo.Repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(repo *defaultrolerepo.Repo) *Service {
|
||||||
|
return &Service{repo: repo}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetRole(ctx context.Context, guildID int64) (int64, error) {
|
||||||
|
return s.repo.GetRole(ctx, guildID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetRole(ctx context.Context, guildID, roleID int64) error {
|
||||||
|
return s.repo.SetRole(ctx, guildID, roleID)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package logsettings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/settingsrepo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
settings *settingsrepo.Repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(settings *settingsrepo.Repo) *Service {
|
||||||
|
return &Service{settings: settings}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetChannel(ctx context.Context, guildID, channelID int64) error {
|
||||||
|
return s.settings.SetLogChannel(ctx, guildID, channelID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetEnabled(ctx context.Context, guildID int64, enabled bool) error {
|
||||||
|
return s.settings.SetLogEnabled(ctx, guildID, enabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetSettings(ctx context.Context, guildID int64) (*settingsrepo.LogSettings, error) {
|
||||||
|
return s.settings.GetLogSettings(ctx, guildID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) HasSettings(ctx context.Context, guildID int64) (bool, error) {
|
||||||
|
return s.settings.HasLogSettings(ctx, guildID)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package meeting
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/settingsrepo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
settings *settingsrepo.Repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(settings *settingsrepo.Repo) *Service {
|
||||||
|
return &Service{settings: settings}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetLobbyChannel(ctx context.Context, guildID int64, lobbyChannelID int64) error {
|
||||||
|
return s.settings.SetMeetingLobbyChannel(ctx, guildID, lobbyChannelID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetLobbyChannel(ctx context.Context, guildID int64) (channelID int64, ok bool, err error) {
|
||||||
|
return s.settings.GetMeetingLobbyChannel(ctx, guildID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) AddTempChannel(ctx context.Context, guildID, channelID, createdBy int64) error {
|
||||||
|
return s.settings.AddMeetingTempChannel(ctx, guildID, channelID, createdBy)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) RemoveTempChannel(ctx context.Context, guildID, channelID int64) error {
|
||||||
|
return s.settings.RemoveMeetingTempChannel(ctx, guildID, channelID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) IsTempChannel(ctx context.Context, guildID, channelID int64) (bool, error) {
|
||||||
|
return s.settings.IsMeetingTempChannel(ctx, guildID, channelID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetTempChannelOwner(ctx context.Context, guildID, channelID int64) (createdBy int64, ok bool, err error) {
|
||||||
|
return s.settings.GetMeetingTempChannelOwner(ctx, guildID, channelID)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package musicsettings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"velox-bot/internal/db/repos/settingsrepo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
settings *settingsrepo.Repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(settings *settingsrepo.Repo) *Service {
|
||||||
|
return &Service{settings: settings}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ToggleMusic(ctx context.Context, guildID int64) (bool, error) {
|
||||||
|
enabled, err := s.settings.IsMusicEnabled(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
newEnabled := !enabled
|
||||||
|
if err := s.settings.SetMusicEnabled(ctx, guildID, newEnabled); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return newEnabled, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) IsMusicEnabled(ctx context.Context, guildID int64) (bool, error) {
|
||||||
|
return s.settings.IsMusicEnabled(ctx, guildID)
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package projects
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/projectsrepo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
repo *projectsrepo.Repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(repo *projectsrepo.Repo) *Service {
|
||||||
|
return &Service{repo: repo}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) CreateProject(ctx context.Context, guildID, creatorID int64, name, description string) (int64, error) {
|
||||||
|
return s.repo.CreateProject(ctx, guildID, creatorID, name, description)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) AddHelper(ctx context.Context, projectID, userID int64) error {
|
||||||
|
return s.repo.AddHelper(ctx, projectID, userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ListActiveWithMembers(ctx context.Context, guildID int64, limit int) ([]*projectsrepo.ProjectWithMembers, error) {
|
||||||
|
return s.repo.ListActiveProjectsWithMembers(ctx, guildID, limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ListActive(ctx context.Context, guildID int64, limit int) ([]*projectsrepo.Project, error) {
|
||||||
|
return s.repo.ListActiveProjects(ctx, guildID, limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ProjectExistsInGuild(ctx context.Context, guildID, projectID int64) (bool, error) {
|
||||||
|
return s.repo.ProjectExistsInGuild(ctx, guildID, projectID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetProject(ctx context.Context, guildID, projectID int64) (*projectsrepo.Project, error) {
|
||||||
|
return s.repo.GetProject(ctx, guildID, projectID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) DeactivateProject(ctx context.Context, guildID, projectID int64) error {
|
||||||
|
return s.repo.DeactivateProject(ctx, guildID, projectID)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
package rps
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"errors"
|
||||||
|
"math/big"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/rpsrepo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repo interface {
|
||||||
|
GetScore(ctx context.Context, guildID int64, userID int64) (int, bool, error)
|
||||||
|
SetScore(ctx context.Context, guildID int64, userID int64, score int) error
|
||||||
|
TopScores(ctx context.Context, guildID int64, limit int) ([]rpsrepo.ScoreEntry, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
repo Repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(repo Repo) *Service {
|
||||||
|
return &Service{repo: repo}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Hand string
|
||||||
|
|
||||||
|
const (
|
||||||
|
HandScissors Hand = "✌️"
|
||||||
|
HandPaper Hand = "✋"
|
||||||
|
HandRock Hand = "🤜"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h Hand) String() string { return string(h) }
|
||||||
|
|
||||||
|
func ParseHand(s string) (Hand, bool) {
|
||||||
|
switch strings.TrimSpace(s) {
|
||||||
|
case string(HandScissors):
|
||||||
|
return HandScissors, true
|
||||||
|
case string(HandPaper):
|
||||||
|
return HandPaper, true
|
||||||
|
case string(HandRock):
|
||||||
|
return HandRock, true
|
||||||
|
default:
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func RandomHand() (Hand, error) {
|
||||||
|
n, err := rand.Int(rand.Reader, big.NewInt(3))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
switch n.Int64() {
|
||||||
|
case 0:
|
||||||
|
return HandScissors, nil
|
||||||
|
case 1:
|
||||||
|
return HandPaper, nil
|
||||||
|
default:
|
||||||
|
return HandRock, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type GameResult string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ResultWin GameResult = "You won!"
|
||||||
|
ResultLose GameResult = "You lost!"
|
||||||
|
ResultTie GameResult = "It's a tie!"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DetermineResult(user Hand, bot Hand) GameResult {
|
||||||
|
if user == bot {
|
||||||
|
return ResultTie
|
||||||
|
}
|
||||||
|
switch user {
|
||||||
|
case HandRock:
|
||||||
|
if bot == HandScissors {
|
||||||
|
return ResultWin
|
||||||
|
}
|
||||||
|
case HandPaper:
|
||||||
|
if bot == HandRock {
|
||||||
|
return ResultWin
|
||||||
|
}
|
||||||
|
case HandScissors:
|
||||||
|
if bot == HandPaper {
|
||||||
|
return ResultWin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ResultLose
|
||||||
|
}
|
||||||
|
|
||||||
|
type PlayOutput struct {
|
||||||
|
UserHand Hand
|
||||||
|
BotHand Hand
|
||||||
|
Result GameResult
|
||||||
|
Score int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) Play(ctx context.Context, guildID int64, userID int64, userHand Hand) (PlayOutput, error) {
|
||||||
|
botHand, err := RandomHand()
|
||||||
|
if err != nil {
|
||||||
|
return PlayOutput{}, err
|
||||||
|
}
|
||||||
|
result := DetermineResult(userHand, botHand)
|
||||||
|
|
||||||
|
score, _, err := s.repo.GetScore(ctx, guildID, userID)
|
||||||
|
if err != nil {
|
||||||
|
return PlayOutput{}, err
|
||||||
|
}
|
||||||
|
if result == ResultWin {
|
||||||
|
score++
|
||||||
|
if err := s.repo.SetScore(ctx, guildID, userID, score); err != nil {
|
||||||
|
return PlayOutput{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return PlayOutput{
|
||||||
|
UserHand: userHand,
|
||||||
|
BotHand: botHand,
|
||||||
|
Result: result,
|
||||||
|
Score: score,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetScore(ctx context.Context, guildID int64, userID int64) (int, bool, error) {
|
||||||
|
return s.repo.GetScore(ctx, guildID, userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) TopScores(ctx context.Context, guildID int64, limit int) ([]rpsrepo.ScoreEntry, error) {
|
||||||
|
return s.repo.TopScores(ctx, guildID, limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrGuildOnly = errors.New("guild only")
|
||||||
|
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package schedule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/schedulerepo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
repo *schedulerepo.Repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(repo *schedulerepo.Repo) *Service {
|
||||||
|
return &Service{repo: repo}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) CreateSchedule(ctx context.Context, guildID, requesterID, inviteeID int64, when time.Time, description string) (int64, error) {
|
||||||
|
return s.repo.CreateSchedule(ctx, guildID, requesterID, inviteeID, when, description)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) UpdateStatus(ctx context.Context, scheduleID int64, status schedulerepo.ScheduleStatus) error {
|
||||||
|
return s.repo.UpdateStatus(ctx, scheduleID, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ListForUser(ctx context.Context, guildID, userID int64, limit int) ([]*schedulerepo.Schedule, error) {
|
||||||
|
return s.repo.ListForUser(ctx, guildID, userID, limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ListDueForReminder(ctx context.Context, now time.Time, ahead time.Duration) ([]*schedulerepo.Schedule, error) {
|
||||||
|
return s.repo.ListDueForReminder(ctx, now, ahead)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) MarkReminded(ctx context.Context, scheduleID int64) error {
|
||||||
|
return s.repo.MarkReminded(ctx, scheduleID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetByID(ctx context.Context, id int64) (*schedulerepo.Schedule, error) {
|
||||||
|
return s.repo.GetByID(ctx, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) Reschedule(ctx context.Context, id int64, when time.Time) error {
|
||||||
|
return s.repo.Reschedule(ctx, id, when)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ProposeReschedule(ctx context.Context, scheduleID int64, newTime time.Time, proposedBy int64) error {
|
||||||
|
return s.repo.ProposeReschedule(ctx, scheduleID, newTime, proposedBy)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) AcceptReschedule(ctx context.Context, id int64) error {
|
||||||
|
return s.repo.AcceptReschedule(ctx, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) DeclineReschedule(ctx context.Context, id int64) error {
|
||||||
|
return s.repo.DeclineReschedule(ctx, id)
|
||||||
|
}
|
||||||
@@ -1,21 +1,51 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"velox-bot/internal/db/services/defaultrole"
|
||||||
"velox-bot/internal/db/services/level"
|
"velox-bot/internal/db/services/level"
|
||||||
"velox-bot/internal/db/services/levelsettings"
|
"velox-bot/internal/db/services/levelsettings"
|
||||||
|
"velox-bot/internal/db/services/logsettings"
|
||||||
|
"velox-bot/internal/db/services/meeting"
|
||||||
|
"velox-bot/internal/db/services/musicsettings"
|
||||||
|
"velox-bot/internal/db/services/projects"
|
||||||
|
"velox-bot/internal/db/services/rps"
|
||||||
|
"velox-bot/internal/db/services/schedule"
|
||||||
|
"velox-bot/internal/db/services/twitch"
|
||||||
|
"velox-bot/internal/db/services/usersettings"
|
||||||
|
"velox-bot/internal/db/services/welcome"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Services struct {
|
type Services struct {
|
||||||
Level *level.Service
|
Level *level.Service
|
||||||
LevelSettings *levelsettings.Service
|
LevelSettings *levelsettings.Service
|
||||||
|
Meeting *meeting.Service
|
||||||
|
Schedule *schedule.Service
|
||||||
|
UserSettings *usersettings.Service
|
||||||
|
Projects *projects.Service
|
||||||
|
RPS *rps.Service
|
||||||
|
Twitch *twitch.Service
|
||||||
|
Welcome *welcome.Service
|
||||||
|
DefaultRole *defaultrole.Service
|
||||||
|
LogSettings *logsettings.Service
|
||||||
|
MusicSettings *musicsettings.Service
|
||||||
}
|
}
|
||||||
|
|
||||||
var Global *Services
|
var Global *Services
|
||||||
|
|
||||||
func NewServices(level *level.Service, levelSettings *levelsettings.Service) *Services {
|
func NewServices(level *level.Service, levelSettings *levelsettings.Service, meeting *meeting.Service, schedule *schedule.Service, userSettings *usersettings.Service, projects *projects.Service, rps *rps.Service, twitchSvc *twitch.Service, welcomeSvc *welcome.Service, defaultRoleSvc *defaultrole.Service, logSettingsSvc *logsettings.Service, musicSvc *musicsettings.Service) *Services {
|
||||||
s := &Services{
|
s := &Services{
|
||||||
Level: level,
|
Level: level,
|
||||||
LevelSettings: levelSettings,
|
LevelSettings: levelSettings,
|
||||||
|
Meeting: meeting,
|
||||||
|
Schedule: schedule,
|
||||||
|
UserSettings: userSettings,
|
||||||
|
Projects: projects,
|
||||||
|
RPS: rps,
|
||||||
|
Twitch: twitchSvc,
|
||||||
|
Welcome: welcomeSvc,
|
||||||
|
DefaultRole: defaultRoleSvc,
|
||||||
|
LogSettings: logSettingsSvc,
|
||||||
|
MusicSettings: musicSvc,
|
||||||
}
|
}
|
||||||
Global = s
|
Global = s
|
||||||
return s
|
return s
|
||||||
|
|||||||
@@ -0,0 +1,117 @@
|
|||||||
|
package twitch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/twitchrepo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
repo *twitchrepo.Repo
|
||||||
|
client *http.Client
|
||||||
|
twitchClient string
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(repo *twitchrepo.Repo, twitchClientID string) *Service {
|
||||||
|
return &Service{
|
||||||
|
repo: repo,
|
||||||
|
client: &http.Client{
|
||||||
|
Timeout: 5 * time.Second,
|
||||||
|
},
|
||||||
|
twitchClient: twitchClientID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ListGuilds(ctx context.Context) ([]int64, error) {
|
||||||
|
return s.repo.ListGuilds(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) ListUsersForGuild(ctx context.Context, guildID int64) ([]string, error) {
|
||||||
|
return s.repo.ListUsersForGuild(ctx, guildID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetStatus(ctx context.Context, guildID int64, username string) (string, bool, error) {
|
||||||
|
return s.repo.GetStatus(ctx, guildID, username)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) UpdateStatus(ctx context.Context, guildID int64, username, status string) error {
|
||||||
|
return s.repo.UpdateStatus(ctx, guildID, username, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) UpsertStreamer(ctx context.Context, guildID int64, username string) error {
|
||||||
|
return s.repo.UpsertStreamer(ctx, guildID, username)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) RemoveStreamer(ctx context.Context, guildID int64, username string) error {
|
||||||
|
return s.repo.RemoveStreamer(ctx, guildID, username)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetNotificationChannel(ctx context.Context, guildID int64) (int64, bool, error) {
|
||||||
|
return s.repo.GetNotificationChannel(ctx, guildID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetNotificationChannel(ctx context.Context, guildID, channelID int64) error {
|
||||||
|
return s.repo.SetNotificationChannel(ctx, guildID, channelID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) IsEnabled(ctx context.Context, guildID int64) (bool, error) {
|
||||||
|
return s.repo.IsEnabled(ctx, guildID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetEnabled(ctx context.Context, guildID int64, enabled bool) error {
|
||||||
|
return s.repo.SetEnabled(ctx, guildID, enabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) IsUserStreaming(ctx context.Context, username string) (bool, error) {
|
||||||
|
type gqlRequest struct {
|
||||||
|
Query string `json:"query"`
|
||||||
|
Variables map[string]any `json:"variables"`
|
||||||
|
}
|
||||||
|
type gqlResponse struct {
|
||||||
|
Data struct {
|
||||||
|
User *struct {
|
||||||
|
Stream *struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
} `json:"stream"`
|
||||||
|
} `json:"user"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
q := fmt.Sprintf(`query { user(login: "%s") { stream { id } } }`, username)
|
||||||
|
body, err := json.Marshal(gqlRequest{
|
||||||
|
Query: q,
|
||||||
|
Variables: map[string]any{},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://gql.twitch.tv/gql", bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Client-Id", s.twitchClient)
|
||||||
|
|
||||||
|
resp, err := s.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
var respBody gqlResponse
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if respBody.Data.User == nil {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return respBody.Data.User.Stream != nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package usersettings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/usersettingsrepo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
repo *usersettingsrepo.Repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(repo *usersettingsrepo.Repo) *Service {
|
||||||
|
return &Service{repo: repo}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetTimezone(ctx context.Context, userID int64, zone string) error {
|
||||||
|
// validate zone before storing
|
||||||
|
if _, err := time.LoadLocation(zone); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return s.repo.SetTimezone(ctx, userID, zone)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTimezone returns the user's location (or UTC) and the zone string and a flag indicating if it was user-defined.
|
||||||
|
func (s *Service) GetTimezone(ctx context.Context, userID int64) (*time.Location, string, bool, error) {
|
||||||
|
zone, ok, err := s.repo.GetTimezone(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
return time.UTC, "UTC", false, err
|
||||||
|
}
|
||||||
|
if !ok || zone == "" {
|
||||||
|
return time.UTC, "UTC", false, nil
|
||||||
|
}
|
||||||
|
loc, err := time.LoadLocation(zone)
|
||||||
|
if err != nil {
|
||||||
|
// fallback to UTC but indicate not user-defined
|
||||||
|
return time.UTC, "UTC", false, nil
|
||||||
|
}
|
||||||
|
return loc, zone, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package welcome
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/welcomerepo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
repo *welcomerepo.Repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(repo *welcomerepo.Repo) *Service {
|
||||||
|
return &Service{repo: repo}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetSettings(ctx context.Context, guildID int64) (*welcomerepo.Settings, error) {
|
||||||
|
return s.repo.GetSettings(ctx, guildID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetChannel(ctx context.Context, guildID, channelID int64) error {
|
||||||
|
return s.repo.UpsertChannel(ctx, guildID, channelID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetMessage(ctx context.Context, guildID int64, message string) error {
|
||||||
|
return s.repo.UpsertMessage(ctx, guildID, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetDM(ctx context.Context, guildID int64, dm string) error {
|
||||||
|
return s.repo.UpsertDM(ctx, guildID, dm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) SetGIF(ctx context.Context, guildID int64, gifURL string) error {
|
||||||
|
return s.repo.UpsertGIF(ctx, guildID, gifURL)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,744 @@
|
|||||||
|
package diceexpr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Limits struct {
|
||||||
|
MaxExprLen int
|
||||||
|
MaxRepeat int
|
||||||
|
MaxDice int
|
||||||
|
MaxSides int
|
||||||
|
MaxAstDepth int
|
||||||
|
MaxExplosionDepth int
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultLimits() Limits {
|
||||||
|
return Limits{
|
||||||
|
MaxExprLen: 200,
|
||||||
|
MaxRepeat: 25,
|
||||||
|
MaxDice: 300,
|
||||||
|
MaxSides: 1000000,
|
||||||
|
MaxAstDepth: 64,
|
||||||
|
MaxExplosionDepth: 100,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type EvalResult struct {
|
||||||
|
Total int
|
||||||
|
Trace *Trace
|
||||||
|
}
|
||||||
|
|
||||||
|
type Trace struct {
|
||||||
|
// Text is a normalized display of the (non-repeat) expression.
|
||||||
|
Text string
|
||||||
|
// Rolls includes all dice rolls (including dropped ones) in encounter order.
|
||||||
|
Rolls []RollGroup
|
||||||
|
}
|
||||||
|
|
||||||
|
type RollGroup struct {
|
||||||
|
Count int
|
||||||
|
Sides int
|
||||||
|
// Dice is the per-die roll chain; each die may have multiple faces due to explosions.
|
||||||
|
Dice []Die
|
||||||
|
// KeepDrop is the modifier used (kh/kl/dh/dl). Empty if none.
|
||||||
|
KeepDrop string
|
||||||
|
KeepDropN int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Die struct {
|
||||||
|
Faces []int
|
||||||
|
Kept bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type MultiResult struct {
|
||||||
|
Repeat int
|
||||||
|
Items []EvalResult
|
||||||
|
}
|
||||||
|
|
||||||
|
func EvalMany(input string, limits Limits) (MultiResult, error) {
|
||||||
|
input = strings.TrimSpace(input)
|
||||||
|
if input == "" {
|
||||||
|
return MultiResult{}, errors.New("empty")
|
||||||
|
}
|
||||||
|
if limits.MaxExprLen > 0 && len(input) > limits.MaxExprLen {
|
||||||
|
return MultiResult{}, errors.New("too long")
|
||||||
|
}
|
||||||
|
|
||||||
|
repeat, expr := splitRepeat(input)
|
||||||
|
if repeat < 1 {
|
||||||
|
repeat = 1
|
||||||
|
}
|
||||||
|
if limits.MaxRepeat > 0 && repeat > limits.MaxRepeat {
|
||||||
|
return MultiResult{}, fmt.Errorf("repeat too large")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse once, evaluate many times.
|
||||||
|
p := newParser(expr, limits)
|
||||||
|
ast, err := p.parse()
|
||||||
|
if err != nil {
|
||||||
|
return MultiResult{}, err
|
||||||
|
}
|
||||||
|
norm := formatExpr(ast)
|
||||||
|
|
||||||
|
out := MultiResult{Repeat: repeat, Items: make([]EvalResult, 0, repeat)}
|
||||||
|
for range repeat {
|
||||||
|
e := evaluator{limits: limits}
|
||||||
|
total, trace, err := e.eval(ast)
|
||||||
|
if err != nil {
|
||||||
|
return MultiResult{}, err
|
||||||
|
}
|
||||||
|
trace.Text = norm
|
||||||
|
out.Items = append(out.Items, EvalResult{Total: total, Trace: trace})
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitRepeat(s string) (int, string) {
|
||||||
|
// Accept "R#expr" where R is positive integer.
|
||||||
|
// We only treat the first '#' as repeat separator if it occurs before any dice operator.
|
||||||
|
// This keeps "d#" or other oddities from being interpreted as repeat.
|
||||||
|
i := strings.IndexByte(s, '#')
|
||||||
|
if i <= 0 {
|
||||||
|
return 1, s
|
||||||
|
}
|
||||||
|
prefix := strings.TrimSpace(s[:i])
|
||||||
|
if prefix == "" {
|
||||||
|
return 1, s
|
||||||
|
}
|
||||||
|
for _, r := range prefix {
|
||||||
|
if !unicode.IsDigit(r) {
|
||||||
|
return 1, s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
n, err := strconv.Atoi(prefix)
|
||||||
|
if err != nil || n <= 0 {
|
||||||
|
return 1, s
|
||||||
|
}
|
||||||
|
return n, strings.TrimSpace(s[i+1:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Formatting ----
|
||||||
|
|
||||||
|
type FormatOptions struct {
|
||||||
|
BoldMinMax bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultFormatOptions() FormatOptions {
|
||||||
|
return FormatOptions{BoldMinMax: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
func FormatResult(r MultiResult, opts FormatOptions) string {
|
||||||
|
lines := make([]string, 0, len(r.Items))
|
||||||
|
for _, item := range r.Items {
|
||||||
|
lines = append(lines, formatOne(item, opts))
|
||||||
|
}
|
||||||
|
return strings.Join(lines, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatOne(item EvalResult, opts FormatOptions) string {
|
||||||
|
// If there were multiple roll groups, we still show them in sequence, e.g. "[...][...] expr".
|
||||||
|
parts := make([]string, 0, len(item.Trace.Rolls))
|
||||||
|
for _, g := range item.Trace.Rolls {
|
||||||
|
parts = append(parts, formatRollGroup(g, opts))
|
||||||
|
}
|
||||||
|
breakdown := strings.Join(parts, " ")
|
||||||
|
if breakdown == "" {
|
||||||
|
breakdown = "[]"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%d \u2190 %s %s", item.Total, breakdown, item.Trace.Text)
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatRollGroup(g RollGroup, opts FormatOptions) string {
|
||||||
|
items := make([]string, 0, len(g.Dice))
|
||||||
|
for _, d := range g.Dice {
|
||||||
|
items = append(items, formatDie(d, g.Sides, opts))
|
||||||
|
}
|
||||||
|
return "[" + strings.Join(items, ", ") + "]"
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatDie(d Die, sides int, opts FormatOptions) string {
|
||||||
|
// Chain faces with '+' if exploded.
|
||||||
|
faceParts := make([]string, 0, len(d.Faces))
|
||||||
|
for _, v := range d.Faces {
|
||||||
|
faceParts = append(faceParts, formatFace(v, sides, opts))
|
||||||
|
}
|
||||||
|
txt := strings.Join(faceParts, "+")
|
||||||
|
if len(d.Faces) > 1 {
|
||||||
|
// denote explosion occurred (kept compact)
|
||||||
|
txt = txt + "!"
|
||||||
|
}
|
||||||
|
if !d.Kept {
|
||||||
|
return "~~" + txt + "~~"
|
||||||
|
}
|
||||||
|
return txt
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatFace(v, sides int, opts FormatOptions) string {
|
||||||
|
if !opts.BoldMinMax {
|
||||||
|
return strconv.Itoa(v)
|
||||||
|
}
|
||||||
|
if v == 1 || v == sides {
|
||||||
|
return "**" + strconv.Itoa(v) + "**"
|
||||||
|
}
|
||||||
|
return strconv.Itoa(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- AST / Parser / Evaluator ----
|
||||||
|
|
||||||
|
type nodeKind int
|
||||||
|
|
||||||
|
const (
|
||||||
|
kindNumber nodeKind = iota
|
||||||
|
kindUnary
|
||||||
|
kindBinary
|
||||||
|
kindDice
|
||||||
|
)
|
||||||
|
|
||||||
|
type node struct {
|
||||||
|
kind nodeKind
|
||||||
|
|
||||||
|
// number
|
||||||
|
num int
|
||||||
|
|
||||||
|
// unary
|
||||||
|
unOp byte
|
||||||
|
unA *node
|
||||||
|
|
||||||
|
// binary
|
||||||
|
binOp byte
|
||||||
|
binL *node
|
||||||
|
binR *node
|
||||||
|
|
||||||
|
// dice
|
||||||
|
diceCount *node
|
||||||
|
diceSides *node
|
||||||
|
explode bool
|
||||||
|
keepDrop string // kh/kl/dh/dl
|
||||||
|
keepN *node
|
||||||
|
}
|
||||||
|
|
||||||
|
type parser struct {
|
||||||
|
s string
|
||||||
|
i int
|
||||||
|
limits Limits
|
||||||
|
depth int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newParser(s string, limits Limits) *parser {
|
||||||
|
return &parser{s: strings.TrimSpace(s), limits: limits}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) parse() (*node, error) {
|
||||||
|
p.skipSpaces()
|
||||||
|
n, err := p.parseExpr()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
p.skipSpaces()
|
||||||
|
if p.i != len(p.s) {
|
||||||
|
return nil, errors.New("trailing input")
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) parseExpr() (*node, error) {
|
||||||
|
// expr = term {( + | - ) term}
|
||||||
|
left, err := p.parseTerm()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
p.skipSpaces()
|
||||||
|
if p.peek() != '+' && p.peek() != '-' {
|
||||||
|
return left, nil
|
||||||
|
}
|
||||||
|
op := p.next()
|
||||||
|
right, err := p.parseTerm()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
left = &node{kind: kindBinary, binOp: op, binL: left, binR: right}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) parseTerm() (*node, error) {
|
||||||
|
// term = factor {( * | / ) factor}
|
||||||
|
left, err := p.parseFactor()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
p.skipSpaces()
|
||||||
|
if p.peek() != '*' && p.peek() != '/' {
|
||||||
|
return left, nil
|
||||||
|
}
|
||||||
|
op := p.next()
|
||||||
|
right, err := p.parseFactor()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
left = &node{kind: kindBinary, binOp: op, binL: left, binR: right}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) parseFactor() (*node, error) {
|
||||||
|
// factor = unary
|
||||||
|
return p.parseUnary()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) parseUnary() (*node, error) {
|
||||||
|
p.skipSpaces()
|
||||||
|
if p.peek() == '+' || p.peek() == '-' {
|
||||||
|
op := p.next()
|
||||||
|
a, err := p.parseUnary()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &node{kind: kindUnary, unOp: op, unA: a}, nil
|
||||||
|
}
|
||||||
|
return p.parsePrimaryOrDice()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) parsePrimaryOrDice() (*node, error) {
|
||||||
|
p.skipSpaces()
|
||||||
|
if p.peek() == '(' {
|
||||||
|
p.next()
|
||||||
|
p.depth++
|
||||||
|
if p.limits.MaxAstDepth > 0 && p.depth > p.limits.MaxAstDepth {
|
||||||
|
return nil, errors.New("expression too deep")
|
||||||
|
}
|
||||||
|
n, err := p.parseExpr()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
p.skipSpaces()
|
||||||
|
if p.peek() != ')' {
|
||||||
|
return nil, errors.New("missing )")
|
||||||
|
}
|
||||||
|
p.next()
|
||||||
|
p.depth--
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try parse leading number or dice with omitted count.
|
||||||
|
start := p.i
|
||||||
|
num, hasNum, err := p.tryParseInt()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
p.skipSpaces()
|
||||||
|
if p.peekLower() == 'd' {
|
||||||
|
// dice: [count] d sides [keep/drop] [explode]
|
||||||
|
var countNode *node
|
||||||
|
if hasNum {
|
||||||
|
countNode = &node{kind: kindNumber, num: num}
|
||||||
|
} else {
|
||||||
|
countNode = &node{kind: kindNumber, num: 1}
|
||||||
|
}
|
||||||
|
p.next() // d
|
||||||
|
sidesNode, err := p.parseSides()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
keepDrop, keepN, err := p.parseKeepDrop()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
explode := false
|
||||||
|
p.skipSpaces()
|
||||||
|
if p.peek() == '!' {
|
||||||
|
explode = true
|
||||||
|
p.next()
|
||||||
|
}
|
||||||
|
return &node{
|
||||||
|
kind: kindDice,
|
||||||
|
diceCount: countNode,
|
||||||
|
diceSides: sidesNode,
|
||||||
|
explode: explode,
|
||||||
|
keepDrop: keepDrop,
|
||||||
|
keepN: keepN,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not a dice; if we had a number, return number; else error.
|
||||||
|
if hasNum {
|
||||||
|
return &node{kind: kindNumber, num: num}, nil
|
||||||
|
}
|
||||||
|
p.i = start
|
||||||
|
return nil, errors.New("expected number, dice, or (")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) parseSides() (*node, error) {
|
||||||
|
p.skipSpaces()
|
||||||
|
if p.peek() == '%' {
|
||||||
|
p.next()
|
||||||
|
return &node{kind: kindNumber, num: 100}, nil
|
||||||
|
}
|
||||||
|
n, ok, err := p.tryParseInt()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("expected sides")
|
||||||
|
}
|
||||||
|
return &node{kind: kindNumber, num: n}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) parseKeepDrop() (string, *node, error) {
|
||||||
|
p.skipSpaces()
|
||||||
|
// khN klN dhN dlN
|
||||||
|
if p.peekLower() != 'k' && p.peekLower() != 'd' {
|
||||||
|
return "", nil, nil
|
||||||
|
}
|
||||||
|
c1 := p.peekLower()
|
||||||
|
if c1 != 'k' && c1 != 'd' {
|
||||||
|
return "", nil, nil
|
||||||
|
}
|
||||||
|
if p.i+1 >= len(p.s) {
|
||||||
|
return "", nil, nil
|
||||||
|
}
|
||||||
|
c2 := byte(unicode.ToLower(rune(p.s[p.i+1])))
|
||||||
|
if c2 != 'h' && c2 != 'l' {
|
||||||
|
return "", nil, nil
|
||||||
|
}
|
||||||
|
op := string([]byte{c1, c2})
|
||||||
|
p.i += 2
|
||||||
|
n, ok, err := p.tryParseInt()
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
if !ok || n <= 0 {
|
||||||
|
return "", nil, errors.New("expected keep/drop count")
|
||||||
|
}
|
||||||
|
return op, &node{kind: kindNumber, num: n}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) tryParseInt() (int, bool, error) {
|
||||||
|
p.skipSpaces()
|
||||||
|
if p.i >= len(p.s) || !unicode.IsDigit(rune(p.s[p.i])) {
|
||||||
|
return 0, false, nil
|
||||||
|
}
|
||||||
|
start := p.i
|
||||||
|
for p.i < len(p.s) && unicode.IsDigit(rune(p.s[p.i])) {
|
||||||
|
p.i++
|
||||||
|
}
|
||||||
|
v, err := strconv.Atoi(p.s[start:p.i])
|
||||||
|
if err != nil {
|
||||||
|
return 0, false, err
|
||||||
|
}
|
||||||
|
return v, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) skipSpaces() {
|
||||||
|
for p.i < len(p.s) && unicode.IsSpace(rune(p.s[p.i])) {
|
||||||
|
p.i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) peek() byte {
|
||||||
|
if p.i >= len(p.s) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return p.s[p.i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) peekLower() byte {
|
||||||
|
if p.i >= len(p.s) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return byte(unicode.ToLower(rune(p.s[p.i])))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parser) next() byte {
|
||||||
|
if p.i >= len(p.s) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
b := p.s[p.i]
|
||||||
|
p.i++
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeSpaces(s string) string {
|
||||||
|
// Keep compact: remove spaces entirely, but preserve leading repeat already stripped outside.
|
||||||
|
var b strings.Builder
|
||||||
|
b.Grow(len(s))
|
||||||
|
for _, r := range s {
|
||||||
|
if !unicode.IsSpace(r) {
|
||||||
|
b.WriteRune(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// formatExpr renders a canonical expression with explicit dice counts and
|
||||||
|
// spaces around binary operators.
|
||||||
|
func formatExpr(n *node) string {
|
||||||
|
return formatExprPrec(n, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatExprPrec(n *node, parentPrec int) string {
|
||||||
|
switch n.kind {
|
||||||
|
case kindNumber:
|
||||||
|
return strconv.Itoa(n.num)
|
||||||
|
case kindUnary:
|
||||||
|
inner := formatExprPrec(n.unA, 3)
|
||||||
|
if n.unOp == '+' {
|
||||||
|
return inner
|
||||||
|
}
|
||||||
|
return string(n.unOp) + inner
|
||||||
|
case kindDice:
|
||||||
|
// count and sides are currently numbers, but we keep it generic.
|
||||||
|
count := formatExprPrec(n.diceCount, 4)
|
||||||
|
sides := formatExprPrec(n.diceSides, 4)
|
||||||
|
out := count + "d" + sides
|
||||||
|
if n.keepDrop != "" && n.keepN != nil {
|
||||||
|
out += n.keepDrop + formatExprPrec(n.keepN, 4)
|
||||||
|
}
|
||||||
|
if n.explode {
|
||||||
|
out += "!"
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
case kindBinary:
|
||||||
|
prec := binPrec(n.binOp)
|
||||||
|
l := formatExprPrec(n.binL, prec)
|
||||||
|
r := formatExprPrec(n.binR, prec+1)
|
||||||
|
out := l + " " + string(n.binOp) + " " + r
|
||||||
|
if prec < parentPrec {
|
||||||
|
return "(" + out + ")"
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func binPrec(op byte) int {
|
||||||
|
switch op {
|
||||||
|
case '+', '-':
|
||||||
|
return 1
|
||||||
|
case '*', '/':
|
||||||
|
return 2
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type evaluator struct {
|
||||||
|
limits Limits
|
||||||
|
dice int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *evaluator) eval(n *node) (int, *Trace, error) {
|
||||||
|
t := &Trace{}
|
||||||
|
v, err := e.evalNode(n, t)
|
||||||
|
if err != nil {
|
||||||
|
return 0, nil, err
|
||||||
|
}
|
||||||
|
return v, t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *evaluator) evalNode(n *node, t *Trace) (int, error) {
|
||||||
|
switch n.kind {
|
||||||
|
case kindNumber:
|
||||||
|
return n.num, nil
|
||||||
|
case kindUnary:
|
||||||
|
v, err := e.evalNode(n.unA, t)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
switch n.unOp {
|
||||||
|
case '+':
|
||||||
|
return v, nil
|
||||||
|
case '-':
|
||||||
|
return -v, nil
|
||||||
|
default:
|
||||||
|
return 0, errors.New("bad unary op")
|
||||||
|
}
|
||||||
|
case kindBinary:
|
||||||
|
l, err := e.evalNode(n.binL, t)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
r, err := e.evalNode(n.binR, t)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
switch n.binOp {
|
||||||
|
case '+':
|
||||||
|
return l + r, nil
|
||||||
|
case '-':
|
||||||
|
return l - r, nil
|
||||||
|
case '*':
|
||||||
|
return l * r, nil
|
||||||
|
case '/':
|
||||||
|
if r == 0 {
|
||||||
|
return 0, errors.New("division by zero")
|
||||||
|
}
|
||||||
|
return l / r, nil
|
||||||
|
default:
|
||||||
|
return 0, errors.New("bad binary op")
|
||||||
|
}
|
||||||
|
case kindDice:
|
||||||
|
return e.evalDice(n, t)
|
||||||
|
default:
|
||||||
|
return 0, errors.New("unknown node")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *evaluator) evalDice(n *node, t *Trace) (int, error) {
|
||||||
|
count, err := e.evalNode(n.diceCount, t)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
sides, err := e.evalNode(n.diceSides, t)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if count <= 0 || sides <= 0 {
|
||||||
|
return 0, errors.New("invalid dice")
|
||||||
|
}
|
||||||
|
if e.limits.MaxDice > 0 && e.dice+count > e.limits.MaxDice {
|
||||||
|
return 0, errors.New("too many dice")
|
||||||
|
}
|
||||||
|
if e.limits.MaxSides > 0 && sides > e.limits.MaxSides {
|
||||||
|
return 0, errors.New("sides too large")
|
||||||
|
}
|
||||||
|
e.dice += count
|
||||||
|
|
||||||
|
keepDrop := n.keepDrop
|
||||||
|
keepN := 0
|
||||||
|
if n.keepN != nil {
|
||||||
|
keepN, err = e.evalNode(n.keepN, t)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if keepDrop != "" && keepN <= 0 {
|
||||||
|
return 0, errors.New("invalid keep/drop")
|
||||||
|
}
|
||||||
|
if keepDrop != "" && keepN > count {
|
||||||
|
keepN = count
|
||||||
|
}
|
||||||
|
|
||||||
|
g := RollGroup{
|
||||||
|
Count: count,
|
||||||
|
Sides: sides,
|
||||||
|
Dice: make([]Die, 0, count),
|
||||||
|
KeepDrop: keepDrop,
|
||||||
|
KeepDropN: keepN,
|
||||||
|
}
|
||||||
|
|
||||||
|
type dieScore struct {
|
||||||
|
idx int
|
||||||
|
score int
|
||||||
|
}
|
||||||
|
scores := make([]dieScore, 0, count)
|
||||||
|
|
||||||
|
// Roll base dice.
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
faces, err := rollExploding(sides, n.explode, e.limits.MaxExplosionDepth)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
sum := 0
|
||||||
|
for _, v := range faces {
|
||||||
|
sum += v
|
||||||
|
}
|
||||||
|
g.Dice = append(g.Dice, Die{Faces: faces, Kept: true})
|
||||||
|
scores = append(scores, dieScore{idx: i, score: sum})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply keep/drop to base dice by comparing chain sums.
|
||||||
|
if keepDrop != "" {
|
||||||
|
sort.SliceStable(scores, func(i, j int) bool { return scores[i].score < scores[j].score })
|
||||||
|
|
||||||
|
kept := make(map[int]bool, count)
|
||||||
|
switch keepDrop {
|
||||||
|
case "kh":
|
||||||
|
for i := len(scores) - keepN; i < len(scores); i++ {
|
||||||
|
if i >= 0 && i < len(scores) {
|
||||||
|
kept[scores[i].idx] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "kl":
|
||||||
|
for i := 0; i < keepN && i < len(scores); i++ {
|
||||||
|
kept[scores[i].idx] = true
|
||||||
|
}
|
||||||
|
case "dh":
|
||||||
|
// drop highest N => keep all except highest N
|
||||||
|
for i := 0; i < len(scores)-keepN; i++ {
|
||||||
|
if i >= 0 && i < len(scores) {
|
||||||
|
kept[scores[i].idx] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "dl":
|
||||||
|
// drop lowest N => keep highest count-N
|
||||||
|
for i := keepN; i < len(scores); i++ {
|
||||||
|
kept[scores[i].idx] = true
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return 0, errors.New("unknown keep/drop")
|
||||||
|
}
|
||||||
|
for i := range g.Dice {
|
||||||
|
g.Dice[i].Kept = kept[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sum kept dice.
|
||||||
|
total := 0
|
||||||
|
for _, d := range g.Dice {
|
||||||
|
if !d.Kept {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, v := range d.Faces {
|
||||||
|
total += v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Rolls = append(t.Rolls, g)
|
||||||
|
return total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func rollExploding(sides int, explode bool, maxDepth int) ([]int, error) {
|
||||||
|
v, err := roll1(sides)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out := []int{v}
|
||||||
|
if !explode {
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
depth := 0
|
||||||
|
for v == sides {
|
||||||
|
depth++
|
||||||
|
if maxDepth > 0 && depth > maxDepth {
|
||||||
|
return nil, errors.New("explosion depth limit")
|
||||||
|
}
|
||||||
|
v, err = roll1(sides)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, v)
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func roll1(sides int) (int, error) {
|
||||||
|
if sides <= 0 {
|
||||||
|
return 0, errors.New("bad sides")
|
||||||
|
}
|
||||||
|
n, err := rand.Int(rand.Reader, big.NewInt(int64(sides)))
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return int(n.Int64()) + 1, nil
|
||||||
|
}
|
||||||
|
|
||||||
@@ -11,6 +11,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HandleMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate, services *services.Services) {
|
func HandleMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate, services *services.Services) {
|
||||||
|
CacheMessageFromCreate(m)
|
||||||
|
|
||||||
if m.Author.Bot || m.GuildID == "" {
|
if m.Author.Bot || m.GuildID == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,376 @@
|
|||||||
|
package events
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
"velox-bot/internal/modlog"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type cachedMessage struct {
|
||||||
|
Content string
|
||||||
|
AuthorID string
|
||||||
|
}
|
||||||
|
|
||||||
|
var messageCache sync.Map // map[messageID]cachedMessage
|
||||||
|
|
||||||
|
func HandleMessageUpdateLog(s *discordgo.Session, m *discordgo.MessageUpdate, svc *services.Services) {
|
||||||
|
if s == nil || m == nil || m.Message == nil || m.GuildID == "" || svc == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if m.Author != nil && m.Author.Bot {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
before := ""
|
||||||
|
if m.BeforeUpdate != nil {
|
||||||
|
before = strings.TrimSpace(m.BeforeUpdate.Content)
|
||||||
|
}
|
||||||
|
if before == "" {
|
||||||
|
if prev, ok := getCachedMessage(m.ID); ok {
|
||||||
|
before = strings.TrimSpace(prev.Content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
after := strings.TrimSpace(m.Content)
|
||||||
|
if before == after {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jumpURL := fmt.Sprintf("https://discord.com/channels/%s/%s/%s", m.GuildID, m.ChannelID, m.ID)
|
||||||
|
authorMention := "Unknown"
|
||||||
|
if m.Author != nil {
|
||||||
|
authorMention = m.Author.Mention()
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Message Edited",
|
||||||
|
Color: 0xF1C40F,
|
||||||
|
Description: fmt.Sprintf("Author: %s\nChannel: <#%s>\n[Jump to message](%s)", authorMention, m.ChannelID, jumpURL),
|
||||||
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
|
{Name: "Before", Value: truncateOrPlaceholder(before), Inline: false},
|
||||||
|
{Name: "After", Value: truncateOrPlaceholder(after), Inline: false},
|
||||||
|
},
|
||||||
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
if err := modlog.Send(s, svc, m.GuildID, embed); err != nil {
|
||||||
|
log.Printf("log: failed to send message edit log: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheMessage(m.ID, after, authorIDOrEmpty(m.Author))
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleMessageDeleteLog(s *discordgo.Session, m *discordgo.MessageDelete, svc *services.Services) {
|
||||||
|
if s == nil || m == nil || m.Message == nil || m.GuildID == "" || svc == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
authorID := ""
|
||||||
|
authorLabel := "Unknown"
|
||||||
|
content := ""
|
||||||
|
if m.BeforeDelete != nil {
|
||||||
|
if m.BeforeDelete.Author != nil {
|
||||||
|
authorID = m.BeforeDelete.Author.ID
|
||||||
|
authorLabel = m.BeforeDelete.Author.Mention()
|
||||||
|
}
|
||||||
|
content = strings.TrimSpace(m.BeforeDelete.Content)
|
||||||
|
}
|
||||||
|
if content == "" {
|
||||||
|
if prev, ok := getCachedMessage(m.ID); ok {
|
||||||
|
content = strings.TrimSpace(prev.Content)
|
||||||
|
if authorID == "" && prev.AuthorID != "" {
|
||||||
|
authorID = prev.AuthorID
|
||||||
|
authorLabel = "<@" + prev.AuthorID + ">"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if authorID == "" && m.Author != nil {
|
||||||
|
authorID = m.Author.ID
|
||||||
|
authorLabel = m.Author.Mention()
|
||||||
|
}
|
||||||
|
if authorID == "" {
|
||||||
|
authorLabel = "Unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Message Deleted",
|
||||||
|
Color: 0xE74C3C,
|
||||||
|
Description: fmt.Sprintf("Author: %s\nChannel: <#%s>\nMessage ID: `%s`", authorLabel, m.ChannelID, m.ID),
|
||||||
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
|
{Name: "Content", Value: truncateOrPlaceholder(content), Inline: false},
|
||||||
|
},
|
||||||
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
if err := modlog.Send(s, svc, m.GuildID, embed); err != nil {
|
||||||
|
log.Printf("log: failed to send message delete log: %v", err)
|
||||||
|
}
|
||||||
|
deleteCachedMessage(m.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleGuildMemberAddLog(s *discordgo.Session, m *discordgo.GuildMemberAdd, svc *services.Services) {
|
||||||
|
if s == nil || m == nil || m.Member == nil || m.User == nil || m.GuildID == "" || svc == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Member Joined",
|
||||||
|
Color: 0x2ECC71,
|
||||||
|
Description: fmt.Sprintf("%s joined the server.\nUser ID: `%s`", m.User.Mention(), m.User.ID),
|
||||||
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
if err := modlog.Send(s, svc, m.GuildID, embed); err != nil {
|
||||||
|
log.Printf("log: failed to send member join log: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleGuildMemberRemoveLog(s *discordgo.Session, m *discordgo.GuildMemberRemove, svc *services.Services) {
|
||||||
|
if s == nil || m == nil || m.Member == nil || m.User == nil || m.GuildID == "" || svc == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to classify this leave as a kick via recent audit log.
|
||||||
|
kickEntry, kickMod := findRecentAuditEntryForTarget(s, m.GuildID, m.User.ID, discordgo.AuditLogActionMemberKick)
|
||||||
|
title := "Member Left"
|
||||||
|
color := 0x95A5A6
|
||||||
|
description := fmt.Sprintf("%s left the server.\nUser ID: `%s`", m.User.Mention(), m.User.ID)
|
||||||
|
if kickEntry != nil {
|
||||||
|
title = "Member Kicked"
|
||||||
|
color = 0xE67E22
|
||||||
|
description = fmt.Sprintf("%s was kicked.\nUser ID: `%s`", m.User.Mention(), m.User.ID)
|
||||||
|
if kickMod != nil {
|
||||||
|
description += "\nModerator: " + kickMod.Mention()
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(kickEntry.Reason) != "" {
|
||||||
|
description += "\nReason: " + kickEntry.Reason
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: title,
|
||||||
|
Color: color,
|
||||||
|
Description: description,
|
||||||
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
if err := modlog.Send(s, svc, m.GuildID, embed); err != nil {
|
||||||
|
log.Printf("log: failed to send member remove log: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleGuildMemberUpdateLog(s *discordgo.Session, m *discordgo.GuildMemberUpdate, svc *services.Services) {
|
||||||
|
if s == nil || m == nil || m.Member == nil || m.User == nil || m.GuildID == "" || svc == nil || m.BeforeUpdate == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
added := diffRoleIDs(m.BeforeUpdate.Roles, m.Roles)
|
||||||
|
removed := diffRoleIDs(m.Roles, m.BeforeUpdate.Roles)
|
||||||
|
if len(added) == 0 && len(removed) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := make([]*discordgo.MessageEmbedField, 0, 2)
|
||||||
|
if len(added) > 0 {
|
||||||
|
fields = append(fields, &discordgo.MessageEmbedField{
|
||||||
|
Name: "Roles Added",
|
||||||
|
Value: joinRoleMentions(added),
|
||||||
|
Inline: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if len(removed) > 0 {
|
||||||
|
fields = append(fields, &discordgo.MessageEmbedField{
|
||||||
|
Name: "Roles Removed",
|
||||||
|
Value: joinRoleMentions(removed),
|
||||||
|
Inline: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Member Roles Updated",
|
||||||
|
Color: 0x3498DB,
|
||||||
|
Description: fmt.Sprintf("Member: %s (`%s`)", m.User.Mention(), m.User.ID),
|
||||||
|
Fields: fields,
|
||||||
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
if err := modlog.Send(s, svc, m.GuildID, embed); err != nil {
|
||||||
|
log.Printf("log: failed to send member update log: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleGuildBanAddLog(s *discordgo.Session, m *discordgo.GuildBanAdd, svc *services.Services) {
|
||||||
|
if s == nil || m == nil || m.User == nil || m.GuildID == "" || svc == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
entry, moderator := findRecentAuditEntryForTarget(s, m.GuildID, m.User.ID, discordgo.AuditLogActionMemberBanAdd)
|
||||||
|
desc := fmt.Sprintf("%s was banned.\nUser ID: `%s`", m.User.Mention(), m.User.ID)
|
||||||
|
if moderator != nil {
|
||||||
|
desc += "\nModerator: " + moderator.Mention()
|
||||||
|
}
|
||||||
|
if entry != nil && strings.TrimSpace(entry.Reason) != "" {
|
||||||
|
desc += "\nReason: " + entry.Reason
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Member Banned",
|
||||||
|
Color: 0xC0392B,
|
||||||
|
Description: desc,
|
||||||
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
if err := modlog.Send(s, svc, m.GuildID, embed); err != nil {
|
||||||
|
log.Printf("log: failed to send ban add log: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleGuildBanRemoveLog(s *discordgo.Session, m *discordgo.GuildBanRemove, svc *services.Services) {
|
||||||
|
if s == nil || m == nil || m.User == nil || m.GuildID == "" || svc == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
entry, moderator := findRecentAuditEntryForTarget(s, m.GuildID, m.User.ID, discordgo.AuditLogActionMemberBanRemove)
|
||||||
|
desc := fmt.Sprintf("%s was unbanned.\nUser ID: `%s`", m.User.Mention(), m.User.ID)
|
||||||
|
if moderator != nil {
|
||||||
|
desc += "\nModerator: " + moderator.Mention()
|
||||||
|
}
|
||||||
|
if entry != nil && strings.TrimSpace(entry.Reason) != "" {
|
||||||
|
desc += "\nReason: " + entry.Reason
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Member Unbanned",
|
||||||
|
Color: 0x27AE60,
|
||||||
|
Description: desc,
|
||||||
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
if err := modlog.Send(s, svc, m.GuildID, embed); err != nil {
|
||||||
|
log.Printf("log: failed to send ban remove log: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func findRecentAuditEntryForTarget(s *discordgo.Session, guildID, targetID string, action discordgo.AuditLogAction) (*discordgo.AuditLogEntry, *discordgo.User) {
|
||||||
|
if s == nil || guildID == "" || targetID == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
logs, err := s.GuildAuditLog(guildID, "", "", int(action), 10)
|
||||||
|
if err != nil || logs == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var chosen *discordgo.AuditLogEntry
|
||||||
|
now := time.Now().UTC()
|
||||||
|
for _, e := range logs.AuditLogEntries {
|
||||||
|
if e == nil || e.TargetID != targetID || e.ActionType == nil || *e.ActionType != action {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ts, err := discordgo.SnowflakeTimestamp(e.ID)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Keep only very recent entries to reduce false positives.
|
||||||
|
if now.Sub(ts) > 2*time.Minute {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
chosen = e
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if chosen == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, u := range logs.Users {
|
||||||
|
if u != nil && u.ID == chosen.UserID {
|
||||||
|
return chosen, u
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return chosen, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func truncateOrPlaceholder(s string) string {
|
||||||
|
v := strings.TrimSpace(s)
|
||||||
|
if v == "" {
|
||||||
|
return "*empty*"
|
||||||
|
}
|
||||||
|
if len(v) <= 1000 {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return v[:997] + "..."
|
||||||
|
}
|
||||||
|
|
||||||
|
func diffRoleIDs(base, compare []string) []string {
|
||||||
|
out := make([]string, 0)
|
||||||
|
for _, id := range base {
|
||||||
|
if id == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !slices.Contains(compare, id) {
|
||||||
|
out = append(out, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func joinRoleMentions(roleIDs []string) string {
|
||||||
|
if len(roleIDs) == 0 {
|
||||||
|
return "*none*"
|
||||||
|
}
|
||||||
|
const max = 12
|
||||||
|
parts := make([]string, 0, len(roleIDs))
|
||||||
|
for idx, id := range roleIDs {
|
||||||
|
if idx >= max {
|
||||||
|
parts = append(parts, fmt.Sprintf("... and %d more", len(roleIDs)-max))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
parts = append(parts, "<@&"+id+">")
|
||||||
|
}
|
||||||
|
return strings.Join(parts, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func CacheMessageFromCreate(m *discordgo.MessageCreate) {
|
||||||
|
if m == nil || m.Message == nil || m.ID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cacheMessage(m.ID, strings.TrimSpace(m.Content), authorIDOrEmpty(m.Author))
|
||||||
|
}
|
||||||
|
|
||||||
|
func cacheMessage(messageID, content, authorID string) {
|
||||||
|
if messageID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
messageCache.Store(messageID, cachedMessage{
|
||||||
|
Content: content,
|
||||||
|
AuthorID: authorID,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCachedMessage(messageID string) (cachedMessage, bool) {
|
||||||
|
if messageID == "" {
|
||||||
|
return cachedMessage{}, false
|
||||||
|
}
|
||||||
|
raw, ok := messageCache.Load(messageID)
|
||||||
|
if !ok {
|
||||||
|
return cachedMessage{}, false
|
||||||
|
}
|
||||||
|
msg, ok := raw.(cachedMessage)
|
||||||
|
if !ok {
|
||||||
|
return cachedMessage{}, false
|
||||||
|
}
|
||||||
|
return msg, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteCachedMessage(messageID string) {
|
||||||
|
if messageID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
messageCache.Delete(messageID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func authorIDOrEmpty(u *discordgo.User) string {
|
||||||
|
if u == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return u.ID
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
package events
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleVoiceStateUpdate implements the "meeting lobby" behavior:
|
||||||
|
// - When a user joins the configured lobby voice channel, create a temporary voice channel
|
||||||
|
// with the same UserLimit and move the user into it.
|
||||||
|
// - When a temporary channel becomes empty, delete it.
|
||||||
|
func HandleVoiceStateUpdate(s *discordgo.Session, vs *discordgo.VoiceStateUpdate, svc *services.Services) {
|
||||||
|
if svc == nil || svc.Meeting == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if vs == nil || vs.VoiceState == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if vs.GuildID == "" || vs.UserID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
guildID64, _ := strconv.ParseInt(vs.GuildID, 10, 64)
|
||||||
|
|
||||||
|
// Clean up the channel the user left (if any)
|
||||||
|
if vs.BeforeUpdate != nil && vs.BeforeUpdate.ChannelID != "" {
|
||||||
|
tryCleanupTempChannel(s, ctx, svc, vs.GuildID, guildID64, vs.BeforeUpdate.ChannelID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If not joining a channel (disconnect), nothing else to do.
|
||||||
|
if vs.ChannelID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
lobbyID, ok, err := svc.Meeting.GetLobbyChannel(ctx, guildID64)
|
||||||
|
if err != nil || !ok || lobbyID == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if vs.ChannelID != strconv.FormatInt(lobbyID, 10) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
lobbyCh, err := s.Channel(vs.ChannelID)
|
||||||
|
if err != nil || lobbyCh == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the new channel in the same category as the lobby, with same user limit.
|
||||||
|
newName := fmt.Sprintf("%s • %s", lobbyCh.Name, time.Now().Format("15:04"))
|
||||||
|
newCh, err := s.GuildChannelCreateComplex(vs.GuildID, discordgo.GuildChannelCreateData{
|
||||||
|
Name: newName,
|
||||||
|
Type: discordgo.ChannelTypeGuildVoice,
|
||||||
|
ParentID: lobbyCh.ParentID,
|
||||||
|
UserLimit: lobbyCh.UserLimit,
|
||||||
|
Bitrate: lobbyCh.Bitrate,
|
||||||
|
})
|
||||||
|
if err != nil || newCh == nil {
|
||||||
|
log.Printf("meeting: failed creating temp channel guild=%s user=%s err=%v", vs.GuildID, vs.UserID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
newChID64, _ := strconv.ParseInt(newCh.ID, 10, 64)
|
||||||
|
_ = svc.Meeting.AddTempChannel(ctx, guildID64, newChID64, mustParseInt64(vs.UserID))
|
||||||
|
|
||||||
|
// Move the user into the new channel.
|
||||||
|
targetID := newCh.ID
|
||||||
|
if err := s.GuildMemberMove(vs.GuildID, vs.UserID, &targetID); err != nil {
|
||||||
|
log.Printf("meeting: failed moving member guild=%s user=%s channel=%s err=%v", vs.GuildID, vs.UserID, newCh.ID, err)
|
||||||
|
// If move fails, delete the channel to avoid junk.
|
||||||
|
_, _ = s.ChannelDelete(newCh.ID)
|
||||||
|
_ = svc.Meeting.RemoveTempChannel(ctx, guildID64, newChID64)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tryCleanupTempChannel(s *discordgo.Session, ctx context.Context, svc *services.Services, guildID string, guildID64 int64, channelID string) {
|
||||||
|
chID64, err := strconv.ParseInt(channelID, 10, 64)
|
||||||
|
if err != nil || chID64 == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
isTemp, err := svc.Meeting.IsTempChannel(ctx, guildID64, chID64)
|
||||||
|
if err != nil || !isTemp {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if anyone is still in the channel.
|
||||||
|
// IMPORTANT: don't use REST `s.Guild(...)` here; it doesn't include VoiceStates.
|
||||||
|
// Use the session state cache (requires IntentsGuildVoiceStates).
|
||||||
|
if s == nil || s.State == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
g, err := s.State.Guild(guildID)
|
||||||
|
if err != nil || g == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, st := range g.VoiceStates {
|
||||||
|
if st != nil && st.ChannelID == channelID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := s.ChannelDelete(channelID); err != nil {
|
||||||
|
// If already deleted, still remove from DB.
|
||||||
|
log.Printf("meeting: failed deleting empty temp channel guild=%s channel=%s err=%v", guildID, channelID, err)
|
||||||
|
}
|
||||||
|
_ = svc.Meeting.RemoveTempChannel(ctx, guildID64, chID64)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,163 @@
|
|||||||
|
package events
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleBotAddedOnGuild applies safe defaults when this bot is added to a guild
|
||||||
|
// and DMs setup instructions to the inviter (or guild owner fallback).
|
||||||
|
func HandleBotAddedOnGuild(s *discordgo.Session, m *discordgo.GuildMemberAdd, svc *services.Services) {
|
||||||
|
if s == nil || m == nil || m.Member == nil || m.User == nil || svc == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.State == nil || s.State.User == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if m.User.ID != s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID64, err := strconv.ParseInt(m.GuildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Treat an existing logsettings row as "already onboarded once".
|
||||||
|
alreadyOnboarded := false
|
||||||
|
if svc.LogSettings != nil {
|
||||||
|
exists, err := svc.LogSettings.HasSettings(context.Background(), guildID64)
|
||||||
|
if err == nil {
|
||||||
|
alreadyOnboarded = exists
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if alreadyOnboarded {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explicitly disable toggleable systems by default.
|
||||||
|
if svc.LevelSettings != nil {
|
||||||
|
if err := svc.LevelSettings.SetLevelSystemEnabled(context.Background(), guildID64, false); err != nil {
|
||||||
|
log.Printf("onboarding: failed to set default level state for guild %s: %v", m.GuildID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if svc.LogSettings != nil {
|
||||||
|
if err := svc.LogSettings.SetEnabled(context.Background(), guildID64, false); err != nil {
|
||||||
|
log.Printf("onboarding: failed to set default log state for guild %s: %v", m.GuildID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
recipientID := findRecentBotAdderID(s, m.GuildID)
|
||||||
|
if recipientID == "" {
|
||||||
|
recipientID = guildOwnerID(s, m.GuildID)
|
||||||
|
}
|
||||||
|
if recipientID == "" {
|
||||||
|
log.Printf("onboarding: unable to resolve DM recipient for guild %s", m.GuildID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dmCh, err := s.UserChannelCreate(recipientID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("onboarding: failed creating DM for user %s: %v", recipientID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildName := m.GuildID
|
||||||
|
if g, err := s.State.Guild(m.GuildID); err == nil && g != nil && strings.TrimSpace(g.Name) != "" {
|
||||||
|
guildName = g.Name
|
||||||
|
} else if g, err := s.Guild(m.GuildID); err == nil && g != nil && strings.TrimSpace(g.Name) != "" {
|
||||||
|
guildName = g.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Thanks for adding Velox Bot!",
|
||||||
|
Color: 0x5865F2,
|
||||||
|
Description: fmt.Sprintf(
|
||||||
|
"Server: **%s**\n\nFor safety, toggleable systems start **disabled by default**.\nUse these commands in your server to enable features:",
|
||||||
|
guildName,
|
||||||
|
),
|
||||||
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
|
{
|
||||||
|
Name: "Level System",
|
||||||
|
Value: "`/slvl toggle`",
|
||||||
|
Inline: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Logging System",
|
||||||
|
Value: "" +
|
||||||
|
"`/config setlogchannel channel:#channel`\n" +
|
||||||
|
"`/config enablelogging`",
|
||||||
|
Inline: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Welcome System",
|
||||||
|
Value: "" +
|
||||||
|
"`/config setwelcomechannel channel:#channel`\n" +
|
||||||
|
"`/config setwelcomemessage message:\"...\"`\n" +
|
||||||
|
"`/config setwelcomedm message:\"...\"`",
|
||||||
|
Inline: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Footer: &discordgo.MessageEmbedFooter{
|
||||||
|
Text: "Tip: run /help in any server channel for the full command reference.",
|
||||||
|
},
|
||||||
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := s.ChannelMessageSendEmbed(dmCh.ID, embed); err != nil {
|
||||||
|
log.Printf("onboarding: failed sending setup DM to user %s: %v", recipientID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func findRecentBotAdderID(s *discordgo.Session, guildID string) string {
|
||||||
|
if s == nil || s.State == nil || s.State.User == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
logs, err := s.GuildAuditLog(guildID, "", "", int(discordgo.AuditLogActionBotAdd), 10)
|
||||||
|
if err != nil || logs == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
botID := s.State.User.ID
|
||||||
|
now := time.Now().UTC()
|
||||||
|
for _, entry := range logs.AuditLogEntries {
|
||||||
|
if entry == nil || entry.TargetID != botID || entry.ActionType == nil || *entry.ActionType != discordgo.AuditLogActionBotAdd {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ts, err := discordgo.SnowflakeTimestamp(entry.ID)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if now.Sub(ts) > 10*time.Minute {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if entry.UserID != "" {
|
||||||
|
return entry.UserID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func guildOwnerID(s *discordgo.Session, guildID string) string {
|
||||||
|
if s == nil || guildID == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if s.State != nil {
|
||||||
|
if g, err := s.State.Guild(guildID); err == nil && g != nil && g.OwnerID != "" {
|
||||||
|
return g.OwnerID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if g, err := s.Guild(guildID); err == nil && g != nil {
|
||||||
|
return g.OwnerID
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
package events
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StartScheduleReminderLoop runs a background loop that periodically checks for
|
||||||
|
// upcoming sessions and sends DM reminders to all participants.
|
||||||
|
func StartScheduleReminderLoop(s *discordgo.Session, svc *services.Services) {
|
||||||
|
if svc == nil || svc.Schedule == nil || s == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
interval = time.Minute // check every minute
|
||||||
|
remindBefore = 10 * time.Minute // remind 10 minutes before
|
||||||
|
initialDelay = 10 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
// small delay so the bot can fully start
|
||||||
|
time.Sleep(initialDelay)
|
||||||
|
|
||||||
|
ticker := time.NewTicker(interval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
now := time.Now().UTC()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Find all sessions that start in the next `remindBefore` window and haven't been reminded.
|
||||||
|
schedules, err := svc.Schedule.ListDueForReminder(ctx, now, remindBefore)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("schedule: reminder query failed: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, sch := range schedules {
|
||||||
|
if sch == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
utcStr := sch.ScheduledAt.UTC().Format("2006-01-02 15:04")
|
||||||
|
desc := sch.Description
|
||||||
|
|
||||||
|
// Notify requester with local time if available.
|
||||||
|
if svc.UserSettings != nil {
|
||||||
|
locReq, zoneReq, _, err := svc.UserSettings.GetTimezone(ctx, sch.RequesterID)
|
||||||
|
if err != nil {
|
||||||
|
locReq = sch.ScheduledAt.UTC().Location()
|
||||||
|
zoneReq = "UTC"
|
||||||
|
}
|
||||||
|
localReq := sch.ScheduledAt.In(locReq).Format("2006-01-02 15:04")
|
||||||
|
base := fmt.Sprintf("Reminder: you have a scheduled session at %s UTC (%s %s) with <@%d>.", utcStr, localReq, zoneReq, sch.InviteeID)
|
||||||
|
if desc != "" {
|
||||||
|
base += "\nTopic: " + desc
|
||||||
|
}
|
||||||
|
if err := dmUser(s, sch.RequesterID, base); err != nil {
|
||||||
|
log.Printf("schedule: failed to DM requester reminder: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify invitee with their local time if available.
|
||||||
|
if svc.UserSettings != nil {
|
||||||
|
locInv, zoneInv, _, err := svc.UserSettings.GetTimezone(ctx, sch.InviteeID)
|
||||||
|
if err != nil {
|
||||||
|
locInv = sch.ScheduledAt.UTC().Location()
|
||||||
|
zoneInv = "UTC"
|
||||||
|
}
|
||||||
|
localInv := sch.ScheduledAt.In(locInv).Format("2006-01-02 15:04")
|
||||||
|
baseInvitee := fmt.Sprintf("Reminder: you have a scheduled session at %s UTC (%s %s) with <@%d>.", utcStr, localInv, zoneInv, sch.RequesterID)
|
||||||
|
if desc != "" {
|
||||||
|
baseInvitee += "\nTopic: " + desc
|
||||||
|
}
|
||||||
|
if err := dmUser(s, sch.InviteeID, baseInvitee); err != nil {
|
||||||
|
log.Printf("schedule: failed to DM invitee reminder: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := svc.Schedule.MarkReminded(ctx, sch.ID); err != nil {
|
||||||
|
log.Printf("schedule: failed to mark reminder sent for id=%d: %v", sch.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func dmUser(s *discordgo.Session, userID int64, content string) error {
|
||||||
|
if s == nil || userID == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
uid := strconv.FormatInt(userID, 10)
|
||||||
|
ch, err := s.UserChannelCreate(uid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = s.ChannelMessageSend(ch.ID, content)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
package events
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StartTwitchLiveLoop periodically checks configured Twitch streamers and
|
||||||
|
// sends live notifications to the configured channel.
|
||||||
|
func StartTwitchLiveLoop(s *discordgo.Session, svc *services.Services) {
|
||||||
|
if svc == nil || svc.Twitch == nil || s == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
interval = 30 * time.Second
|
||||||
|
initialDelay = 10 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
time.Sleep(initialDelay)
|
||||||
|
|
||||||
|
log.Printf("twitch: starting live notification loop (interval=%s)", interval)
|
||||||
|
|
||||||
|
ticker := time.NewTicker(interval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
guildIDs, err := svc.Twitch.ListGuilds(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("twitch: failed to list guilds: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(guildIDs) == 0 {
|
||||||
|
log.Printf("twitch: no guilds with configured streamers")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, guildID := range guildIDs {
|
||||||
|
log.Printf("twitch: processing guild %d", guildID)
|
||||||
|
|
||||||
|
enabled, err := svc.Twitch.IsEnabled(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("twitch: failed to check enabled state for guild %d: %v", guildID, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !enabled {
|
||||||
|
log.Printf("twitch: notifications disabled for guild %d", guildID)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
channelID, ok, err := svc.Twitch.GetNotificationChannel(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("twitch: failed to get channel for guild %d: %v", guildID, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !ok || channelID == 0 {
|
||||||
|
log.Printf("twitch: no notification channel configured for guild %d", guildID)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
users, err := svc.Twitch.ListUsersForGuild(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("twitch: failed to list users for guild %d: %v", guildID, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(users) == 0 {
|
||||||
|
log.Printf("twitch: no streamers configured for guild %d", guildID)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
chIDStr := strconv.FormatInt(channelID, 10)
|
||||||
|
|
||||||
|
for _, username := range users {
|
||||||
|
log.Printf("twitch: checking live status for guild %d, user %s", guildID, username)
|
||||||
|
|
||||||
|
isLive, err := svc.Twitch.IsUserStreaming(ctx, username)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("twitch: failed to check stream for %s: %v", username, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
status, ok, err := svc.Twitch.GetStatus(ctx, guildID, username)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("twitch: failed to get status for %s: %v", username, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
status = "not live"
|
||||||
|
}
|
||||||
|
|
||||||
|
if isLive {
|
||||||
|
if status == "not live" {
|
||||||
|
log.Printf("twitch: %s went live in guild %d, sending notification to channel %s", username, guildID, chIDStr)
|
||||||
|
|
||||||
|
liveURL := "https://www.twitch.tv/" + username
|
||||||
|
content := "@everyone"
|
||||||
|
|
||||||
|
previewURL := "https://static-cdn.jtvnw.net/previews-ttv/live_user_" + username + "-640x360.jpg"
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: ":red_circle: " + username + " is now LIVE on Twitch!",
|
||||||
|
Description: "Click the link below to watch the stream.",
|
||||||
|
URL: liveURL,
|
||||||
|
Color: 0x9146FF, // Twitch purple
|
||||||
|
Image: &discordgo.MessageEmbedImage{
|
||||||
|
URL: previewURL,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := s.ChannelMessageSendComplex(chIDStr, &discordgo.MessageSend{
|
||||||
|
Content: content,
|
||||||
|
Embed: embed,
|
||||||
|
}); err != nil {
|
||||||
|
log.Printf("twitch: failed to send notification for %s: %v", username, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := svc.Twitch.UpdateStatus(ctx, guildID, username, "live"); err != nil {
|
||||||
|
log.Printf("twitch: failed to update status to live for %s in guild %d: %v", username, guildID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if status != "not live" {
|
||||||
|
log.Printf("twitch: %s is no longer live in guild %d, updating status", username, guildID)
|
||||||
|
if err := svc.Twitch.UpdateStatus(ctx, guildID, username, "not live"); err != nil {
|
||||||
|
log.Printf("twitch: failed to update status to not live for %s in guild %d: %v", username, guildID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
package events
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleGuildMemberAdd sends a configurable welcome message in a guild channel
|
||||||
|
// and an optional configurable DM to the joining user.
|
||||||
|
func HandleGuildMemberAdd(s *discordgo.Session, m *discordgo.GuildMemberAdd, svc *services.Services) {
|
||||||
|
if s == nil || m == nil || svc == nil || svc.Welcome == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if m.User == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
guildID := mustParseInt64(m.GuildID)
|
||||||
|
|
||||||
|
settings, err := svc.Welcome.GetSettings(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("welcome: failed to load settings for guild %s: %v", m.GuildID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hasWelcomeChannel := settings != nil && settings.ChannelID != 0
|
||||||
|
|
||||||
|
memberMention := m.User.Mention()
|
||||||
|
|
||||||
|
// Resolve guild/server name and user avatar for placeholders / visuals.
|
||||||
|
guildName := ""
|
||||||
|
userAvatarURL := ""
|
||||||
|
if g, err := s.State.Guild(m.GuildID); err == nil && g != nil {
|
||||||
|
guildName = g.Name
|
||||||
|
} else if g, err := s.Guild(m.GuildID); err == nil && g != nil {
|
||||||
|
guildName = g.Name
|
||||||
|
}
|
||||||
|
if m.User.Avatar != "" {
|
||||||
|
userAvatarURL = discordgo.EndpointUserAvatar(m.User.ID, m.User.Avatar)
|
||||||
|
}
|
||||||
|
|
||||||
|
if hasWelcomeChannel {
|
||||||
|
// Build guild welcome message.
|
||||||
|
channelID := strconv.FormatInt(settings.ChannelID, 10)
|
||||||
|
welcomeMsg := strings.TrimSpace(settings.WelcomeMessage)
|
||||||
|
if welcomeMsg == "" {
|
||||||
|
// Default message when none configured.
|
||||||
|
// "<user>! Welcome to <server>! Have fun!"
|
||||||
|
welcomeMsg = "<user>! Welcome to <server>! Have fun!"
|
||||||
|
}
|
||||||
|
welcomeMsg = strings.ReplaceAll(welcomeMsg, "{user}", memberMention)
|
||||||
|
if guildName != "" {
|
||||||
|
welcomeMsg = strings.ReplaceAll(welcomeMsg, "{server}", guildName)
|
||||||
|
welcomeMsg = strings.ReplaceAll(welcomeMsg, "<server>", guildName)
|
||||||
|
}
|
||||||
|
welcomeMsg = strings.ReplaceAll(welcomeMsg, "<user>", memberMention)
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "👋 Welcome!",
|
||||||
|
Description: welcomeMsg,
|
||||||
|
Color: 0xFFA500,
|
||||||
|
Footer: &discordgo.MessageEmbedFooter{
|
||||||
|
Text: "ID: " + m.User.ID,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if userAvatarURL != "" {
|
||||||
|
embed.Author = &discordgo.MessageEmbedAuthor{
|
||||||
|
Name: m.User.Username,
|
||||||
|
IconURL: userAvatarURL,
|
||||||
|
}
|
||||||
|
embed.Thumbnail = &discordgo.MessageEmbedThumbnail{
|
||||||
|
URL: userAvatarURL,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gif := strings.TrimSpace(settings.WelcomeGIFURL)
|
||||||
|
if gif == "" {
|
||||||
|
// Default GIF when none configured.
|
||||||
|
gif = "https://images-ext-1.discordapp.net/external/uJ6XfdK2WwDnei3RmNWUqSiOVboC4mK9r78TtgVE_9g/https/media.giphy.com/media/XD9o33QG9BoMis7iM4/giphy.gif"
|
||||||
|
}
|
||||||
|
embed.Image = &discordgo.MessageEmbedImage{URL: gif}
|
||||||
|
|
||||||
|
if _, err := s.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{
|
||||||
|
Content: memberMention,
|
||||||
|
Embed: embed,
|
||||||
|
}); err != nil {
|
||||||
|
log.Printf("welcome: failed to send channel welcome in guild %s: %v", m.GuildID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optional DM.
|
||||||
|
dmText := ""
|
||||||
|
if settings != nil {
|
||||||
|
dmText = strings.TrimSpace(settings.WelcomeDM)
|
||||||
|
}
|
||||||
|
if dmText == "" {
|
||||||
|
// Default DM when none configured.
|
||||||
|
// "Welcome to <server>! Have fun!"
|
||||||
|
dmText = "Welcome to <server>! Have fun!"
|
||||||
|
}
|
||||||
|
dmText = strings.ReplaceAll(dmText, "{user}", memberMention)
|
||||||
|
if guildName != "" {
|
||||||
|
dmText = strings.ReplaceAll(dmText, "{server}", guildName)
|
||||||
|
dmText = strings.ReplaceAll(dmText, "<server>", guildName)
|
||||||
|
}
|
||||||
|
|
||||||
|
dmCh, err := s.UserChannelCreate(m.User.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("welcome: failed to create DM channel for user %s: %v", m.User.ID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := s.ChannelMessageSend(dmCh.ID, dmText); err != nil {
|
||||||
|
log.Printf("welcome: failed to send DM welcome to user %s: %v", m.User.ID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default role assignment.
|
||||||
|
if svc.DefaultRole != nil {
|
||||||
|
roleID, err := svc.DefaultRole.GetRole(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("defaultrole: failed to get default role for guild %s: %v", m.GuildID, err)
|
||||||
|
} else if roleID != 0 {
|
||||||
|
if err := s.GuildMemberRoleAdd(m.GuildID, m.User.ID, strconv.FormatInt(roleID, 10)); err != nil {
|
||||||
|
log.Printf("defaultrole: failed to assign role %d to user %s in guild %s: %v", roleID, m.User.ID, m.GuildID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package modlog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/services"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Send(s *discordgo.Session, svc *services.Services, guildID string, embed *discordgo.MessageEmbed) error {
|
||||||
|
if s == nil || svc == nil || svc.LogSettings == nil || embed == nil || guildID == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
gid, err := strconv.ParseInt(guildID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := svc.LogSettings.GetSettings(context.Background(), gid)
|
||||||
|
if err != nil || cfg == nil || !cfg.IsEnabled || cfg.ChannelID == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = s.ChannelMessageSendEmbed(strconv.FormatInt(cfg.ChannelID, 10), embed)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
package music
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HandleComponent(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
// DJ role is required for all music controls.
|
||||||
|
if !memberHasDJRoleForComponents(s, i.GuildID, i.Member) {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "You need the **DJ** role to use music commands.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
customID := i.MessageComponentData().CustomID
|
||||||
|
parts := strings.Split(customID, ":")
|
||||||
|
if len(parts) < 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
action := parts[1]
|
||||||
|
guildID := i.GuildID
|
||||||
|
|
||||||
|
// Ignore controls from old now-playing messages
|
||||||
|
currentMsgID := CurrentNowPlayingMessageID(guildID)
|
||||||
|
if currentMsgID != "" && i.Message != nil && i.Message.ID != currentMsgID {
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "These controls are outdated.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Acknowledge immediately to avoid "This interaction failed".
|
||||||
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseDeferredMessageUpdate,
|
||||||
|
})
|
||||||
|
|
||||||
|
var err error
|
||||||
|
content := ""
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case "pause":
|
||||||
|
err = Pause(guildID, true)
|
||||||
|
content = "Paused."
|
||||||
|
case "resume":
|
||||||
|
err = Pause(guildID, false)
|
||||||
|
content = "Resumed."
|
||||||
|
case "toggle_pause":
|
||||||
|
paused, _ := pausedAndVolume(guildID)
|
||||||
|
err = Pause(guildID, !paused)
|
||||||
|
if paused {
|
||||||
|
content = "Resumed."
|
||||||
|
} else {
|
||||||
|
content = "Paused."
|
||||||
|
}
|
||||||
|
updateNowPlayingButtons(i.ChannelID, i.Message.ID, guildID)
|
||||||
|
case "skip":
|
||||||
|
err = Skip(guildID)
|
||||||
|
content = "Skipped."
|
||||||
|
case "stop":
|
||||||
|
err = Stop(guildID)
|
||||||
|
content = "Stopped."
|
||||||
|
case "repeat_song":
|
||||||
|
rs, rq := ToggleRepeatSong(guildID)
|
||||||
|
_ = rs
|
||||||
|
_ = rq
|
||||||
|
updateNowPlayingButtons(i.ChannelID, i.Message.ID, guildID)
|
||||||
|
if rs {
|
||||||
|
content = "Repeat song enabled."
|
||||||
|
} else {
|
||||||
|
content = "Repeat song disabled."
|
||||||
|
}
|
||||||
|
case "repeat_queue":
|
||||||
|
rs, rq := ToggleRepeatQueue(guildID)
|
||||||
|
_ = rs
|
||||||
|
_ = rq
|
||||||
|
updateNowPlayingButtons(i.ChannelID, i.Message.ID, guildID)
|
||||||
|
if rq {
|
||||||
|
content = "Repeat queue enabled."
|
||||||
|
} else {
|
||||||
|
content = "Repeat queue disabled."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if content == "" {
|
||||||
|
content = "Done."
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
// err can carry a raw Lavalink/network failure here (Player.Update),
|
||||||
|
// not just our own "music manager not initialized" - don't echo
|
||||||
|
// that detail into the ephemeral reply.
|
||||||
|
content = "That didn't work. Please try again."
|
||||||
|
}
|
||||||
|
|
||||||
|
// send ephemeral confirmation
|
||||||
|
_, _ = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
|
||||||
|
Content: content,
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
package music
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"velox-bot/internal/db/repos/musicrepo"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func StartCommandListener(ctx context.Context, connString string, repo *musicrepo.Repo) error {
|
||||||
|
conn, err := pgx.Connect(ctx, connString)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("connect: %w", err)
|
||||||
|
}
|
||||||
|
defer conn.Close(ctx)
|
||||||
|
|
||||||
|
_, err = conn.Exec(ctx, "LISTEN music_commands")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("exec: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
_, err := conn.WaitForNotification(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("waitfornotification: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
processCommands(ctx, repo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func processCommands(ctx context.Context, repo *musicrepo.Repo) {
|
||||||
|
cmds, err := repo.ListUnprocessedCommands(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("music: failed to list unprocessed commands: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cmd := range cmds {
|
||||||
|
if err := dispatchCommand(cmd); err != nil {
|
||||||
|
log.Printf("music: command %d (%s) for guild %d failed: %v", cmd.ID, cmd.CommandType, cmd.GuildID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := repo.MarkCommandProcessed(ctx, cmd.ID); err != nil {
|
||||||
|
log.Printf("music: failed to mark command %d processed: %v", cmd.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dispatchCommand(cmd musicrepo.MusicCommand) error {
|
||||||
|
guildID := strconv.FormatInt(cmd.GuildID, 10)
|
||||||
|
|
||||||
|
switch cmd.CommandType {
|
||||||
|
case "skip":
|
||||||
|
return Skip(guildID)
|
||||||
|
|
||||||
|
case "pause":
|
||||||
|
return Pause(guildID, true)
|
||||||
|
|
||||||
|
case "resume":
|
||||||
|
return Pause(guildID, false)
|
||||||
|
|
||||||
|
case "stop":
|
||||||
|
return Stop(guildID)
|
||||||
|
|
||||||
|
case "set_volume":
|
||||||
|
var payload struct {
|
||||||
|
Volume int `json:"volume"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(cmd.Payload, &payload); err != nil {
|
||||||
|
return fmt.Errorf("unmarshal payload: %w", err)
|
||||||
|
}
|
||||||
|
return SetVolume(guildID, payload.Volume)
|
||||||
|
|
||||||
|
case "remove_track":
|
||||||
|
var payload struct {
|
||||||
|
QueueID string `json:"queue_id"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(cmd.Payload, &payload); err != nil {
|
||||||
|
return fmt.Errorf("unmarshal payload: %w", err)
|
||||||
|
}
|
||||||
|
return RemoveFromQueue(guildID, payload.QueueID)
|
||||||
|
|
||||||
|
case "reorder":
|
||||||
|
var payload struct {
|
||||||
|
QueueID string `json:"queue_id"`
|
||||||
|
Position int `json:"position"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(cmd.Payload, &payload); err != nil {
|
||||||
|
return fmt.Errorf("unmarshal payload: %w", err)
|
||||||
|
}
|
||||||
|
return Reorder(guildID, payload.QueueID, payload.Position)
|
||||||
|
|
||||||
|
case "repeat_song":
|
||||||
|
ToggleRepeatSong(guildID)
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case "repeat_queue":
|
||||||
|
ToggleRepeatQueue(guildID)
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case "add_song":
|
||||||
|
var payload struct {
|
||||||
|
Query string `json:"query"`
|
||||||
|
VoiceChannelID string `json:"voice_channel_id"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(cmd.Payload, &payload); err != nil {
|
||||||
|
return fmt.Errorf("unmarshal payload: %w", err)
|
||||||
|
}
|
||||||
|
requesterID := strconv.FormatInt(cmd.RequestedBy, 10)
|
||||||
|
|
||||||
|
// A voice channel means "start fresh" (nothing was playing, the
|
||||||
|
// dashboard had the user pick one), otherwise this just adds to
|
||||||
|
// whatever's already going.
|
||||||
|
if payload.VoiceChannelID != "" {
|
||||||
|
// Voice channels have their own built-in text chat at the same
|
||||||
|
// channel ID, post the now-playing embed there so starting
|
||||||
|
// playback from the dashboard still shows up somewhere in
|
||||||
|
// Discord, same as starting it with /play would.
|
||||||
|
_, _, err := EnqueueAndPlay(guildID, payload.VoiceChannelID, payload.VoiceChannelID, payload.Query, "Dashboard", requesterID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err := AddToQueue(guildID, payload.Query, requesterID)
|
||||||
|
return err
|
||||||
|
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unknown command type %q", cmd.CommandType)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,820 @@
|
|||||||
|
package music
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
"velox-bot/internal/db/repos/musicrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/disgoorg/disgolink/v3/disgolink"
|
||||||
|
"github.com/disgoorg/disgolink/v3/lavalink"
|
||||||
|
"github.com/disgoorg/snowflake/v2"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TrackEntry struct {
|
||||||
|
Track lavalink.Track
|
||||||
|
RequestedBy string
|
||||||
|
RequesterID string
|
||||||
|
StartedAt time.Time
|
||||||
|
|
||||||
|
// QueueID is generated once, when the track is first added to the
|
||||||
|
// queue, and never changes for the rest of its life there - it's
|
||||||
|
// what lets syncState upsert this track's row across syncs instead
|
||||||
|
// of deleting and recreating it (and its DB id) every single time,
|
||||||
|
// which is what remove/reorder commands need to reliably target a
|
||||||
|
// specific track.
|
||||||
|
QueueID string
|
||||||
|
}
|
||||||
|
|
||||||
|
type guildPlayer struct {
|
||||||
|
Player disgolink.Player
|
||||||
|
Queue []TrackEntry
|
||||||
|
RepeatSong bool
|
||||||
|
RepeatQueue bool
|
||||||
|
Paused bool
|
||||||
|
Volume int
|
||||||
|
IdleSince time.Time
|
||||||
|
TextChannelID string
|
||||||
|
NowPlayingMsgID string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Manager struct {
|
||||||
|
client disgolink.Client
|
||||||
|
session *discordgo.Session
|
||||||
|
repo *musicrepo.Repo
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
players map[string]*guildPlayer
|
||||||
|
}
|
||||||
|
|
||||||
|
var manager *Manager
|
||||||
|
|
||||||
|
func Init(session *discordgo.Session, repo *musicrepo.Repo, appID, lavalinkHost, lavalinkPass string) error {
|
||||||
|
if lavalinkHost == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
userID, err := snowflake.Parse(appID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("parse app id for lavalink: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
m := &Manager{
|
||||||
|
client: disgolink.New(userID, disgolink.WithListenerFunc(onTrackEnd)),
|
||||||
|
session: session,
|
||||||
|
repo: repo,
|
||||||
|
players: make(map[string]*guildPlayer),
|
||||||
|
}
|
||||||
|
|
||||||
|
u, err := url.Parse(lavalinkHost)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("parse lavalink host: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
secure := u.Scheme == "https" || u.Scheme == "wss"
|
||||||
|
password := lavalinkPass
|
||||||
|
if password == "" {
|
||||||
|
password = "youshallnotpass"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connecting to the Lavalink node is a blocking network call (and, on
|
||||||
|
// failure, disgolink retries indefinitely with backoff) — do it in the
|
||||||
|
// background so bot startup isn't held hostage by it. Music commands
|
||||||
|
// simply fail with "not initialized" via the manager==nil checks until
|
||||||
|
// this completes.
|
||||||
|
go func() {
|
||||||
|
_, err := m.client.AddNode(context.Background(), disgolink.NodeConfig{
|
||||||
|
Name: "main",
|
||||||
|
Address: u.Host,
|
||||||
|
Password: password,
|
||||||
|
Secure: secure,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("music: failed to connect to lavalink node: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
manager = m
|
||||||
|
go idleDisconnectLoop()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func idleDisconnectLoop() {
|
||||||
|
ticker := time.NewTicker(10 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
if manager == nil || manager.session == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
var toDisconnect []string
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
for guildID, gp := range manager.players {
|
||||||
|
if gp == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we're paused or have anything queued/playing, clear idle timer.
|
||||||
|
if gp.Paused || len(gp.Queue) > 0 {
|
||||||
|
gp.IdleSince = time.Time{}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if gp.IdleSince.IsZero() {
|
||||||
|
gp.IdleSince = now
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if now.Sub(gp.IdleSince) >= time.Minute {
|
||||||
|
toDisconnect = append(toDisconnect, guildID)
|
||||||
|
gp.IdleSince = time.Time{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
for _, guildID := range toDisconnect {
|
||||||
|
// Best-effort: disconnect. Safe even if we're already disconnected.
|
||||||
|
_ = manager.session.ChannelVoiceJoinManual(guildID, "", false, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func OnVoiceStateUpdate(vs *discordgo.VoiceStateUpdate) {
|
||||||
|
if manager == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if vs.UserID != manager.session.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID := snowflake.MustParse(vs.GuildID)
|
||||||
|
var channelID *snowflake.ID
|
||||||
|
if vs.ChannelID != "" {
|
||||||
|
id := snowflake.MustParse(vs.ChannelID)
|
||||||
|
channelID = &id
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.client.OnVoiceStateUpdate(
|
||||||
|
context.Background(),
|
||||||
|
guildID,
|
||||||
|
channelID,
|
||||||
|
vs.SessionID,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func OnVoiceServerUpdate(ev *discordgo.VoiceServerUpdate) {
|
||||||
|
if manager == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
endpoint := ""
|
||||||
|
if ev.Endpoint != "" {
|
||||||
|
endpoint = ev.Endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.client.OnVoiceServerUpdate(
|
||||||
|
context.Background(),
|
||||||
|
snowflake.MustParse(ev.GuildID),
|
||||||
|
ev.Token,
|
||||||
|
endpoint,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getOrCreateGuildPlayer(guildID string) *guildPlayer {
|
||||||
|
manager.mu.Lock()
|
||||||
|
defer manager.mu.Unlock()
|
||||||
|
|
||||||
|
if gp, ok := manager.players[guildID]; ok {
|
||||||
|
return gp
|
||||||
|
}
|
||||||
|
|
||||||
|
player := manager.client.Player(snowflake.MustParse(guildID))
|
||||||
|
gp := &guildPlayer{
|
||||||
|
Player: player,
|
||||||
|
Queue: make([]TrackEntry, 0),
|
||||||
|
Volume: 100,
|
||||||
|
}
|
||||||
|
manager.players[guildID] = gp
|
||||||
|
return gp
|
||||||
|
}
|
||||||
|
|
||||||
|
func syncState(guidID string) {
|
||||||
|
if manager == nil || manager.repo == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
manager.mu.Lock()
|
||||||
|
gp, ok := manager.players[guidID]
|
||||||
|
if !ok {
|
||||||
|
manager.mu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
queue := make([]TrackEntry, len(gp.Queue))
|
||||||
|
copy(queue, gp.Queue)
|
||||||
|
paused := gp.Paused
|
||||||
|
volume := gp.Volume
|
||||||
|
repeatSong := gp.RepeatSong
|
||||||
|
repeatQueue := gp.RepeatQueue
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
gID, err := strconv.ParseInt(guidID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(queue) == 0 {
|
||||||
|
err := manager.repo.ClearNowPlaying(context.Background(), gID)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = manager.repo.ReplaceQueue(context.Background(), gID, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
current := queue[0]
|
||||||
|
title := current.Track.Info.Title
|
||||||
|
|
||||||
|
var uri string
|
||||||
|
if current.Track.Info.URI != nil {
|
||||||
|
uri = *current.Track.Info.URI
|
||||||
|
}
|
||||||
|
|
||||||
|
length := int(current.Track.Info.Length / 1000)
|
||||||
|
startedAt := current.StartedAt
|
||||||
|
|
||||||
|
err = manager.repo.SetNowPlaying(context.Background(), gID, title, uri, length, startedAt, paused, volume, repeatSong, repeatQueue)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rest := queue[1:]
|
||||||
|
var queueEntries []musicrepo.QueueEntry
|
||||||
|
for _, entry := range rest {
|
||||||
|
reqID, _ := strconv.ParseInt(entry.RequesterID, 10, 64)
|
||||||
|
var uri string
|
||||||
|
if entry.Track.Info.URI != nil {
|
||||||
|
uri = *entry.Track.Info.URI
|
||||||
|
}
|
||||||
|
queueEntries = append(queueEntries, musicrepo.QueueEntry{
|
||||||
|
Title: entry.Track.Info.Title,
|
||||||
|
URL: uri,
|
||||||
|
RequestedBy: reqID,
|
||||||
|
ClientID: entry.QueueID,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
err = manager.repo.ReplaceQueue(context.Background(), gID, queueEntries)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// NormalizeQuery turns plain text into a YouTube search and strips
|
||||||
|
// auto-generated "radio/mix" params off a pasted YouTube URL
|
||||||
|
// (https://www.youtube.com/watch?v=ID&list=RDID&start_radio=1 becomes
|
||||||
|
// .../watch?v=ID), while leaving normal playlist URLs and non-YouTube
|
||||||
|
// links (SoundCloud, etc.) untouched. Every entry point that accepts a
|
||||||
|
// user-supplied query - /play and the dashboard's "add song" - should
|
||||||
|
// normalize through this before handing the query to Lavalink.
|
||||||
|
func NormalizeQuery(query string) string {
|
||||||
|
if !strings.HasPrefix(query, "http://") && !strings.HasPrefix(query, "https://") && !strings.HasPrefix(query, "ytsearch:") {
|
||||||
|
return "ytsearch:" + query
|
||||||
|
}
|
||||||
|
|
||||||
|
u, err := url.Parse(query)
|
||||||
|
if err != nil || u == nil {
|
||||||
|
return query
|
||||||
|
}
|
||||||
|
|
||||||
|
host := strings.ToLower(u.Host)
|
||||||
|
if !strings.Contains(host, "youtube.com") || u.Path != "/watch" {
|
||||||
|
return query
|
||||||
|
}
|
||||||
|
|
||||||
|
q := u.Query()
|
||||||
|
v := q.Get("v")
|
||||||
|
if v == "" {
|
||||||
|
return query
|
||||||
|
}
|
||||||
|
|
||||||
|
list := q.Get("list")
|
||||||
|
_, hasStartRadio := q["start_radio"]
|
||||||
|
if !hasStartRadio && !strings.HasPrefix(list, "RD") {
|
||||||
|
return query
|
||||||
|
}
|
||||||
|
|
||||||
|
u.RawQuery = url.Values{"v": []string{v}}.Encode()
|
||||||
|
u.Fragment = ""
|
||||||
|
return u.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// resolveTracks searches Lavalink for query (already normalized) and
|
||||||
|
// returns a playable track list - a single track, a search result's
|
||||||
|
// first hit, or a whole playlist (rotated to respect Lavalink's own
|
||||||
|
// selectedTrack when present).
|
||||||
|
func resolveTracks(query string) ([]lavalink.Track, error) {
|
||||||
|
var loadedTracks []lavalink.Track
|
||||||
|
|
||||||
|
manager.client.BestNode().LoadTracksHandler(
|
||||||
|
context.Background(),
|
||||||
|
query,
|
||||||
|
disgolink.NewResultHandler(
|
||||||
|
func(track lavalink.Track) {
|
||||||
|
loadedTracks = append(loadedTracks, track)
|
||||||
|
},
|
||||||
|
func(playlist lavalink.Playlist) {
|
||||||
|
if len(playlist.Tracks) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Respect Lavalink's selectedTrack when present by rotating the playlist.
|
||||||
|
selected := playlist.Info.SelectedTrack
|
||||||
|
if selected >= 0 && selected < len(playlist.Tracks) {
|
||||||
|
loadedTracks = append(loadedTracks, playlist.Tracks[selected:]...)
|
||||||
|
loadedTracks = append(loadedTracks, playlist.Tracks[:selected]...)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
loadedTracks = append(loadedTracks, playlist.Tracks...)
|
||||||
|
},
|
||||||
|
func(tracks []lavalink.Track) {
|
||||||
|
if len(tracks) > 0 {
|
||||||
|
loadedTracks = append(loadedTracks, tracks[0])
|
||||||
|
}
|
||||||
|
},
|
||||||
|
func() {
|
||||||
|
},
|
||||||
|
func(err error) {
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(loadedTracks) == 0 {
|
||||||
|
return nil, fmt.Errorf("no tracks found for query")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Some sources may return unplayable entries; pick the first with an encoded track.
|
||||||
|
firstPlayableIdx := 0
|
||||||
|
for idx := range loadedTracks {
|
||||||
|
if loadedTracks[idx].Encoded != "" {
|
||||||
|
firstPlayableIdx = idx
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if firstPlayableIdx != 0 && firstPlayableIdx < len(loadedTracks) {
|
||||||
|
loadedTracks = append(loadedTracks[firstPlayableIdx:], loadedTracks[:firstPlayableIdx]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return loadedTracks, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func EnqueueAndPlay(guildID, voiceChannelID, textChannelID, query, requester, requesterID string) (*TrackEntry, bool, error) {
|
||||||
|
if manager == nil {
|
||||||
|
return nil, false, fmt.Errorf("music manager not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join voice channel
|
||||||
|
if err := manager.session.ChannelVoiceJoinManual(guildID, voiceChannelID, false, false); err != nil {
|
||||||
|
return nil, false, fmt.Errorf("join voice channel: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
if textChannelID != "" {
|
||||||
|
manager.mu.Lock()
|
||||||
|
gp.TextChannelID = textChannelID
|
||||||
|
manager.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
loadedTracks, err := resolveTracks(NormalizeQuery(query))
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
entries := make([]TrackEntry, len(loadedTracks))
|
||||||
|
for idx, t := range loadedTracks {
|
||||||
|
entries[idx] = TrackEntry{
|
||||||
|
Track: t,
|
||||||
|
RequestedBy: requester,
|
||||||
|
RequesterID: requesterID,
|
||||||
|
QueueID: uuid.New().String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
firstEntry := entries[0]
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
shouldStart := len(gp.Queue) == 0
|
||||||
|
gp.Queue = append(gp.Queue, entries...)
|
||||||
|
gp.IdleSince = time.Time{}
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
if shouldStart {
|
||||||
|
gp.Queue[0].StartedAt = time.Now()
|
||||||
|
|
||||||
|
if err := gp.Player.Update(context.Background(), lavalink.WithTrack(firstEntry.Track)); err != nil {
|
||||||
|
return nil, false, fmt.Errorf("start track: %w", err)
|
||||||
|
}
|
||||||
|
if gp.TextChannelID != "" {
|
||||||
|
postNowPlaying(guildID, gp.TextChannelID, firstEntry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
syncState(guildID)
|
||||||
|
|
||||||
|
return &firstEntry, shouldStart, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddToQueue adds a track to a guild's existing queue without joining a
|
||||||
|
// voice channel, unlike EnqueueAndPlay it can't start a brand new
|
||||||
|
// session (that needs a voice channel to join, which this has no way to
|
||||||
|
// pick), so it requires one to already be active.
|
||||||
|
func AddToQueue(guildID, query, requesterID string) (*TrackEntry, error) {
|
||||||
|
if manager == nil {
|
||||||
|
return nil, fmt.Errorf("music manager not initialized")
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
alreadyActive := len(gp.Queue) > 0
|
||||||
|
manager.mu.Unlock()
|
||||||
|
if !alreadyActive {
|
||||||
|
return nil, fmt.Errorf("nothing is currently playing in this server")
|
||||||
|
}
|
||||||
|
|
||||||
|
loadedTracks, err := resolveTracks(NormalizeQuery(query))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
entries := make([]TrackEntry, len(loadedTracks))
|
||||||
|
for idx, t := range loadedTracks {
|
||||||
|
entries[idx] = TrackEntry{
|
||||||
|
Track: t,
|
||||||
|
RequestedBy: "Dashboard",
|
||||||
|
RequesterID: requesterID,
|
||||||
|
QueueID: uuid.New().String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
gp.Queue = append(gp.Queue, entries...)
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
syncState(guildID)
|
||||||
|
|
||||||
|
return &entries[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func onTrackEnd(player disgolink.Player, event lavalink.TrackEndEvent) {
|
||||||
|
if manager == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !event.Reason.MayStartNext() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guildID := player.GuildID().String()
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
gp, ok := manager.players[guildID]
|
||||||
|
if !ok || len(gp.Queue) == 0 {
|
||||||
|
manager.mu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// repeat current track
|
||||||
|
if gp.RepeatSong {
|
||||||
|
gp.Queue[0].StartedAt = time.Now()
|
||||||
|
next := gp.Queue[0]
|
||||||
|
textChannelID := gp.TextChannelID
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
if err := gp.Player.Update(context.Background(), lavalink.WithTrack(next.Track)); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if textChannelID != "" {
|
||||||
|
postNowPlaying(guildID, textChannelID, next)
|
||||||
|
}
|
||||||
|
syncState(guildID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// pop current and optionally cycle it to end
|
||||||
|
finished := gp.Queue[0]
|
||||||
|
gp.Queue = gp.Queue[1:]
|
||||||
|
|
||||||
|
if gp.RepeatQueue {
|
||||||
|
gp.Queue = append(gp.Queue, finished)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(gp.Queue) == 0 {
|
||||||
|
textChannelID := gp.TextChannelID
|
||||||
|
nowPlayingMsgID := gp.NowPlayingMsgID
|
||||||
|
gp.NowPlayingMsgID = ""
|
||||||
|
gp.IdleSince = time.Now()
|
||||||
|
manager.mu.Unlock()
|
||||||
|
if textChannelID != "" && nowPlayingMsgID != "" {
|
||||||
|
disableNowPlaying(textChannelID, nowPlayingMsgID)
|
||||||
|
}
|
||||||
|
_ = gp.Player.Update(context.Background(), lavalink.WithNullTrack())
|
||||||
|
syncState(guildID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
gp.Queue[0].StartedAt = time.Now()
|
||||||
|
next := gp.Queue[0]
|
||||||
|
textChannelID := gp.TextChannelID
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
if err := gp.Player.Update(context.Background(), lavalink.WithTrack(next.Track)); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if textChannelID != "" {
|
||||||
|
postNowPlaying(guildID, textChannelID, next)
|
||||||
|
}
|
||||||
|
syncState(guildID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Pause(guildID string, pause bool) error {
|
||||||
|
if manager == nil {
|
||||||
|
return fmt.Errorf("music manager not initialized")
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
manager.mu.Lock()
|
||||||
|
gp.Paused = pause
|
||||||
|
if pause {
|
||||||
|
gp.IdleSince = time.Time{}
|
||||||
|
}
|
||||||
|
manager.mu.Unlock()
|
||||||
|
syncState(guildID)
|
||||||
|
return gp.Player.Update(context.Background(), lavalink.WithPaused(pause))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Skip(guildID string) error {
|
||||||
|
if manager == nil {
|
||||||
|
return fmt.Errorf("music manager not initialized")
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
if len(gp.Queue) == 0 {
|
||||||
|
manager.mu.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// skip always advances regardless of repeat song
|
||||||
|
skipped := gp.Queue[0]
|
||||||
|
gp.Queue = gp.Queue[1:]
|
||||||
|
if gp.RepeatQueue {
|
||||||
|
gp.Queue = append(gp.Queue, skipped)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(gp.Queue) == 0 {
|
||||||
|
textChannelID := gp.TextChannelID
|
||||||
|
nowPlayingMsgID := gp.NowPlayingMsgID
|
||||||
|
gp.NowPlayingMsgID = ""
|
||||||
|
gp.IdleSince = time.Now()
|
||||||
|
manager.mu.Unlock()
|
||||||
|
if textChannelID != "" && nowPlayingMsgID != "" {
|
||||||
|
disableNowPlaying(textChannelID, nowPlayingMsgID)
|
||||||
|
}
|
||||||
|
syncState(guildID)
|
||||||
|
return gp.Player.Update(context.Background(), lavalink.WithNullTrack())
|
||||||
|
}
|
||||||
|
|
||||||
|
gp.Queue[0].StartedAt = time.Now()
|
||||||
|
next := gp.Queue[0]
|
||||||
|
textChannelID := gp.TextChannelID
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
if err := gp.Player.Update(context.Background(), lavalink.WithTrack(next.Track)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if textChannelID != "" {
|
||||||
|
postNowPlaying(guildID, textChannelID, next)
|
||||||
|
}
|
||||||
|
syncState(guildID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Stop(guildID string) error {
|
||||||
|
if manager == nil {
|
||||||
|
return fmt.Errorf("music manager not initialized")
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
textChannelID := gp.TextChannelID
|
||||||
|
nowPlayingMsgID := gp.NowPlayingMsgID
|
||||||
|
gp.Queue = nil
|
||||||
|
gp.Paused = false
|
||||||
|
gp.NowPlayingMsgID = ""
|
||||||
|
gp.IdleSince = time.Now()
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
if textChannelID != "" && nowPlayingMsgID != "" {
|
||||||
|
disableNowPlaying(textChannelID, nowPlayingMsgID)
|
||||||
|
}
|
||||||
|
syncState(guildID)
|
||||||
|
|
||||||
|
return gp.Player.Update(context.Background(), lavalink.WithNullTrack())
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetQueue(guildID string) (*TrackEntry, []TrackEntry) {
|
||||||
|
if manager == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
defer manager.mu.Unlock()
|
||||||
|
|
||||||
|
if len(gp.Queue) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
current := gp.Queue[0]
|
||||||
|
rest := make([]TrackEntry, len(gp.Queue)-1)
|
||||||
|
copy(rest, gp.Queue[1:])
|
||||||
|
return ¤t, rest
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToggleRepeatSong(guildID string) (bool, bool) {
|
||||||
|
if manager == nil {
|
||||||
|
return false, false
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
manager.mu.Lock()
|
||||||
|
gp.RepeatSong = !gp.RepeatSong
|
||||||
|
if gp.RepeatSong {
|
||||||
|
gp.RepeatQueue = false
|
||||||
|
}
|
||||||
|
repeatSong, repeatQueue := gp.RepeatSong, gp.RepeatQueue
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
syncState(guildID)
|
||||||
|
return repeatSong, repeatQueue
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToggleRepeatQueue(guildID string) (bool, bool) {
|
||||||
|
if manager == nil {
|
||||||
|
return false, false
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
manager.mu.Lock()
|
||||||
|
gp.RepeatQueue = !gp.RepeatQueue
|
||||||
|
if gp.RepeatQueue {
|
||||||
|
gp.RepeatSong = false
|
||||||
|
}
|
||||||
|
repeatSong, repeatQueue := gp.RepeatSong, gp.RepeatQueue
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
syncState(guildID)
|
||||||
|
return repeatSong, repeatQueue
|
||||||
|
}
|
||||||
|
|
||||||
|
func repeatFlags(guildID string) (bool, bool) {
|
||||||
|
if manager == nil {
|
||||||
|
return false, false
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
manager.mu.Lock()
|
||||||
|
defer manager.mu.Unlock()
|
||||||
|
return gp.RepeatSong, gp.RepeatQueue
|
||||||
|
}
|
||||||
|
|
||||||
|
func pausedAndVolume(guildID string) (bool, int) {
|
||||||
|
if manager == nil {
|
||||||
|
return false, 100
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
manager.mu.Lock()
|
||||||
|
defer manager.mu.Unlock()
|
||||||
|
vol := gp.Volume
|
||||||
|
if vol <= 0 {
|
||||||
|
vol = 100
|
||||||
|
}
|
||||||
|
return gp.Paused, vol
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetVolume(guildID string, volume int) error {
|
||||||
|
if manager == nil {
|
||||||
|
return fmt.Errorf("music manager not initialized")
|
||||||
|
}
|
||||||
|
if volume < 0 {
|
||||||
|
volume = 0
|
||||||
|
}
|
||||||
|
if volume > 150 {
|
||||||
|
volume = 150
|
||||||
|
}
|
||||||
|
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
manager.mu.Lock()
|
||||||
|
gp.Volume = volume
|
||||||
|
textChannelID := gp.TextChannelID
|
||||||
|
nowPlayingMsgID := gp.NowPlayingMsgID
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
if err := gp.Player.Update(context.Background(), lavalink.WithVolume(volume)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Refresh the embed so the displayed volume matches.
|
||||||
|
if textChannelID != "" && nowPlayingMsgID != "" {
|
||||||
|
current, _ := GetQueue(guildID)
|
||||||
|
if current != nil {
|
||||||
|
postNowPlaying(guildID, textChannelID, *current)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
syncState(guildID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveFromQueue(guildID, queueID string) error {
|
||||||
|
if manager == nil {
|
||||||
|
return fmt.Errorf("music manager not initialized")
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
idx := -1
|
||||||
|
for i, q := range gp.Queue {
|
||||||
|
if q.QueueID == queueID {
|
||||||
|
idx = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if idx == -1 {
|
||||||
|
manager.mu.Unlock()
|
||||||
|
return fmt.Errorf("queue not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
if idx == 0 {
|
||||||
|
manager.mu.Unlock()
|
||||||
|
return Skip(guildID)
|
||||||
|
}
|
||||||
|
|
||||||
|
gp.Queue = append(gp.Queue[:idx], gp.Queue[idx+1:]...)
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
syncState(guildID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Reorder(guildID, queueID string, newPosition int) error {
|
||||||
|
if manager == nil {
|
||||||
|
return fmt.Errorf("music manager not initialized")
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
idx := -1
|
||||||
|
for i, q := range gp.Queue {
|
||||||
|
if q.QueueID == queueID {
|
||||||
|
idx = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if idx == -1 {
|
||||||
|
manager.mu.Unlock()
|
||||||
|
return fmt.Errorf("queue not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
if idx == 0 {
|
||||||
|
manager.mu.Unlock()
|
||||||
|
return Skip(guildID)
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := gp.Queue[idx]
|
||||||
|
gp.Queue = append(gp.Queue[:idx], gp.Queue[idx+1:]...)
|
||||||
|
|
||||||
|
pos := max(1, min(newPosition, len(gp.Queue)))
|
||||||
|
gp.Queue = append(gp.Queue[:pos], append([]TrackEntry{entry}, gp.Queue[pos:]...)...)
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
syncState(guildID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CurrentNowPlayingMessageID(guildID string) string {
|
||||||
|
if manager == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
gp := getOrCreateGuildPlayer(guildID)
|
||||||
|
manager.mu.Lock()
|
||||||
|
defer manager.mu.Unlock()
|
||||||
|
return gp.NowPlayingMsgID
|
||||||
|
}
|
||||||
@@ -0,0 +1,179 @@
|
|||||||
|
package music
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/disgoorg/disgolink/v3/lavalink"
|
||||||
|
"github.com/disgoorg/snowflake/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func nowPlayingComponents(disabled bool, repeatSong bool, repeatQueue bool, paused bool) []discordgo.MessageComponent {
|
||||||
|
repeatSongStyle := discordgo.SecondaryButton
|
||||||
|
if repeatSong {
|
||||||
|
repeatSongStyle = discordgo.SuccessButton
|
||||||
|
}
|
||||||
|
repeatQueueStyle := discordgo.SecondaryButton
|
||||||
|
if repeatQueue {
|
||||||
|
repeatQueueStyle = discordgo.SuccessButton
|
||||||
|
}
|
||||||
|
|
||||||
|
return []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.Button{
|
||||||
|
Style: discordgo.SecondaryButton,
|
||||||
|
Emoji: &discordgo.ComponentEmoji{Name: map[bool]string{true: "▶️", false: "⏸️"}[paused]},
|
||||||
|
CustomID: "music:toggle_pause",
|
||||||
|
Disabled: disabled,
|
||||||
|
},
|
||||||
|
discordgo.Button{
|
||||||
|
Style: discordgo.SecondaryButton,
|
||||||
|
Emoji: &discordgo.ComponentEmoji{Name: "⏭️"},
|
||||||
|
CustomID: "music:skip",
|
||||||
|
Disabled: disabled,
|
||||||
|
},
|
||||||
|
discordgo.Button{
|
||||||
|
Style: repeatSongStyle,
|
||||||
|
Emoji: &discordgo.ComponentEmoji{Name: "🔂"},
|
||||||
|
CustomID: "music:repeat_song",
|
||||||
|
Disabled: disabled,
|
||||||
|
},
|
||||||
|
discordgo.Button{
|
||||||
|
Style: repeatQueueStyle,
|
||||||
|
Emoji: &discordgo.ComponentEmoji{Name: "🔁"},
|
||||||
|
CustomID: "music:repeat_queue",
|
||||||
|
Disabled: disabled,
|
||||||
|
},
|
||||||
|
discordgo.Button{
|
||||||
|
Style: discordgo.DangerButton,
|
||||||
|
Emoji: &discordgo.ComponentEmoji{Name: "✖️"},
|
||||||
|
CustomID: "music:stop",
|
||||||
|
Disabled: disabled,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func disableNowPlaying(textChannelID, messageID string) {
|
||||||
|
if manager == nil || textChannelID == "" || messageID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
disabled := nowPlayingComponents(true, false, false, false)
|
||||||
|
_, _ = manager.session.ChannelMessageEditComplex(&discordgo.MessageEdit{
|
||||||
|
Channel: textChannelID,
|
||||||
|
ID: messageID,
|
||||||
|
Components: &disabled,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func postNowPlaying(guildID, textChannelID string, entry TrackEntry) {
|
||||||
|
if manager == nil || textChannelID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
gp, ok := manager.players[guildID]
|
||||||
|
var oldMsgID string
|
||||||
|
if ok {
|
||||||
|
oldMsgID = gp.NowPlayingMsgID
|
||||||
|
}
|
||||||
|
manager.mu.Unlock()
|
||||||
|
|
||||||
|
if oldMsgID != "" {
|
||||||
|
disableNowPlaying(textChannelID, oldMsgID)
|
||||||
|
}
|
||||||
|
|
||||||
|
repeatSong, repeatQueue := repeatFlags(guildID)
|
||||||
|
paused, volume := pausedAndVolume(guildID)
|
||||||
|
|
||||||
|
info := entry.Track.Info
|
||||||
|
title := info.Title
|
||||||
|
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Author: &discordgo.MessageEmbedAuthor{
|
||||||
|
Name: info.Author,
|
||||||
|
IconURL: sourceIconURL(info.SourceName),
|
||||||
|
},
|
||||||
|
Title: title,
|
||||||
|
Color: 0x2B2D31, // matches Discord dark-ish embed accent
|
||||||
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
|
{Name: "Duration", Value: formatDuration(info.Length, info.IsStream), Inline: true},
|
||||||
|
{Name: "Volume", Value: fmt.Sprintf("%d%%", volume), Inline: true},
|
||||||
|
{Name: "Requested by", Value: entry.RequestedBy, Inline: true},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if info.URI != nil && *info.URI != "" {
|
||||||
|
embed.URL = *info.URI
|
||||||
|
}
|
||||||
|
|
||||||
|
if art := artworkURL(info); art != "" {
|
||||||
|
embed.Thumbnail = &discordgo.MessageEmbedThumbnail{URL: art}
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err := manager.session.ChannelMessageSendComplex(textChannelID, &discordgo.MessageSend{
|
||||||
|
Embeds: []*discordgo.MessageEmbed{embed},
|
||||||
|
Components: nowPlayingComponents(false, repeatSong, repeatQueue, paused),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.mu.Lock()
|
||||||
|
// Avoid calling getOrCreateGuildPlayer while holding manager.mu (it also locks).
|
||||||
|
gp = manager.players[guildID]
|
||||||
|
if gp == nil {
|
||||||
|
gp = &guildPlayer{
|
||||||
|
Player: manager.client.Player(snowflake.MustParse(guildID)),
|
||||||
|
Queue: make([]TrackEntry, 0),
|
||||||
|
}
|
||||||
|
manager.players[guildID] = gp
|
||||||
|
}
|
||||||
|
gp.TextChannelID = textChannelID
|
||||||
|
gp.NowPlayingMsgID = msg.ID
|
||||||
|
manager.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func artworkURL(info lavalink.TrackInfo) string {
|
||||||
|
if info.ArtworkURL != nil && *info.ArtworkURL != "" {
|
||||||
|
return *info.ArtworkURL
|
||||||
|
}
|
||||||
|
// Lavalink sets Identifier for YouTube videos; use standard thumbnail as fallback.
|
||||||
|
if strings.EqualFold(info.SourceName, "youtube") && info.Identifier != "" {
|
||||||
|
return "https://img.youtube.com/vi/" + info.Identifier + "/hqdefault.jpg"
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatDuration(d lavalink.Duration, isStream bool) string {
|
||||||
|
if isStream {
|
||||||
|
return "Live"
|
||||||
|
}
|
||||||
|
secs := d.Seconds()
|
||||||
|
if secs < 0 {
|
||||||
|
secs = 0
|
||||||
|
}
|
||||||
|
h := secs / 3600
|
||||||
|
m := (secs % 3600) / 60
|
||||||
|
s := secs % 60
|
||||||
|
if h > 0 {
|
||||||
|
return fmt.Sprintf("%d:%02d:%02d", h, m, s)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%d:%02d", m, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sourceIconURL(source string) string {
|
||||||
|
switch strings.ToLower(strings.TrimSpace(source)) {
|
||||||
|
case "youtube":
|
||||||
|
// Stable PNG favicon (Discord doesn't render .ico/.svg reliably in embeds)
|
||||||
|
return "https://www.google.com/s2/favicons?sz=64&domain=youtube.com"
|
||||||
|
case "soundcloud":
|
||||||
|
return "https://www.google.com/s2/favicons?sz=64&domain=soundcloud.com"
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package music
|
||||||
|
|
||||||
|
import "github.com/bwmarrin/discordgo"
|
||||||
|
|
||||||
|
func memberHasDJRoleForComponents(s *discordgo.Session, guildID string, m *discordgo.Member) bool {
|
||||||
|
if s == nil || guildID == "" || m == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if len(m.Roles) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
roles, err := s.GuildRoles(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
djRoleID := ""
|
||||||
|
for _, r := range roles {
|
||||||
|
if r != nil && r.Name == "DJ" {
|
||||||
|
djRoleID = r.ID
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if djRoleID == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rid := range m.Roles {
|
||||||
|
if rid == djRoleID {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package music
|
||||||
|
|
||||||
|
import "github.com/bwmarrin/discordgo"
|
||||||
|
|
||||||
|
func updateNowPlayingButtons(channelID, messageID string, guildID string) {
|
||||||
|
if manager == nil || channelID == "" || messageID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
repeatSong, repeatQueue := repeatFlags(guildID)
|
||||||
|
paused, _ := pausedAndVolume(guildID)
|
||||||
|
comps := nowPlayingComponents(false, repeatSong, repeatQueue, paused)
|
||||||
|
_, _ = manager.session.ChannelMessageEditComplex(&discordgo.MessageEdit{
|
||||||
|
Channel: channelID,
|
||||||
|
ID: messageID,
|
||||||
|
Components: &comps,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
colorAlert = 0xdc2626
|
||||||
|
colorWarn = 0xf59e0b
|
||||||
|
colorOK = 0x16a34a
|
||||||
|
)
|
||||||
|
|
||||||
|
type alert struct {
|
||||||
|
embed *discordgo.MessageEmbed
|
||||||
|
ping bool // ping the alert role (raids and nukes, not routine join gate actions)
|
||||||
|
pingOwner bool
|
||||||
|
// critical alerts also go by DM to the owner and trusted users, since
|
||||||
|
// the alert channel may be gone or locked in exactly these moments.
|
||||||
|
critical bool
|
||||||
|
components []discordgo.MessageComponent
|
||||||
|
file *discordgo.File
|
||||||
|
}
|
||||||
|
|
||||||
|
// send posts an alert to the configured channel. It only ever pings the
|
||||||
|
// configured alert role (never @everyone, even if that was saved somehow) and
|
||||||
|
// optionally the owner.
|
||||||
|
func (e *Engine) send(ctx context.Context, guildID string, s *securityrepo.Settings, a alert) *discordgo.Message {
|
||||||
|
if a.critical && s.AlertDM {
|
||||||
|
if e.syncDMs {
|
||||||
|
e.dmAlert(ctx, guildID, s, a)
|
||||||
|
} else {
|
||||||
|
go e.dmAlert(context.Background(), guildID, s, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if s.AlertChannelID == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
msg := &discordgo.MessageSend{
|
||||||
|
Embeds: []*discordgo.MessageEmbed{a.embed},
|
||||||
|
Components: a.components,
|
||||||
|
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
||||||
|
}
|
||||||
|
if a.ping && s.AlertRoleID != 0 && id(s.AlertRoleID) != guildID {
|
||||||
|
msg.Content = "<@&" + id(s.AlertRoleID) + ">"
|
||||||
|
msg.AllowedMentions.Roles = []string{id(s.AlertRoleID)}
|
||||||
|
}
|
||||||
|
if a.pingOwner {
|
||||||
|
if owner, err := e.OwnerID(guildID); err == nil && owner != "" {
|
||||||
|
msg.Content += " <@" + owner + ">"
|
||||||
|
msg.AllowedMentions.Users = []string{owner}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if a.file != nil {
|
||||||
|
msg.Files = []*discordgo.File{a.file}
|
||||||
|
}
|
||||||
|
m, err := e.d.ChannelMessageSendComplex(id(s.AlertChannelID), msg)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("security: alert in guild %s: %v", guildID, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func textFile(name, content string) *discordgo.File {
|
||||||
|
return &discordgo.File{Name: name, ContentType: "text/plain; charset=utf-8", Reader: bytes.NewReader([]byte(content))}
|
||||||
|
}
|
||||||
|
|
||||||
|
// maxAlertDMs caps how many people get each critical alert by DM. Mass DMs
|
||||||
|
// from a bot look like spam to Discord.
|
||||||
|
const maxAlertDMs = 10
|
||||||
|
|
||||||
|
// alertDMRecipients is the owner plus trusted users (not roles, which could
|
||||||
|
// hold many members), without duplicates or the bot itself.
|
||||||
|
func (e *Engine) alertDMRecipients(ctx context.Context, guildID string) []string {
|
||||||
|
seen := map[string]bool{e.botID: true}
|
||||||
|
var out []string
|
||||||
|
add := func(u string) {
|
||||||
|
if u != "" && !seen[u] && len(out) < maxAlertDMs {
|
||||||
|
seen[u] = true
|
||||||
|
out = append(out, u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if owner, err := e.OwnerID(guildID); err == nil {
|
||||||
|
add(owner)
|
||||||
|
}
|
||||||
|
if _, trusted, err := e.load(ctx, guildID); err == nil {
|
||||||
|
for _, t := range trusted {
|
||||||
|
if t.Kind == "user" {
|
||||||
|
add(id(t.TargetID))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// dmAlert sends a critical alert to each recipient's DMs. Closed DMs and
|
||||||
|
// other failures are skipped: the alert channel is still the main record.
|
||||||
|
func (e *Engine) dmAlert(ctx context.Context, guildID string, s *securityrepo.Settings, a alert) {
|
||||||
|
if a.embed == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
server := e.guildName(guildID)
|
||||||
|
if server == "" {
|
||||||
|
server = "a server"
|
||||||
|
}
|
||||||
|
embed := *a.embed
|
||||||
|
embed.Footer = &discordgo.MessageEmbedFooter{Text: clip("Velox security · "+server, 200)}
|
||||||
|
extra := ""
|
||||||
|
if a.file != nil {
|
||||||
|
if s.AlertChannelID != 0 {
|
||||||
|
extra = "\nThe full list is posted in the alert channel."
|
||||||
|
} else {
|
||||||
|
extra = "\nSet an alert channel on the dashboard to also get the full list of accounts."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if extra != "" {
|
||||||
|
embed.Description = clip(embed.Description+extra, 4000)
|
||||||
|
}
|
||||||
|
msg := &discordgo.MessageSend{
|
||||||
|
Content: "Security alert in **" + escapeText(server) + "**. Use the dashboard or `/security` in the server to act.",
|
||||||
|
Embeds: []*discordgo.MessageEmbed{&embed},
|
||||||
|
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
||||||
|
}
|
||||||
|
for _, u := range e.alertDMRecipients(ctx, guildID) {
|
||||||
|
ch, err := e.d.UserChannelCreate(u)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := e.d.ChannelMessageSendComplex(ch.ID, msg); err != nil {
|
||||||
|
log.Printf("security: DM alert to %s for guild %s: %v", u, guildID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// escapeText defuses markdown and mentions in a name (e.g. a server name).
|
||||||
|
func escapeText(s string) string {
|
||||||
|
return safeName(&discordgo.User{Username: s})
|
||||||
|
}
|
||||||
@@ -0,0 +1,207 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The configuration calls below do their own permission checks, so every
|
||||||
|
// way in (slash commands, buttons, anything added later) gets the same rules:
|
||||||
|
//
|
||||||
|
// - switching a system on: Manage Server, or owner/trusted
|
||||||
|
// - switching a system off, alert and quarantine settings: owner/trusted
|
||||||
|
// - the trusted list: owner only
|
||||||
|
//
|
||||||
|
// actorPerms is the actor's permission set as Discord sent it with the
|
||||||
|
// interaction.
|
||||||
|
|
||||||
|
const maxTrusted = 25
|
||||||
|
|
||||||
|
func hasManageGuild(perms int64) bool {
|
||||||
|
return perms&(discordgo.PermissionAdministrator|discordgo.PermissionManageGuild) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFeature turns the Join Gate, raid mode or anti-nuke on or off.
|
||||||
|
func (e *Engine) SetFeature(ctx context.Context, guildID string, actor *discordgo.Member, actorPerms int64, feature string, on bool) error {
|
||||||
|
if actor == nil || actor.User == nil {
|
||||||
|
return ErrNotTrusted
|
||||||
|
}
|
||||||
|
switch feature {
|
||||||
|
case securityrepo.FeatureJoinGate, securityrepo.FeatureAntiRaid, securityrepo.FeatureAntiNuke, securityrepo.FeatureHeat:
|
||||||
|
default:
|
||||||
|
return UserError("Unknown feature.")
|
||||||
|
}
|
||||||
|
trustedErr := e.RequireTrusted(ctx, guildID, actor)
|
||||||
|
if !on && trustedErr != nil {
|
||||||
|
return trustedErr
|
||||||
|
}
|
||||||
|
if on && trustedErr != nil && !hasManageGuild(actorPerms) {
|
||||||
|
return UserError("You need Manage Server to turn security features on.")
|
||||||
|
}
|
||||||
|
if err := e.st.SetEnabled(ctx, parseID(guildID), feature, on); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e.Invalidate(guildID)
|
||||||
|
state := "off"
|
||||||
|
if on {
|
||||||
|
state = "on"
|
||||||
|
}
|
||||||
|
e.event(ctx, guildID, "settings", actor.User.ID, "", feature+" "+state)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) roleByID(guildID, roleID string) (*discordgo.Role, []*discordgo.Role, error) {
|
||||||
|
roles, err := e.d.GuildRoles(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("listing roles: %w", err)
|
||||||
|
}
|
||||||
|
for _, r := range roles {
|
||||||
|
if r.ID == roleID {
|
||||||
|
return r, roles, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, roles, UserError("That role isn't in this server.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAlerts sets where alerts go and which role they ping. Changing it could
|
||||||
|
// hide alerts from the staff, so it needs owner or trusted.
|
||||||
|
func (e *Engine) SetAlerts(ctx context.Context, guildID string, actor *discordgo.Member, channelID, roleID string) error {
|
||||||
|
if err := e.RequireTrusted(ctx, guildID, actor); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ch, err := e.d.Channel(channelID)
|
||||||
|
if err != nil || ch.GuildID != guildID {
|
||||||
|
return UserError("That channel isn't in this server.")
|
||||||
|
}
|
||||||
|
if ch.Type != discordgo.ChannelTypeGuildText && ch.Type != discordgo.ChannelTypeGuildNews {
|
||||||
|
return UserError("Alerts need a text channel.")
|
||||||
|
}
|
||||||
|
if roleID != "" {
|
||||||
|
if roleID == guildID {
|
||||||
|
return UserError("Alerts can't ping @everyone. Pick a staff role.")
|
||||||
|
}
|
||||||
|
if _, _, err := e.roleByID(guildID, roleID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := e.st.SetAlerts(ctx, parseID(guildID), parseID(channelID), parseID(roleID)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e.Invalidate(guildID)
|
||||||
|
e.event(ctx, guildID, "settings", actor.User.ID, channelID, "alerts channel and role changed")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateQuarantineRole checks a role can safely be the quarantine role:
|
||||||
|
// in this server, not @everyone, not managed, no dangerous permissions, and
|
||||||
|
// below Velox's highest role so the bot can assign it.
|
||||||
|
func (e *Engine) ValidateQuarantineRole(guildID, roleID string) error {
|
||||||
|
if roleID == guildID {
|
||||||
|
return UserError("@everyone can't be the quarantine role.")
|
||||||
|
}
|
||||||
|
role, roles, err := e.roleByID(guildID, roleID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if role.Managed {
|
||||||
|
return UserError("That role is managed by an integration and can't be assigned.")
|
||||||
|
}
|
||||||
|
if role.Permissions&DangerousPerms != 0 {
|
||||||
|
return UserError("The quarantine role can't have Administrator, Manage Server, Manage Roles or Manage Webhooks.")
|
||||||
|
}
|
||||||
|
me, err := e.d.GuildMember(guildID, e.botID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("looking up Velox: %w", err)
|
||||||
|
}
|
||||||
|
top := -1
|
||||||
|
for _, r := range roles {
|
||||||
|
for _, mine := range me.Roles {
|
||||||
|
if r.ID == mine && r.Position > top {
|
||||||
|
top = r.Position
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if role.Position >= top {
|
||||||
|
return UserError("That role is above Velox's highest role, so Velox couldn't assign it. Move it below Velox's role.")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetQuarantineRole sets (or with "" clears) the quarantine role.
|
||||||
|
func (e *Engine) SetQuarantineRole(ctx context.Context, guildID string, actor *discordgo.Member, roleID string) error {
|
||||||
|
if err := e.RequireTrusted(ctx, guildID, actor); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if roleID != "" {
|
||||||
|
if err := e.ValidateQuarantineRole(guildID, roleID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := e.st.SetQuarantineRole(ctx, parseID(guildID), parseID(roleID)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e.Invalidate(guildID)
|
||||||
|
e.event(ctx, guildID, "settings", actor.User.ID, roleID, "quarantine role changed")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trust adds a user or role to the trusted list. Owner only: anyone trusted
|
||||||
|
// can switch protection off, so trust must not spread on its own.
|
||||||
|
func (e *Engine) Trust(ctx context.Context, guildID string, actor *discordgo.Member, kind, targetID string) error {
|
||||||
|
if actor == nil || actor.User == nil || !e.IsOwner(guildID, actor.User.ID) {
|
||||||
|
return ErrNotOwner
|
||||||
|
}
|
||||||
|
_, trusted, err := e.load(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(trusted) >= maxTrusted {
|
||||||
|
return UserError(fmt.Sprintf("The trusted list can hold at most %d entries.", maxTrusted))
|
||||||
|
}
|
||||||
|
switch kind {
|
||||||
|
case "role":
|
||||||
|
if targetID == guildID {
|
||||||
|
return UserError("Trusting @everyone would make everyone trusted.")
|
||||||
|
}
|
||||||
|
role, _, err := e.roleByID(guildID, targetID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if role.Managed {
|
||||||
|
return UserError("Integration roles can't be trusted; trust the bot user instead.")
|
||||||
|
}
|
||||||
|
case "user":
|
||||||
|
if _, err := e.d.GuildMember(guildID, targetID); err != nil {
|
||||||
|
return UserError("That user isn't in this server.")
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return UserError("Trust a user or a role.")
|
||||||
|
}
|
||||||
|
if err := e.st.AddTrusted(ctx, parseID(guildID), securityrepo.Trusted{TargetID: parseID(targetID), Kind: kind, AddedBy: parseID(actor.User.ID)}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e.Invalidate(guildID)
|
||||||
|
e.event(ctx, guildID, "trusted_added", actor.User.ID, targetID, kind)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Untrust removes a user or role from the trusted list. Owner only.
|
||||||
|
func (e *Engine) Untrust(ctx context.Context, guildID string, actor *discordgo.Member, targetID string) error {
|
||||||
|
if actor == nil || actor.User == nil || !e.IsOwner(guildID, actor.User.ID) {
|
||||||
|
return ErrNotOwner
|
||||||
|
}
|
||||||
|
ok, err := e.st.RemoveTrusted(ctx, parseID(guildID), parseID(targetID))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
return UserError("That isn't on the trusted list.")
|
||||||
|
}
|
||||||
|
e.Invalidate(guildID)
|
||||||
|
e.event(ctx, guildID, "trusted_removed", actor.User.ID, targetID, "")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func nuke(e *Engine) {
|
||||||
|
e.OnAuditLogEntry(ctx, guild, entry(discordgo.AuditLogActionMemberPrune, "50", ""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCriticalAlertsAreDMed(t *testing.T) {
|
||||||
|
e, d, st := nukeEngine(t)
|
||||||
|
d.guildName = "Velox **HQ** @everyone"
|
||||||
|
// Owner (1) is also on the list as a user; a trusted role must not be expanded.
|
||||||
|
st.trusted[100] = []securityrepo.Trusted{{TargetID: 1, Kind: "user"}, {TargetID: 60, Kind: "user"}, {TargetID: 201, Kind: "role"}, {TargetID: 999, Kind: "user"}}
|
||||||
|
refresh(e)
|
||||||
|
nuke(e)
|
||||||
|
|
||||||
|
got := strings.Join(d.dmsTo(), ",")
|
||||||
|
if got != "1,60" {
|
||||||
|
t.Fatalf("DMs went to %q, want the owner and the trusted user once each (no role members, not the bot)", got)
|
||||||
|
}
|
||||||
|
var dm *discordgo.MessageSend
|
||||||
|
for i, ch := range d.sentTo {
|
||||||
|
if ch == "dm-1" {
|
||||||
|
dm = d.sent[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !strings.Contains(dm.Embeds[0].Title, "Nuke attempt stopped") {
|
||||||
|
t.Errorf("DM should carry the alert: %+v", dm.Embeds[0])
|
||||||
|
}
|
||||||
|
if strings.Contains(dm.Content, "@everyone") || strings.Contains(dm.Content, "**HQ**") || dm.AllowedMentions == nil {
|
||||||
|
t.Errorf("the server name must be escaped and nothing pinged: %q", dm.Content)
|
||||||
|
}
|
||||||
|
if len(dm.Components) != 0 {
|
||||||
|
t.Error("no action buttons in DMs")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDMsWorkWithoutAnAlertChannel(t *testing.T) {
|
||||||
|
e, d, st := nukeEngine(t)
|
||||||
|
st.set(100).AlertChannelID = 0
|
||||||
|
refresh(e)
|
||||||
|
nuke(e)
|
||||||
|
if strings.Join(d.dmsTo(), ",") != "1" {
|
||||||
|
t.Errorf("the owner should still be told: %v", d.dmsTo())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDMsCanBeTurnedOff(t *testing.T) {
|
||||||
|
e, d, st := nukeEngine(t)
|
||||||
|
st.set(100).AlertDM = false
|
||||||
|
refresh(e)
|
||||||
|
nuke(e)
|
||||||
|
if len(d.dmsTo()) != 0 {
|
||||||
|
t.Errorf("DMs are off: %v", d.dmsTo())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRoutineAlertsAreNotDMed(t *testing.T) {
|
||||||
|
e, d, st, now := testEngine(t)
|
||||||
|
s := st.set(100)
|
||||||
|
s.JoinGateEnabled, s.NoAvatar = true, securityrepo.ActionKick
|
||||||
|
refresh(e)
|
||||||
|
e.OnMemberJoin(ctx, guild, &discordgo.Member{User: &discordgo.User{ID: snowflakeAt(now.AddDate(-1, 0, 0), 1)}})
|
||||||
|
if len(d.dmsTo()) != 0 {
|
||||||
|
t.Error("Join Gate actions only go to the alert channel")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClosedDMsAreSkippedAndListIsCapped(t *testing.T) {
|
||||||
|
e, d, st := nukeEngine(t)
|
||||||
|
d.closedDMs = map[string]bool{"1": true}
|
||||||
|
for i := 0; i < 15; i++ {
|
||||||
|
st.trusted[100] = append(st.trusted[100], securityrepo.Trusted{TargetID: int64(700 + i), Kind: "user"})
|
||||||
|
}
|
||||||
|
refresh(e)
|
||||||
|
nuke(e)
|
||||||
|
got := d.dmsTo()
|
||||||
|
if len(got) != maxAlertDMs-1 {
|
||||||
|
t.Errorf("owner's DMs are closed and the list is capped at %d: got %d (%v)", maxAlertDMs, len(got), got)
|
||||||
|
}
|
||||||
|
if got[0] != fmt.Sprint(700) {
|
||||||
|
t.Errorf("the others still get it: %v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,557 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"sort"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fakeStore keeps everything in memory with the same rules as the SQL.
|
||||||
|
type fakeStore struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
settings map[int64]*securityrepo.Settings
|
||||||
|
trusted map[int64][]securityrepo.Trusted
|
||||||
|
quarantines map[string]securityrepo.Quarantine
|
||||||
|
lockdowns map[string]securityrepo.Lockdown
|
||||||
|
events []securityrepo.Event
|
||||||
|
cmds []securityrepo.Command
|
||||||
|
done map[int64]bool
|
||||||
|
raids map[int64]securityrepo.Raid
|
||||||
|
caught map[int64][]securityrepo.Caught
|
||||||
|
snapshots []securityrepo.Snapshot
|
||||||
|
}
|
||||||
|
|
||||||
|
func newStore() *fakeStore {
|
||||||
|
return &fakeStore{
|
||||||
|
settings: map[int64]*securityrepo.Settings{}, trusted: map[int64][]securityrepo.Trusted{},
|
||||||
|
quarantines: map[string]securityrepo.Quarantine{}, lockdowns: map[string]securityrepo.Lockdown{}, done: map[int64]bool{},
|
||||||
|
raids: map[int64]securityrepo.Raid{}, caught: map[int64][]securityrepo.Caught{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeStore) SaveRaid(_ context.Context, x securityrepo.Raid) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if old, ok := f.raids[x.GuildID]; ok {
|
||||||
|
x.StartedAt = old.StartedAt
|
||||||
|
}
|
||||||
|
f.raids[x.GuildID] = x
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) ListRaids(context.Context) ([]securityrepo.Raid, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
var out []securityrepo.Raid
|
||||||
|
for _, x := range f.raids {
|
||||||
|
out = append(out, x)
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) DeleteRaid(_ context.Context, g int64) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
delete(f.raids, g)
|
||||||
|
delete(f.caught, g)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) AddRaidCaught(_ context.Context, g int64, c securityrepo.Caught) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
for _, x := range f.caught[g] {
|
||||||
|
if x.UserID == c.UserID {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.caught[g] = append(f.caught[g], c)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) ListRaidCaught(_ context.Context, g int64, limit int) ([]securityrepo.Caught, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
out := f.caught[g]
|
||||||
|
if len(out) > limit {
|
||||||
|
out = out[:limit]
|
||||||
|
}
|
||||||
|
return append([]securityrepo.Caught(nil), out...), nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) SaveSnapshot(_ context.Context, s securityrepo.Snapshot) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
s.ID = int64(len(f.snapshots) + 1)
|
||||||
|
s.CreatedAt = time.Date(2026, 10, 20, 6, 0, 0, 0, time.UTC)
|
||||||
|
f.snapshots = append(f.snapshots, s)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) LatestSnapshot(_ context.Context, g int64) (*securityrepo.Snapshot, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
for i := len(f.snapshots) - 1; i >= 0; i-- {
|
||||||
|
if f.snapshots[i].GuildID == g {
|
||||||
|
s := f.snapshots[i]
|
||||||
|
return &s, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) GuildsDueForSnapshot(context.Context, time.Duration) ([]int64, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func key(a, b int64) string { return fmt.Sprint(a, "/", b) }
|
||||||
|
|
||||||
|
func (f *fakeStore) set(g int64) *securityrepo.Settings {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
s, ok := f.settings[g]
|
||||||
|
if !ok {
|
||||||
|
s = securityrepo.Defaults(g)
|
||||||
|
s.Configured = true
|
||||||
|
f.settings[g] = s
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeStore) GetSettings(_ context.Context, g int64) (*securityrepo.Settings, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if s, ok := f.settings[g]; ok {
|
||||||
|
cp := *s
|
||||||
|
return &cp, nil
|
||||||
|
}
|
||||||
|
return securityrepo.Defaults(g), nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) SetEnabled(_ context.Context, g int64, feature string, on bool) error {
|
||||||
|
s := f.set(g)
|
||||||
|
switch feature {
|
||||||
|
case securityrepo.FeatureJoinGate:
|
||||||
|
s.JoinGateEnabled = on
|
||||||
|
case securityrepo.FeatureAntiRaid:
|
||||||
|
s.AntiRaidEnabled = on
|
||||||
|
case securityrepo.FeatureAntiNuke:
|
||||||
|
s.AntiNukeEnabled = on
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) SetAlerts(_ context.Context, g, ch, role int64) error {
|
||||||
|
s := f.set(g)
|
||||||
|
s.AlertChannelID, s.AlertRoleID = ch, role
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) SetQuarantineRole(_ context.Context, g, role int64) error {
|
||||||
|
f.set(g).QuarantineRoleID = role
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) SetJoinLock(_ context.Context, g int64, mode string) error {
|
||||||
|
f.set(g).JoinLock = mode
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) ListTrusted(_ context.Context, g int64) ([]securityrepo.Trusted, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
return append([]securityrepo.Trusted(nil), f.trusted[g]...), nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) AddTrusted(_ context.Context, g int64, t securityrepo.Trusted) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
f.trusted[g] = append(f.trusted[g], t)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) RemoveTrusted(_ context.Context, g, target int64) (bool, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
var keep []securityrepo.Trusted
|
||||||
|
found := false
|
||||||
|
for _, t := range f.trusted[g] {
|
||||||
|
if t.TargetID == target {
|
||||||
|
found = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
keep = append(keep, t)
|
||||||
|
}
|
||||||
|
f.trusted[g] = keep
|
||||||
|
return found, nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) SaveQuarantine(_ context.Context, q securityrepo.Quarantine) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if _, ok := f.quarantines[key(q.GuildID, q.UserID)]; !ok {
|
||||||
|
f.quarantines[key(q.GuildID, q.UserID)] = q
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) GetQuarantine(_ context.Context, g, u int64) (*securityrepo.Quarantine, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if q, ok := f.quarantines[key(g, u)]; ok {
|
||||||
|
return &q, nil
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) ListQuarantines(_ context.Context, g int64) ([]securityrepo.Quarantine, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
var out []securityrepo.Quarantine
|
||||||
|
for _, q := range f.quarantines {
|
||||||
|
if q.GuildID == g {
|
||||||
|
out = append(out, q)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) DeleteQuarantine(_ context.Context, g, u int64) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
delete(f.quarantines, key(g, u))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) SaveLockdown(_ context.Context, l securityrepo.Lockdown) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if _, ok := f.lockdowns[key(l.GuildID, l.ChannelID)]; !ok {
|
||||||
|
f.lockdowns[key(l.GuildID, l.ChannelID)] = l
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) GetLockdown(_ context.Context, g, ch int64) (*securityrepo.Lockdown, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if l, ok := f.lockdowns[key(g, ch)]; ok {
|
||||||
|
return &l, nil
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) ListLockdowns(_ context.Context, g int64, serverOnly bool) ([]securityrepo.Lockdown, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
var out []securityrepo.Lockdown
|
||||||
|
for _, l := range f.lockdowns {
|
||||||
|
if l.GuildID == g && (!serverOnly || l.ServerWide) {
|
||||||
|
out = append(out, l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Slice(out, func(i, j int) bool { return out[i].ChannelID < out[j].ChannelID })
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) DeleteLockdown(_ context.Context, g, ch int64) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
delete(f.lockdowns, key(g, ch))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) AddEvent(_ context.Context, e securityrepo.Event) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
f.events = append(f.events, e)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) eventKinds() []string {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
var out []string
|
||||||
|
for _, e := range f.events {
|
||||||
|
out = append(out, e.Kind)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
func (f *fakeStore) ListUnprocessedCommands(context.Context) ([]securityrepo.Command, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
var out []securityrepo.Command
|
||||||
|
for _, c := range f.cmds {
|
||||||
|
if !f.done[c.ID] {
|
||||||
|
out = append(out, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
func (f *fakeStore) MarkCommandProcessed(_ context.Context, id int64) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
f.done[id] = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fakeDiscord is a tiny in-memory guild.
|
||||||
|
type fakeDiscord struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
ownerID string
|
||||||
|
roles []*discordgo.Role
|
||||||
|
members map[string]*discordgo.Member
|
||||||
|
users map[string]*discordgo.User
|
||||||
|
channels map[string]*discordgo.Channel
|
||||||
|
kicked []string
|
||||||
|
banned []string
|
||||||
|
timeouts map[string]*time.Time
|
||||||
|
sent []*discordgo.MessageSend
|
||||||
|
sentTo []string
|
||||||
|
files map[string]string
|
||||||
|
requests []string // "METHOD url body"
|
||||||
|
deletedMsgs []string
|
||||||
|
nextID int
|
||||||
|
failEdits error
|
||||||
|
rejectRole string // edits that assign this role fail, like a role above Velox's
|
||||||
|
lookups int // member and role lookups, to check harmless entries cost nothing
|
||||||
|
closedDMs map[string]bool
|
||||||
|
guildName string
|
||||||
|
}
|
||||||
|
|
||||||
|
const guild = "100"
|
||||||
|
|
||||||
|
func newDiscord() *fakeDiscord {
|
||||||
|
return &fakeDiscord{
|
||||||
|
ownerID: "1",
|
||||||
|
roles: []*discordgo.Role{
|
||||||
|
{ID: guild, Name: "@everyone", Position: 0},
|
||||||
|
{ID: "200", Name: "Admin", Position: 5, Permissions: discordgo.PermissionAdministrator},
|
||||||
|
{ID: "201", Name: "Mod", Position: 3, Permissions: discordgo.PermissionKickMembers},
|
||||||
|
{ID: "202", Name: "Member", Position: 1},
|
||||||
|
{ID: "203", Name: "Quarantine", Position: 2},
|
||||||
|
{ID: "204", Name: "Booster", Position: 1, Managed: true},
|
||||||
|
{ID: "299", Name: "Velox", Position: 9, Managed: true, Permissions: discordgo.PermissionAdministrator},
|
||||||
|
},
|
||||||
|
members: map[string]*discordgo.Member{
|
||||||
|
"1": {User: &discordgo.User{ID: "1", Username: "owner"}},
|
||||||
|
"999": {User: &discordgo.User{ID: "999", Username: "velox", Bot: true}, Roles: []string{"299"}},
|
||||||
|
},
|
||||||
|
users: map[string]*discordgo.User{},
|
||||||
|
channels: map[string]*discordgo.Channel{},
|
||||||
|
timeouts: map[string]*time.Time{},
|
||||||
|
files: map[string]string{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeDiscord) addMember(id string, roles ...string) *discordgo.Member {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
m := &discordgo.Member{User: &discordgo.User{ID: id, Username: "user" + id, Avatar: "a"}, Roles: roles}
|
||||||
|
f.members[id] = m
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeDiscord) Guild(g string, _ ...discordgo.RequestOption) (*discordgo.Guild, error) {
|
||||||
|
return &discordgo.Guild{ID: g, OwnerID: f.ownerID, Name: f.guildName}, nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) UserChannelCreate(u string, _ ...discordgo.RequestOption) (*discordgo.Channel, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if f.closedDMs[u] {
|
||||||
|
return nil, errors.New("Cannot send messages to this user")
|
||||||
|
}
|
||||||
|
return &discordgo.Channel{ID: "dm-" + u, Type: discordgo.ChannelTypeDM}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// dmsTo lists who got a DM, in order.
|
||||||
|
func (f *fakeDiscord) dmsTo() []string {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
var out []string
|
||||||
|
for _, ch := range f.sentTo {
|
||||||
|
if len(ch) > 3 && ch[:3] == "dm-" {
|
||||||
|
out = append(out, ch[3:])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) User(id string, _ ...discordgo.RequestOption) (*discordgo.User, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if u, ok := f.users[id]; ok {
|
||||||
|
return u, nil
|
||||||
|
}
|
||||||
|
return nil, errors.New("unknown user")
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) GuildRoles(string, ...discordgo.RequestOption) ([]*discordgo.Role, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
f.lookups++
|
||||||
|
return f.roles, nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) GuildChannels(g string, _ ...discordgo.RequestOption) ([]*discordgo.Channel, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
var out []*discordgo.Channel
|
||||||
|
for _, c := range f.channels {
|
||||||
|
if c.GuildID == g {
|
||||||
|
out = append(out, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) GuildMember(_, u string, _ ...discordgo.RequestOption) (*discordgo.Member, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
f.lookups++
|
||||||
|
if m, ok := f.members[u]; ok {
|
||||||
|
cp := *m
|
||||||
|
cp.Roles = append([]string(nil), m.Roles...)
|
||||||
|
return &cp, nil
|
||||||
|
}
|
||||||
|
return nil, errors.New("unknown member")
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) GuildMemberEdit(_, u string, data *discordgo.GuildMemberParams, _ ...discordgo.RequestOption) (*discordgo.Member, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if f.failEdits != nil {
|
||||||
|
return nil, f.failEdits
|
||||||
|
}
|
||||||
|
if data.Roles != nil && f.rejectRole != "" {
|
||||||
|
for _, r := range *data.Roles {
|
||||||
|
if r == f.rejectRole {
|
||||||
|
return nil, errors.New("403 Missing Permissions")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m, ok := f.members[u]
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("unknown member")
|
||||||
|
}
|
||||||
|
if data.Roles != nil {
|
||||||
|
m.Roles = append([]string(nil), (*data.Roles)...)
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) GuildMemberTimeout(_, u string, until *time.Time, _ ...discordgo.RequestOption) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
f.timeouts[u] = until
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) GuildMemberDeleteWithReason(_, u, _ string, _ ...discordgo.RequestOption) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
f.kicked = append(f.kicked, u)
|
||||||
|
delete(f.members, u)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) GuildBanCreateWithReason(_, u, _ string, _ int, _ ...discordgo.RequestOption) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
f.banned = append(f.banned, u)
|
||||||
|
delete(f.members, u)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) Channel(id string, _ ...discordgo.RequestOption) (*discordgo.Channel, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if c, ok := f.channels[id]; ok {
|
||||||
|
cp := *c
|
||||||
|
cp.PermissionOverwrites = nil
|
||||||
|
for _, o := range c.PermissionOverwrites {
|
||||||
|
oc := *o
|
||||||
|
cp.PermissionOverwrites = append(cp.PermissionOverwrites, &oc)
|
||||||
|
}
|
||||||
|
return &cp, nil
|
||||||
|
}
|
||||||
|
return nil, errors.New("unknown channel")
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) ChannelPermissionSet(ch, target string, t discordgo.PermissionOverwriteType, allow, deny int64, _ ...discordgo.RequestOption) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
c := f.channels[ch]
|
||||||
|
for _, o := range c.PermissionOverwrites {
|
||||||
|
if o.ID == target {
|
||||||
|
o.Allow, o.Deny = allow, deny
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.PermissionOverwrites = append(c.PermissionOverwrites, &discordgo.PermissionOverwrite{ID: target, Type: t, Allow: allow, Deny: deny})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) ChannelPermissionDelete(ch, target string, _ ...discordgo.RequestOption) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
c := f.channels[ch]
|
||||||
|
var keep []*discordgo.PermissionOverwrite
|
||||||
|
for _, o := range c.PermissionOverwrites {
|
||||||
|
if o.ID != target {
|
||||||
|
keep = append(keep, o)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.PermissionOverwrites = keep
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) ChannelMessageSendComplex(ch string, data *discordgo.MessageSend, _ ...discordgo.RequestOption) (*discordgo.Message, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
f.sentTo = append(f.sentTo, ch)
|
||||||
|
for _, file := range data.Files {
|
||||||
|
raw, _ := io.ReadAll(file.Reader)
|
||||||
|
f.files[file.Name] = string(raw)
|
||||||
|
}
|
||||||
|
f.sent = append(f.sent, data)
|
||||||
|
return &discordgo.Message{ID: "m"}, nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) RequestWithBucketID(method, url string, data interface{}, _ string, _ ...discordgo.RequestOption) ([]byte, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
f.requests = append(f.requests, fmt.Sprintf("%s %s %v", method, url, data))
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// sentCount counts channel alerts (not DMs).
|
||||||
|
func (f *fakeDiscord) sentCount() int {
|
||||||
|
return len(f.channelSends())
|
||||||
|
}
|
||||||
|
|
||||||
|
// channelSends lists messages sent to channels, leaving out DMs.
|
||||||
|
func (f *fakeDiscord) channelSends() []*discordgo.MessageSend {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
var out []*discordgo.MessageSend
|
||||||
|
for i, m := range f.sent {
|
||||||
|
if i >= len(f.sentTo) || len(f.sentTo[i]) < 3 || f.sentTo[i][:3] != "dm-" {
|
||||||
|
out = append(out, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeDiscord) newID() string {
|
||||||
|
f.nextID++
|
||||||
|
return fmt.Sprint(8000 + f.nextID)
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) ChannelMessageDelete(ch, msg string, _ ...discordgo.RequestOption) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
f.deletedMsgs = append(f.deletedMsgs, msg)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) GuildRoleCreate(_ string, p *discordgo.RoleParams, _ ...discordgo.RequestOption) (*discordgo.Role, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
r := &discordgo.Role{ID: f.newID(), Name: p.Name}
|
||||||
|
if p.Permissions != nil {
|
||||||
|
r.Permissions = *p.Permissions
|
||||||
|
}
|
||||||
|
f.roles = append(f.roles, r)
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) GuildRoleEdit(_, roleID string, p *discordgo.RoleParams, _ ...discordgo.RequestOption) (*discordgo.Role, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
for _, r := range f.roles {
|
||||||
|
if r.ID == roleID {
|
||||||
|
if p.Permissions != nil {
|
||||||
|
r.Permissions = *p.Permissions
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, errors.New("unknown role")
|
||||||
|
}
|
||||||
|
func (f *fakeDiscord) GuildChannelCreateComplex(g string, d discordgo.GuildChannelCreateData, _ ...discordgo.RequestOption) (*discordgo.Channel, error) {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
c := &discordgo.Channel{ID: f.newID(), GuildID: g, Name: d.Name, Type: d.Type, ParentID: d.ParentID, PermissionOverwrites: d.PermissionOverwrites}
|
||||||
|
f.channels[c.ID] = c
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,328 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---- Raid persistence ----
|
||||||
|
|
||||||
|
func TestRaidSurvivesARestart(t *testing.T) {
|
||||||
|
e, d, st, now := testEngine(t)
|
||||||
|
s := st.set(100)
|
||||||
|
s.AntiRaidEnabled, s.RaidJoinCount, s.RaidAction, s.RaidQuietMinutes = true, 3, securityrepo.ActionKick, 5
|
||||||
|
refresh(e)
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
e.OnMemberJoin(ctx, guild, d.addMember(snowflakeAt(now.AddDate(-1, 0, 0), i)))
|
||||||
|
}
|
||||||
|
if r, ok := st.raids[100]; !ok || !r.InvitesPaused || r.CaughtTotal != 4 || len(st.caught[100]) != 4 {
|
||||||
|
t.Fatalf("raid not persisted: %+v caught=%d", st.raids[100], len(st.caught[100]))
|
||||||
|
}
|
||||||
|
|
||||||
|
// The bot restarts: a fresh engine on the same database.
|
||||||
|
e2 := New(d, st, "999")
|
||||||
|
e2.now = e.now
|
||||||
|
e2.ResumeRaids(ctx)
|
||||||
|
if !e2.RaidActive(guild) {
|
||||||
|
t.Fatal("raid mode must resume after a restart")
|
||||||
|
}
|
||||||
|
// A join after the restart is still treated as a raider.
|
||||||
|
*now = now.Add(time.Minute)
|
||||||
|
m := d.addMember(snowflakeAt(now.AddDate(-1, 0, 0), 50))
|
||||||
|
e2.OnMemberJoin(ctx, guild, m)
|
||||||
|
if !contains(d.kicked, m.User.ID) {
|
||||||
|
t.Error("raid actions continue after a restart")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quiet period: it ends, invites resume, the list covers both lifetimes.
|
||||||
|
*now = now.Add(6 * time.Minute)
|
||||||
|
e2.checkRaids(ctx)
|
||||||
|
if e2.RaidActive(guild) {
|
||||||
|
t.Fatal("the resumed raid should end after the quiet period")
|
||||||
|
}
|
||||||
|
if !strings.Contains(d.requests[len(d.requests)-1], "invites_disabled_until:<nil>") {
|
||||||
|
t.Error("invites paused before the restart must be resumed")
|
||||||
|
}
|
||||||
|
var list string
|
||||||
|
for name, content := range d.files {
|
||||||
|
if strings.HasPrefix(name, "raid-") {
|
||||||
|
list = content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if strings.Count(list, "\tkick\t") != 5 {
|
||||||
|
t.Errorf("summary should list all 5 raiders:\n%s", list)
|
||||||
|
}
|
||||||
|
if _, ok := st.raids[100]; ok {
|
||||||
|
t.Error("the raid record should be cleared")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Heat ----
|
||||||
|
|
||||||
|
func heatEngine(t *testing.T) (*Engine, *fakeDiscord, *fakeStore, *time.Time) {
|
||||||
|
e, d, st, now := testEngine(t)
|
||||||
|
s := st.set(100)
|
||||||
|
s.HeatEnabled, s.HeatPanicMembers = true, 0
|
||||||
|
refresh(e)
|
||||||
|
return e, d, st, now
|
||||||
|
}
|
||||||
|
|
||||||
|
var msgN int
|
||||||
|
|
||||||
|
func say(e *Engine, userID, content string, edit ...func(m *discordgo.MessageCreate)) *discordgo.MessageCreate {
|
||||||
|
msgN++
|
||||||
|
m := &discordgo.MessageCreate{Message: &discordgo.Message{
|
||||||
|
ID: "msg" + string(rune('A'+msgN%26)) + strings.Repeat("x", msgN/26), GuildID: guild, ChannelID: "300", Content: content,
|
||||||
|
Author: &discordgo.User{ID: userID, Username: "u" + userID},
|
||||||
|
Member: &discordgo.Member{Roles: []string{}, JoinedAt: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)},
|
||||||
|
}}
|
||||||
|
for _, f := range edit {
|
||||||
|
f(m)
|
||||||
|
}
|
||||||
|
e.OnMessage(ctx, m)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalChatNeverHeatsUp(t *testing.T) {
|
||||||
|
e, d, _, now := heatEngine(t)
|
||||||
|
for i := 0; i < 60; i++ {
|
||||||
|
say(e, "50", "just talking about the game tonight, number "+string(rune('a'+i%26)))
|
||||||
|
*now = now.Add(2 * time.Second)
|
||||||
|
}
|
||||||
|
if len(d.deletedMsgs) != 0 || len(d.timeouts) != 0 {
|
||||||
|
t.Error("a message every two seconds is normal conversation")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSpamIsPunishedAndEscalates(t *testing.T) {
|
||||||
|
e, d, st, now := heatEngine(t)
|
||||||
|
var last *discordgo.MessageCreate
|
||||||
|
for i := 0; i < 6 && d.timeouts["50"] == nil; i++ {
|
||||||
|
last = say(e, "50", "BUY CHEAP STUFF NOW")
|
||||||
|
*now = now.Add(200 * time.Millisecond)
|
||||||
|
}
|
||||||
|
first := d.timeouts["50"]
|
||||||
|
if first == nil || first.Sub(*now) > 11*time.Minute || first.Sub(*now) < 9*time.Minute {
|
||||||
|
t.Fatalf("repeated spam should earn the 10 minute timeout, got %v", first)
|
||||||
|
}
|
||||||
|
if !contains(d.deletedMsgs, last.ID) {
|
||||||
|
t.Error("the message that maxed the heat should be deleted")
|
||||||
|
}
|
||||||
|
if !contains(st.eventKinds(), securityrepo.EventHeat) {
|
||||||
|
t.Error("recorded")
|
||||||
|
}
|
||||||
|
|
||||||
|
// A second offence the same day doubles it.
|
||||||
|
*now = now.Add(time.Hour)
|
||||||
|
d.timeouts["50"] = nil
|
||||||
|
for i := 0; i < 6 && d.timeouts["50"] == nil; i++ {
|
||||||
|
say(e, "50", "BUY CHEAP STUFF NOW")
|
||||||
|
*now = now.Add(200 * time.Millisecond)
|
||||||
|
}
|
||||||
|
if second := d.timeouts["50"]; second == nil || second.Sub(*now) < 19*time.Minute {
|
||||||
|
t.Errorf("second offence should be 20 minutes, got %v", second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInviteLinksAndMassMentions(t *testing.T) {
|
||||||
|
e, d, _, _ := heatEngine(t)
|
||||||
|
say(e, "51", "join my server discord.gg/abcdef")
|
||||||
|
if d.timeouts["51"] == nil {
|
||||||
|
t.Error("an invite link is an instant max")
|
||||||
|
}
|
||||||
|
say(e, "52", "hey @everyone check this")
|
||||||
|
say(e, "52", "@everyone again")
|
||||||
|
if d.timeouts["52"] == nil {
|
||||||
|
t.Error("repeated @everyone should max out fast")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHeatExemptions(t *testing.T) {
|
||||||
|
e, d, st, _ := heatEngine(t)
|
||||||
|
st.trusted[100] = []securityrepo.Trusted{{TargetID: 60, Kind: "user"}}
|
||||||
|
refresh(e)
|
||||||
|
e.ChannelPerms = func(userID, _ string) (int64, error) {
|
||||||
|
if userID == "61" {
|
||||||
|
return discordgo.PermissionManageMessages, nil
|
||||||
|
}
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
for _, who := range []string{"1", "60", "61"} {
|
||||||
|
say(e, who, "discord.gg/x")
|
||||||
|
}
|
||||||
|
say(e, "62", "discord.gg/x", func(m *discordgo.MessageCreate) { m.Author.Bot = true })
|
||||||
|
say(e, "63", "discord.gg/x", func(m *discordgo.MessageCreate) { m.WebhookID = "w" })
|
||||||
|
if len(d.timeouts) != 0 || len(d.deletedMsgs) != 0 {
|
||||||
|
t.Errorf("owner, trusted, moderators, bots and webhooks are exempt: %v", d.timeouts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHeatPanicTimesOutNewMembers(t *testing.T) {
|
||||||
|
e, d, st, now := heatEngine(t)
|
||||||
|
st.set(100).HeatPanicMembers = 3
|
||||||
|
refresh(e)
|
||||||
|
for _, u := range []string{"70", "71", "72"} {
|
||||||
|
say(e, u, "discord.gg/spam")
|
||||||
|
}
|
||||||
|
if !contains(st.eventKinds(), securityrepo.EventHeatPanic) {
|
||||||
|
t.Fatal("three members maxing out within a minute starts heat panic")
|
||||||
|
}
|
||||||
|
newcomer := func(m *discordgo.MessageCreate) { m.Member.JoinedAt = now.Add(-time.Hour) }
|
||||||
|
say(e, "80", "hello everyone", newcomer)
|
||||||
|
if d.timeouts["80"] == nil {
|
||||||
|
t.Error("someone who joined an hour ago speaking during panic is timed out")
|
||||||
|
}
|
||||||
|
say(e, "81", "hello everyone")
|
||||||
|
if d.timeouts["81"] != nil {
|
||||||
|
t.Error("established members are unaffected by panic")
|
||||||
|
}
|
||||||
|
*now = now.Add(11 * time.Minute)
|
||||||
|
say(e, "82", "hello", newcomer)
|
||||||
|
if d.timeouts["82"] != nil {
|
||||||
|
t.Error("panic ends after 10 minutes")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMentionFloodLocksTheServer(t *testing.T) {
|
||||||
|
e, d, st, _ := heatEngine(t)
|
||||||
|
s := st.set(100)
|
||||||
|
s.HeatLockdownMentions, s.HeatStrictness = 20, "low"
|
||||||
|
refresh(e)
|
||||||
|
d.channels["300"] = &discordgo.Channel{ID: "300", GuildID: guild, Type: discordgo.ChannelTypeGuildText}
|
||||||
|
five := func(m *discordgo.MessageCreate) {
|
||||||
|
m.Mentions = []*discordgo.User{{ID: "a"}, {ID: "b"}, {ID: "c"}, {ID: "d"}, {ID: "e"}}
|
||||||
|
}
|
||||||
|
for i, u := range []string{"90", "91", "92", "93"} {
|
||||||
|
_ = i
|
||||||
|
say(e, u, "hi", five)
|
||||||
|
}
|
||||||
|
deadline := time.Now().Add(2 * time.Second)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
st.mu.Lock()
|
||||||
|
n := len(st.lockdowns)
|
||||||
|
st.mu.Unlock()
|
||||||
|
if n > 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(20 * time.Millisecond)
|
||||||
|
}
|
||||||
|
t.Error("20 mentions within 30 seconds should lock the server")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHeatAlertsAreCapped(t *testing.T) {
|
||||||
|
e, d, _, _ := heatEngine(t)
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
say(e, string(rune('A'+i)), "discord.gg/x")
|
||||||
|
}
|
||||||
|
if n := d.sentCount(); n > heatAlertsPerMin {
|
||||||
|
t.Errorf("a spam wave produced %d alerts, cap is %d", n, heatAlertsPerMin)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Snapshots and restore ----
|
||||||
|
|
||||||
|
func TestSnapshotAndRestore(t *testing.T) {
|
||||||
|
e, d, st, _ := testEngine(t)
|
||||||
|
d.channels["400"] = &discordgo.Channel{ID: "400", GuildID: guild, Type: discordgo.ChannelTypeGuildCategory, Name: "Staff"}
|
||||||
|
d.channels["401"] = &discordgo.Channel{ID: "401", GuildID: guild, Type: discordgo.ChannelTypeGuildText, Name: "mod-chat", ParentID: "400",
|
||||||
|
PermissionOverwrites: []*discordgo.PermissionOverwrite{
|
||||||
|
{ID: guild, Type: discordgo.PermissionOverwriteTypeRole, Deny: discordgo.PermissionViewChannel},
|
||||||
|
{ID: "201", Type: discordgo.PermissionOverwriteTypeRole, Allow: discordgo.PermissionViewChannel},
|
||||||
|
}}
|
||||||
|
d.channels["402"] = &discordgo.Channel{ID: "402", GuildID: guild, Type: discordgo.ChannelTypeGuildPublicThread, Name: "a thread"}
|
||||||
|
|
||||||
|
if _, err := e.Restore(ctx, guild, "1"); !errors.As(err, new(UserError)) {
|
||||||
|
t.Fatalf("no snapshot yet should be a clear message: %v", err)
|
||||||
|
}
|
||||||
|
roles, channels, err := e.TakeSnapshot(ctx, guild, "1")
|
||||||
|
if err != nil || roles != 5 || channels != 2 {
|
||||||
|
t.Fatalf("snapshot = %d roles, %d channels, %v (managed roles and threads are skipped)", roles, channels, err)
|
||||||
|
}
|
||||||
|
var data snapshotData
|
||||||
|
_ = json.Unmarshal(st.snapshots[0].Data, &data)
|
||||||
|
for _, r := range data.Roles {
|
||||||
|
if r.ID == "204" || r.ID == "299" {
|
||||||
|
t.Error("integration roles must not be in the snapshot")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The nuke: the Mod role and the whole Staff category are deleted, and
|
||||||
|
// the Member role is given Administrator.
|
||||||
|
var keep []*discordgo.Role
|
||||||
|
for _, r := range d.roles {
|
||||||
|
if r.ID != "201" {
|
||||||
|
keep = append(keep, r)
|
||||||
|
}
|
||||||
|
if r.ID == "202" {
|
||||||
|
r.Permissions |= discordgo.PermissionAdministrator
|
||||||
|
}
|
||||||
|
}
|
||||||
|
d.roles = keep
|
||||||
|
delete(d.channels, "400")
|
||||||
|
delete(d.channels, "401")
|
||||||
|
|
||||||
|
res, err := e.Restore(ctx, guild, "1")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if res.RolesCreated != 1 || res.RolesReverted != 1 || res.ChannelsCreated != 2 || res.Failed != 0 {
|
||||||
|
t.Fatalf("restore result = %+v", res)
|
||||||
|
}
|
||||||
|
var newMod, newCat, newText *string
|
||||||
|
for _, r := range d.roles {
|
||||||
|
if r.Name == "Mod" {
|
||||||
|
newMod = &r.ID
|
||||||
|
}
|
||||||
|
if r.ID == "202" && r.Permissions&discordgo.PermissionAdministrator != 0 {
|
||||||
|
t.Error("Administrator added to Member should be reverted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, c := range d.channels {
|
||||||
|
switch c.Name {
|
||||||
|
case "Staff":
|
||||||
|
newCat = &c.ID
|
||||||
|
case "mod-chat":
|
||||||
|
newText = &c.ID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if newMod == nil || newCat == nil || newText == nil {
|
||||||
|
t.Fatal("role or channels not recreated")
|
||||||
|
}
|
||||||
|
text := d.channels[*newText]
|
||||||
|
if text.ParentID != *newCat {
|
||||||
|
t.Error("the channel should go back into the recreated category")
|
||||||
|
}
|
||||||
|
foundMod := false
|
||||||
|
for _, o := range text.PermissionOverwrites {
|
||||||
|
if o.ID == *newMod && o.Allow == discordgo.PermissionViewChannel {
|
||||||
|
foundMod = true
|
||||||
|
}
|
||||||
|
if o.ID == "201" {
|
||||||
|
t.Error("overwrites must point at the recreated role, not the deleted one")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !foundMod {
|
||||||
|
t.Errorf("the Mod overwrite should be restored: %+v", text.PermissionOverwrites)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restoring again finds nothing missing.
|
||||||
|
res, _ = e.Restore(ctx, guild, "1")
|
||||||
|
if res.RolesCreated+res.ChannelsCreated+res.RolesReverted != 0 {
|
||||||
|
t.Errorf("a second restore should change nothing: %+v", res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOnlyOneRestoreAtATime(t *testing.T) {
|
||||||
|
e, _, _, _ := testEngine(t)
|
||||||
|
e.restoring[guild] = true
|
||||||
|
if _, err := e.Restore(ctx, guild, "1"); err == nil || !strings.Contains(err.Error(), "already running") {
|
||||||
|
t.Errorf("err = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,368 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Heat is a per-member spam score, like Wick's. Every message adds heat,
|
||||||
|
// more for what spam looks like; heat cools down over time, so normal
|
||||||
|
// conversation never builds up. At maxHeat the message is deleted and the
|
||||||
|
// configured action applied.
|
||||||
|
const (
|
||||||
|
maxHeat = 100.0
|
||||||
|
heatDecayPerSec = 8.0 // a message every couple of seconds never accumulates
|
||||||
|
heatBase = 8.0
|
||||||
|
heatRepeat = 25.0
|
||||||
|
heatMention = 10.0
|
||||||
|
heatEveryone = 60.0
|
||||||
|
heatEmoji = 2.0
|
||||||
|
heatLine = 2.0 // per line beyond the fourth
|
||||||
|
heatPer100Chars = 1.0
|
||||||
|
heatAttachment = 10.0
|
||||||
|
heatLink = 8.0
|
||||||
|
heatBlockedWord = 60.0
|
||||||
|
maxHeatUsers = 50000 // per guild, swept when exceeded
|
||||||
|
panicWindow = time.Minute
|
||||||
|
panicDuration = 10 * time.Minute
|
||||||
|
panicTimeout = time.Hour
|
||||||
|
panicNewMember = 24 * time.Hour
|
||||||
|
mentionWindow = 30 * time.Second
|
||||||
|
floodCooldown = 10 * time.Minute
|
||||||
|
heatAlertsPerMin = 5
|
||||||
|
maxTimeout = 28*24*time.Hour - time.Hour
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
linkRe = regexp.MustCompile(`https?://\S+`)
|
||||||
|
customEmojiRe = regexp.MustCompile(`<a?:\w+:\d+>`)
|
||||||
|
)
|
||||||
|
|
||||||
|
var strictness = map[string]float64{"low": 0.6, "medium": 1.0, "high": 1.5}
|
||||||
|
|
||||||
|
type memberHeat struct {
|
||||||
|
heat float64
|
||||||
|
last time.Time
|
||||||
|
recent []string // last few messages, normalized, for repeats
|
||||||
|
offences int // within the last day
|
||||||
|
lastOffence time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type guildHeat struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
members map[string]*memberHeat
|
||||||
|
maxedAt map[string]time.Time // who hit max heat recently, for panic
|
||||||
|
panicUntil time.Time
|
||||||
|
mentions []time.Time // mentions by non-exempt members, for flood lockdown
|
||||||
|
lastFlood time.Time
|
||||||
|
alertTimes []time.Time
|
||||||
|
alertsMuted int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) heatFor(guildID string) *guildHeat {
|
||||||
|
e.mu.Lock()
|
||||||
|
defer e.mu.Unlock()
|
||||||
|
g, ok := e.heat[guildID]
|
||||||
|
if !ok {
|
||||||
|
g = &guildHeat{members: map[string]*memberHeat{}, maxedAt: map[string]time.Time{}}
|
||||||
|
e.heat[guildID] = g
|
||||||
|
}
|
||||||
|
return g
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalize(s string) string {
|
||||||
|
return strings.Join(strings.Fields(strings.ToLower(s)), " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func countEmoji(s string) int {
|
||||||
|
n := len(customEmojiRe.FindAllString(s, -1))
|
||||||
|
for _, r := range s {
|
||||||
|
if r >= 0x1F000 || (r >= 0x2600 && r <= 0x27BF) {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// MessageHeat scores one message. recent is the author's last few messages
|
||||||
|
// (normalized) for spotting repeats. The second result is true when the
|
||||||
|
// message is an instant maximum (an invite link).
|
||||||
|
func MessageHeat(s *securityrepo.Settings, m *discordgo.Message, recent []string) (float64, bool, string) {
|
||||||
|
content := m.Content
|
||||||
|
lower := strings.ToLower(content)
|
||||||
|
if inviteRe.MatchString(lower) {
|
||||||
|
return maxHeat, true, "invite link"
|
||||||
|
}
|
||||||
|
reason := "spam"
|
||||||
|
h := heatBase
|
||||||
|
norm := normalize(content)
|
||||||
|
if norm != "" {
|
||||||
|
for _, r := range recent {
|
||||||
|
if r == norm {
|
||||||
|
h += heatRepeat
|
||||||
|
reason = "repeated messages"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mentions := len(m.Mentions) + len(m.MentionRoles)
|
||||||
|
h += float64(mentions) * heatMention
|
||||||
|
if m.MentionEveryone || strings.Contains(lower, "@everyone") || strings.Contains(lower, "@here") {
|
||||||
|
h += heatEveryone
|
||||||
|
reason = "mass mentions"
|
||||||
|
} else if mentions >= 3 {
|
||||||
|
reason = "mention spam"
|
||||||
|
}
|
||||||
|
h += float64(countEmoji(content)) * heatEmoji
|
||||||
|
if lines := strings.Count(content, "\n") + 1; lines > 4 {
|
||||||
|
h += float64(lines-4) * heatLine
|
||||||
|
}
|
||||||
|
h += float64(len([]rune(content))/100) * heatPer100Chars
|
||||||
|
h += float64(len(m.Attachments)+len(m.StickerItems)) * heatAttachment
|
||||||
|
h += float64(len(linkRe.FindAllString(content, -1))) * heatLink
|
||||||
|
for _, w := range s.HeatBlockedWords {
|
||||||
|
if w = strings.ToLower(strings.TrimSpace(w)); w != "" && strings.Contains(lower, w) {
|
||||||
|
h += heatBlockedWord
|
||||||
|
reason = "blocked word"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mult := strictness[s.HeatStrictness]
|
||||||
|
if mult == 0 {
|
||||||
|
mult = 1
|
||||||
|
}
|
||||||
|
return h * mult, false, reason
|
||||||
|
}
|
||||||
|
|
||||||
|
// heatExempt: bots, webhooks, the owner, trusted members and anyone who can
|
||||||
|
// manage messages in the channel.
|
||||||
|
func (e *Engine) heatExempt(ctx context.Context, m *discordgo.MessageCreate) bool {
|
||||||
|
if m.Author == nil || m.Author.Bot || m.WebhookID != "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
var roles []string
|
||||||
|
if m.Member != nil {
|
||||||
|
roles = m.Member.Roles
|
||||||
|
}
|
||||||
|
if e.IsTrusted(ctx, m.GuildID, m.Author.ID, nonNil(roles)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if e.ChannelPerms != nil {
|
||||||
|
if p, err := e.ChannelPerms(m.Author.ID, m.ChannelID); err == nil &&
|
||||||
|
p&(discordgo.PermissionAdministrator|discordgo.PermissionManageMessages) != 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnMessage runs the heat system on a guild message.
|
||||||
|
func (e *Engine) OnMessage(ctx context.Context, m *discordgo.MessageCreate) {
|
||||||
|
if m == nil || m.Message == nil || m.GuildID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s, _, err := e.load(ctx, m.GuildID)
|
||||||
|
if err != nil || !s.HeatEnabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if e.heatExempt(ctx, m) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
now := e.now()
|
||||||
|
g := e.heatFor(m.GuildID)
|
||||||
|
|
||||||
|
// Mention flood: lock the server once, then cool down.
|
||||||
|
if s.HeatLockdownMentions > 0 {
|
||||||
|
n := len(m.Mentions) + len(m.MentionRoles)
|
||||||
|
if m.MentionEveryone {
|
||||||
|
n += 10
|
||||||
|
}
|
||||||
|
if n > 0 && e.mentionFlood(g, now, n, s.HeatLockdownMentions) {
|
||||||
|
go e.floodLockdown(context.Background(), m.GuildID, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
g.mu.Lock()
|
||||||
|
if now.Before(g.panicUntil) && m.Member != nil && !m.Member.JoinedAt.IsZero() && now.Sub(m.Member.JoinedAt) < panicNewMember {
|
||||||
|
g.mu.Unlock()
|
||||||
|
e.heatPunish(ctx, m, s, "heat panic: new member speaking during a spam wave", panicTimeout, securityrepo.ActionTimeout)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mh, ok := g.members[m.Author.ID]
|
||||||
|
if !ok {
|
||||||
|
if len(g.members) >= maxHeatUsers {
|
||||||
|
g.sweep(now)
|
||||||
|
}
|
||||||
|
mh = &memberHeat{}
|
||||||
|
g.members[m.Author.ID] = mh
|
||||||
|
}
|
||||||
|
if !mh.last.IsZero() {
|
||||||
|
mh.heat = math.Max(0, mh.heat-now.Sub(mh.last).Seconds()*heatDecayPerSec)
|
||||||
|
}
|
||||||
|
mh.last = now
|
||||||
|
add, instant, reason := MessageHeat(s, m.Message, mh.recent)
|
||||||
|
mh.heat += add
|
||||||
|
if norm := normalize(m.Content); norm != "" {
|
||||||
|
mh.recent = append(mh.recent, norm)
|
||||||
|
if len(mh.recent) > 3 {
|
||||||
|
mh.recent = mh.recent[len(mh.recent)-3:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if mh.heat < maxHeat && !instant {
|
||||||
|
g.mu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Over the top: reset, count the offence, and work out the timeout.
|
||||||
|
mh.heat = 0
|
||||||
|
if now.Sub(mh.lastOffence) > 24*time.Hour {
|
||||||
|
mh.offences = 0
|
||||||
|
}
|
||||||
|
mh.offences++
|
||||||
|
mh.lastOffence = now
|
||||||
|
timeout := time.Duration(s.HeatTimeoutMinutes) * time.Minute
|
||||||
|
if s.HeatEscalate && mh.offences > 1 {
|
||||||
|
timeout *= time.Duration(1 << min(mh.offences-1, 12))
|
||||||
|
}
|
||||||
|
if timeout > maxTimeout {
|
||||||
|
timeout = maxTimeout
|
||||||
|
}
|
||||||
|
// Heat panic: enough different members maxing out within a minute.
|
||||||
|
g.maxedAt[m.Author.ID] = now
|
||||||
|
for u, t := range g.maxedAt {
|
||||||
|
if now.Sub(t) > panicWindow {
|
||||||
|
delete(g.maxedAt, u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
startPanic := s.HeatPanicMembers > 0 && len(g.maxedAt) >= s.HeatPanicMembers && !now.Before(g.panicUntil)
|
||||||
|
if startPanic {
|
||||||
|
g.panicUntil = now.Add(panicDuration)
|
||||||
|
}
|
||||||
|
g.mu.Unlock()
|
||||||
|
|
||||||
|
e.heatPunish(ctx, m, s, reason, timeout, s.HeatAction)
|
||||||
|
if startPanic {
|
||||||
|
e.event(ctx, m.GuildID, securityrepo.EventHeatPanic, e.botID, "", fmt.Sprintf("%d members hit max heat within a minute", s.HeatPanicMembers))
|
||||||
|
e.send(ctx, m.GuildID, s, alert{ping: true, critical: true, embed: &discordgo.MessageEmbed{
|
||||||
|
Title: "🔥 Spam wave: heat panic on",
|
||||||
|
Description: fmt.Sprintf("%d members hit max heat within a minute. For the next 10 minutes, anyone who joined in the last day is timed out as soon as they speak.", s.HeatPanicMembers),
|
||||||
|
Color: colorAlert,
|
||||||
|
}})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *guildHeat) sweep(now time.Time) {
|
||||||
|
for u, mh := range g.members {
|
||||||
|
cooled := mh.heat-now.Sub(mh.last).Seconds()*heatDecayPerSec <= 0
|
||||||
|
if cooled && now.Sub(mh.lastOffence) > 24*time.Hour {
|
||||||
|
delete(g.members, u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// mentionFlood records n mentions and reports whether the guild just went
|
||||||
|
// over the limit (once per cooldown).
|
||||||
|
func (e *Engine) mentionFlood(g *guildHeat, now time.Time, n, limit int) bool {
|
||||||
|
g.mu.Lock()
|
||||||
|
defer g.mu.Unlock()
|
||||||
|
for i := 0; i < n && len(g.mentions) < 5000; i++ {
|
||||||
|
g.mentions = append(g.mentions, now)
|
||||||
|
}
|
||||||
|
cut := 0
|
||||||
|
for cut < len(g.mentions) && now.Sub(g.mentions[cut]) > mentionWindow {
|
||||||
|
cut++
|
||||||
|
}
|
||||||
|
g.mentions = g.mentions[cut:]
|
||||||
|
if len(g.mentions) >= limit && now.Sub(g.lastFlood) > floodCooldown {
|
||||||
|
g.lastFlood = now
|
||||||
|
g.mentions = nil
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) floodLockdown(ctx context.Context, guildID string, s *securityrepo.Settings) {
|
||||||
|
locked, failed, err := e.LockServer(ctx, guildID, e.botID)
|
||||||
|
desc := fmt.Sprintf("%d or more mentions within 30 seconds: every channel is locked (%d locked", s.HeatLockdownMentions, locked)
|
||||||
|
if failed > 0 {
|
||||||
|
desc += fmt.Sprintf(", %d failed", failed)
|
||||||
|
}
|
||||||
|
desc += "). Unlock with `/unlock server` once it's under control."
|
||||||
|
if err != nil {
|
||||||
|
desc = "A mention flood was detected but locking the server failed: " + clip(err.Error(), 200)
|
||||||
|
}
|
||||||
|
e.send(ctx, guildID, s, alert{ping: true, critical: true, embed: &discordgo.MessageEmbed{Title: "🔒 Mention flood: server locked", Description: desc, Color: colorAlert}})
|
||||||
|
}
|
||||||
|
|
||||||
|
// heatPunish deletes the message and applies the action. Alerts are capped
|
||||||
|
// per minute so a spam wave can't flood the alert channel.
|
||||||
|
func (e *Engine) heatPunish(ctx context.Context, m *discordgo.MessageCreate, s *securityrepo.Settings, reason string, timeout time.Duration, action string) {
|
||||||
|
if err := e.d.ChannelMessageDelete(m.ChannelID, m.ID, discordgo.WithAuditLogReason("Velox heat: "+reason)); err != nil {
|
||||||
|
log.Printf("security: deleting a hot message in guild %s: %v", m.GuildID, err)
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
if action == securityrepo.ActionTimeout {
|
||||||
|
if m.Author.ID == e.botID || e.IsOwner(m.GuildID, m.Author.ID) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
until := e.now().Add(timeout)
|
||||||
|
err = e.d.GuildMemberTimeout(m.GuildID, m.Author.ID, &until, discordgo.WithAuditLogReason(clip("Velox heat: "+reason, 400)))
|
||||||
|
} else {
|
||||||
|
err = e.punish(m.GuildID, m.Author, action, "heat: "+reason)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("security: heat action on %s in guild %s: %v", m.Author.ID, m.GuildID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
what := action
|
||||||
|
if action == securityrepo.ActionTimeout {
|
||||||
|
what = "timed out for " + humanize(timeout)
|
||||||
|
}
|
||||||
|
e.event(ctx, m.GuildID, securityrepo.EventHeat, e.botID, m.Author.ID, what+": "+reason)
|
||||||
|
|
||||||
|
g := e.heatFor(m.GuildID)
|
||||||
|
now := e.now()
|
||||||
|
g.mu.Lock()
|
||||||
|
cut := 0
|
||||||
|
for cut < len(g.alertTimes) && now.Sub(g.alertTimes[cut]) > time.Minute {
|
||||||
|
cut++
|
||||||
|
}
|
||||||
|
g.alertTimes = g.alertTimes[cut:]
|
||||||
|
allowed := len(g.alertTimes) < heatAlertsPerMin
|
||||||
|
muted := 0
|
||||||
|
if allowed {
|
||||||
|
g.alertTimes = append(g.alertTimes, now)
|
||||||
|
muted, g.alertsMuted = g.alertsMuted, 0
|
||||||
|
} else {
|
||||||
|
g.alertsMuted++
|
||||||
|
}
|
||||||
|
g.mu.Unlock()
|
||||||
|
if !allowed {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
desc := fmt.Sprintf("<@%s> (%s) was %s: %s. Their message was deleted.", m.Author.ID, safeName(m.Author), what, reason)
|
||||||
|
if muted > 0 {
|
||||||
|
desc += fmt.Sprintf("\n(%d more since the last alert weren't posted, to keep this channel readable.)", muted)
|
||||||
|
}
|
||||||
|
e.send(ctx, m.GuildID, s, alert{embed: &discordgo.MessageEmbed{Title: "Heat", Description: desc, Color: colorWarn}})
|
||||||
|
}
|
||||||
|
|
||||||
|
func humanize(d time.Duration) string {
|
||||||
|
switch {
|
||||||
|
case d >= 24*time.Hour:
|
||||||
|
return fmt.Sprintf("%.0f days", d.Hours()/24)
|
||||||
|
case d >= time.Hour:
|
||||||
|
return fmt.Sprintf("%.0f hours", d.Hours())
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%.0f minutes", math.Max(1, d.Minutes()))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,176 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Invite links in a name: discord.gg/x, discord.com/invite/x, dsc.gg/x and
|
||||||
|
// the common lookalikes. Matched case-insensitively against the lowercased
|
||||||
|
// name; the pattern is fixed, never built from user input.
|
||||||
|
var inviteRe = regexp.MustCompile(`(discord(app)?\.(gg|com/invite|io|me|li)|dsc\.gg|\.gg/)`)
|
||||||
|
|
||||||
|
var severity = map[string]int{
|
||||||
|
securityrepo.ActionOff: 0, securityrepo.ActionAlert: 1, securityrepo.ActionTimeout: 2,
|
||||||
|
securityrepo.ActionKick: 3, securityrepo.ActionBan: 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
// GateResult is what the Join Gate decided about one member.
|
||||||
|
type GateResult struct {
|
||||||
|
Action string // off, timeout, kick, ban
|
||||||
|
Reason string
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckJoin runs the Join Gate filters on a new member. When several match,
|
||||||
|
// the strictest action wins. Bots are only checked for being unverified;
|
||||||
|
// bots added by untrusted members are handled from the audit log.
|
||||||
|
func CheckJoin(s *securityrepo.Settings, u *discordgo.User, now time.Time) GateResult {
|
||||||
|
var res GateResult
|
||||||
|
consider := func(action, reason string) {
|
||||||
|
if severity[action] > severity[res.Action] {
|
||||||
|
res = GateResult{Action: action, Reason: reason}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.Action = securityrepo.ActionOff
|
||||||
|
if !s.JoinGateEnabled || u == nil {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
if u.Bot {
|
||||||
|
if u.PublicFlags&discordgo.UserFlagVerifiedBot == 0 {
|
||||||
|
consider(s.UnverifiedBot, "unverified bot")
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
names := strings.ToLower(u.Username + " " + u.GlobalName)
|
||||||
|
if inviteRe.MatchString(names) {
|
||||||
|
consider(s.AdvertisingName, "invite link in name")
|
||||||
|
}
|
||||||
|
if s.UsernameFilter != securityrepo.ActionOff {
|
||||||
|
for _, p := range s.UsernamePatterns {
|
||||||
|
p = strings.ToLower(strings.TrimSpace(p))
|
||||||
|
if p != "" && strings.Contains(names, p) {
|
||||||
|
consider(s.UsernameFilter, "blocked word in name")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if created, err := discordgo.SnowflakeTimestamp(u.ID); err == nil {
|
||||||
|
if now.Sub(created) < time.Duration(s.MinAccountAgeDays)*24*time.Hour {
|
||||||
|
consider(s.NewAccount, fmt.Sprintf("account younger than %d days", s.MinAccountAgeDays))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if u.Avatar == "" {
|
||||||
|
consider(s.NoAvatar, "no avatar")
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// errProtected is returned when asked to punish the owner or the bot.
|
||||||
|
var errProtected = errors.New("the owner and the bot are never punished")
|
||||||
|
|
||||||
|
// punish applies a member action. It never touches the owner or the bot.
|
||||||
|
func (e *Engine) punish(guildID string, u *discordgo.User, action, reason string) error {
|
||||||
|
if u == nil || u.ID == e.botID || e.IsOwner(guildID, u.ID) {
|
||||||
|
return errProtected
|
||||||
|
}
|
||||||
|
reason = clip("Velox security: "+reason, 400)
|
||||||
|
switch action {
|
||||||
|
case securityrepo.ActionTimeout:
|
||||||
|
until := e.now().Add(timeoutJoin)
|
||||||
|
return e.d.GuildMemberTimeout(guildID, u.ID, &until, discordgo.WithAuditLogReason(reason))
|
||||||
|
case securityrepo.ActionKick:
|
||||||
|
return e.d.GuildMemberDeleteWithReason(guildID, u.ID, reason)
|
||||||
|
case securityrepo.ActionBan:
|
||||||
|
return e.d.GuildBanCreateWithReason(guildID, u.ID, reason, 1)
|
||||||
|
}
|
||||||
|
return errors.New("no action " + action)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnMemberJoin runs every check for a new member: the join lock, the Join
|
||||||
|
// Gate and raid detection. It reports whether the member was removed, so
|
||||||
|
// welcome messages and default roles can be skipped.
|
||||||
|
func (e *Engine) OnMemberJoin(ctx context.Context, guildID string, m *discordgo.Member) (removed bool) {
|
||||||
|
if m == nil || m.User == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
s, _, err := e.load(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("security: settings for guild %s: %v", guildID, err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Join lockdown: everyone new is kicked or banned.
|
||||||
|
if s.JoinLock == securityrepo.ActionKick || s.JoinLock == securityrepo.ActionBan {
|
||||||
|
if err := e.punish(guildID, m.User, s.JoinLock, "joins are locked"); err != nil {
|
||||||
|
if !errors.Is(err, errProtected) {
|
||||||
|
log.Printf("security: join lock in guild %s: %v", guildID, err)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
e.raidJoin(ctx, guildID, s, m, true)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Join Gate. A bot's verified flag isn't always in the join event, so
|
||||||
|
// it's read from Discord; if that fails the bot is left alone rather than
|
||||||
|
// risk kicking a verified one.
|
||||||
|
u := m.User
|
||||||
|
if u.Bot && s.JoinGateEnabled && s.UnverifiedBot != securityrepo.ActionOff {
|
||||||
|
fresh, err := e.d.User(u.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("security: looking up bot %s: %v", u.ID, err)
|
||||||
|
a := e.raidJoin(ctx, guildID, s, m, false)
|
||||||
|
return a == securityrepo.ActionKick || a == securityrepo.ActionBan
|
||||||
|
}
|
||||||
|
u = fresh
|
||||||
|
}
|
||||||
|
gate := CheckJoin(s, u, e.now())
|
||||||
|
if gate.Action != securityrepo.ActionOff {
|
||||||
|
if err := e.punish(guildID, m.User, gate.Action, gate.Reason); err != nil {
|
||||||
|
if !errors.Is(err, errProtected) {
|
||||||
|
log.Printf("security: join gate in guild %s: %v", guildID, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
removed = gate.Action == securityrepo.ActionKick || gate.Action == securityrepo.ActionBan
|
||||||
|
// During a raid the summary lists everyone; one alert per
|
||||||
|
// raider would flood the channel and Discord's rate limit.
|
||||||
|
if !e.RaidActive(guildID) {
|
||||||
|
e.event(ctx, guildID, securityrepo.EventJoinGate, e.botID, m.User.ID, gate.Action+": "+gate.Reason)
|
||||||
|
e.send(ctx, guildID, s, alert{embed: &discordgo.MessageEmbed{
|
||||||
|
Title: "Join Gate",
|
||||||
|
Description: fmt.Sprintf("%s <@%s> (%s): %s.", actionPast(gate.Action), m.User.ID, safeName(m.User), gate.Reason),
|
||||||
|
Color: colorWarn,
|
||||||
|
}})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Raid detection, which counts every join, gated or not.
|
||||||
|
if a := e.raidJoin(ctx, guildID, s, m, removed); a == securityrepo.ActionKick || a == securityrepo.ActionBan {
|
||||||
|
removed = true
|
||||||
|
}
|
||||||
|
return removed
|
||||||
|
}
|
||||||
|
|
||||||
|
func actionPast(a string) string {
|
||||||
|
switch a {
|
||||||
|
case securityrepo.ActionTimeout:
|
||||||
|
return "Timed out"
|
||||||
|
case securityrepo.ActionKick:
|
||||||
|
return "Kicked"
|
||||||
|
case securityrepo.ActionBan:
|
||||||
|
return "Banned"
|
||||||
|
}
|
||||||
|
return "Flagged"
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StartCommandListener carries out the dashboard's requests
|
||||||
|
// (security_commands rows announced with NOTIFY). The dashboard API checks
|
||||||
|
// that the requester is the owner or trusted before inserting; the payload
|
||||||
|
// is still validated here.
|
||||||
|
func (e *Engine) StartCommandListener(ctx context.Context, connString string) error {
|
||||||
|
conn, err := pgx.Connect(ctx, connString)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("connect: %w", err)
|
||||||
|
}
|
||||||
|
defer conn.Close(ctx)
|
||||||
|
if _, err := conn.Exec(ctx, "LISTEN security_commands"); err != nil {
|
||||||
|
return fmt.Errorf("listen: %w", err)
|
||||||
|
}
|
||||||
|
e.ProcessCommands(ctx)
|
||||||
|
for {
|
||||||
|
if _, err := conn.WaitForNotification(ctx); err != nil {
|
||||||
|
return fmt.Errorf("waiting for notifications: %w", err)
|
||||||
|
}
|
||||||
|
e.ProcessCommands(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProcessCommands runs every pending command once and marks it done, even
|
||||||
|
// when it fails (the error is logged; retrying would only fail again).
|
||||||
|
func (e *Engine) ProcessCommands(ctx context.Context) {
|
||||||
|
cmds, err := e.st.ListUnprocessedCommands(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("security: listing dashboard commands: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, c := range cmds {
|
||||||
|
if err := e.runCommand(ctx, c); err != nil {
|
||||||
|
log.Printf("security: dashboard command %d (%s) for guild %d: %v", c.ID, c.CommandType, c.GuildID, err)
|
||||||
|
}
|
||||||
|
if err := e.st.MarkCommandProcessed(ctx, c.ID); err != nil {
|
||||||
|
log.Printf("security: marking command %d done: %v", c.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) runCommand(ctx context.Context, c securityrepo.Command) error {
|
||||||
|
guildID, by := id(c.GuildID), id(c.RequestedBy)
|
||||||
|
switch c.CommandType {
|
||||||
|
case "end_raid":
|
||||||
|
return e.EndRaid(ctx, guildID, by)
|
||||||
|
case "unlock_server":
|
||||||
|
_, _, err := e.UnlockServer(ctx, guildID, by)
|
||||||
|
return err
|
||||||
|
case "unlock_joins":
|
||||||
|
return e.SetJoinLock(ctx, guildID, securityrepo.ActionOff, by)
|
||||||
|
case "snapshot":
|
||||||
|
_, _, err := e.TakeSnapshot(ctx, guildID, by)
|
||||||
|
return err
|
||||||
|
case "restore":
|
||||||
|
// Recreating many channels takes minutes under Discord's rate
|
||||||
|
// limits; don't hold up the other commands meanwhile.
|
||||||
|
go func() {
|
||||||
|
if _, err := e.Restore(context.Background(), guildID, by); err != nil {
|
||||||
|
log.Printf("security: restore for guild %s from the dashboard: %v", guildID, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
|
case "release_quarantine":
|
||||||
|
var p struct {
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(c.Payload, &p); err != nil {
|
||||||
|
return fmt.Errorf("bad payload: %w", err)
|
||||||
|
}
|
||||||
|
if _, err := strconv.ParseUint(p.UserID, 10, 64); err != nil {
|
||||||
|
return fmt.Errorf("bad user id %q", p.UserID)
|
||||||
|
}
|
||||||
|
return e.Release(ctx, guildID, p.UserID, by)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("unknown command %q", c.CommandType)
|
||||||
|
}
|
||||||
@@ -0,0 +1,166 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LockBits are what a lockdown denies @everyone: talking, threads, reactions.
|
||||||
|
const LockBits = discordgo.PermissionSendMessages | discordgo.PermissionSendMessagesInThreads |
|
||||||
|
discordgo.PermissionCreatePublicThreads | discordgo.PermissionCreatePrivateThreads | discordgo.PermissionAddReactions
|
||||||
|
|
||||||
|
// lockable reports whether a channel type has a chat to lock.
|
||||||
|
func lockable(t discordgo.ChannelType) bool {
|
||||||
|
switch t {
|
||||||
|
case discordgo.ChannelTypeGuildText, discordgo.ChannelTypeGuildNews, discordgo.ChannelTypeGuildForum,
|
||||||
|
discordgo.ChannelTypeGuildVoice, discordgo.ChannelTypeGuildStageVoice, discordgo.ChannelTypeGuildMedia:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func everyoneOverwrite(ch *discordgo.Channel, guildID string) (allow, deny int64, found bool) {
|
||||||
|
for _, o := range ch.PermissionOverwrites {
|
||||||
|
if o.ID == guildID && o.Type == discordgo.PermissionOverwriteTypeRole {
|
||||||
|
return o.Allow, o.Deny, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// LockChannel stops @everyone from talking in a channel, remembering the
|
||||||
|
// overwrite it had so UnlockChannel can put it back.
|
||||||
|
func (e *Engine) LockChannel(ctx context.Context, guildID, channelID, by string, serverWide bool) error {
|
||||||
|
ch, err := e.d.Channel(channelID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("looking up the channel: %w", err)
|
||||||
|
}
|
||||||
|
// Never act on a channel of another server, whatever ID was passed in.
|
||||||
|
if ch.GuildID != guildID {
|
||||||
|
return UserError("That channel isn't in this server.")
|
||||||
|
}
|
||||||
|
if !lockable(ch.Type) {
|
||||||
|
return UserError("That channel has no chat to lock.")
|
||||||
|
}
|
||||||
|
allow, deny, had := everyoneOverwrite(ch, guildID)
|
||||||
|
if err := e.st.SaveLockdown(ctx, securityrepo.Lockdown{
|
||||||
|
GuildID: parseID(guildID), ChannelID: parseID(channelID), HadOverwrite: had,
|
||||||
|
PreviousAllow: allow, PreviousDeny: deny, ServerWide: serverWide, LockedBy: parseID(by),
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := e.d.ChannelPermissionSet(channelID, guildID, discordgo.PermissionOverwriteTypeRole, allow&^LockBits, deny|LockBits,
|
||||||
|
discordgo.WithAuditLogReason("Velox lockdown")); err != nil {
|
||||||
|
_ = e.st.DeleteLockdown(ctx, parseID(guildID), parseID(channelID))
|
||||||
|
return fmt.Errorf("locking: %w", err)
|
||||||
|
}
|
||||||
|
if !serverWide {
|
||||||
|
e.event(ctx, guildID, securityrepo.EventLocked, by, channelID, "channel")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnlockChannel restores the lock bits of @everyone's overwrite to what they
|
||||||
|
// were before. Anything else changed in the overwrite during the lockdown is
|
||||||
|
// kept. Unlocking relaxes protection, so callers must check the actor first.
|
||||||
|
func (e *Engine) UnlockChannel(ctx context.Context, guildID, channelID, by string) error {
|
||||||
|
rec, err := e.st.GetLockdown(ctx, parseID(guildID), parseID(channelID))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if rec == nil {
|
||||||
|
return UserError("That channel isn't locked by Velox.")
|
||||||
|
}
|
||||||
|
ch, err := e.d.Channel(channelID)
|
||||||
|
if err != nil {
|
||||||
|
// The channel is gone: nothing to unlock.
|
||||||
|
return e.st.DeleteLockdown(ctx, rec.GuildID, rec.ChannelID)
|
||||||
|
}
|
||||||
|
allow, deny, _ := everyoneOverwrite(ch, guildID)
|
||||||
|
allow = allow&^LockBits | rec.PreviousAllow&LockBits
|
||||||
|
deny = deny&^LockBits | rec.PreviousDeny&LockBits
|
||||||
|
|
||||||
|
reason := discordgo.WithAuditLogReason("Velox unlock")
|
||||||
|
if allow == 0 && deny == 0 && !rec.HadOverwrite {
|
||||||
|
err = e.d.ChannelPermissionDelete(channelID, guildID, reason)
|
||||||
|
} else {
|
||||||
|
err = e.d.ChannelPermissionSet(channelID, guildID, discordgo.PermissionOverwriteTypeRole, allow, deny, reason)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unlocking: %w", err)
|
||||||
|
}
|
||||||
|
if err := e.st.DeleteLockdown(ctx, rec.GuildID, rec.ChannelID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !rec.ServerWide {
|
||||||
|
e.event(ctx, guildID, securityrepo.EventUnlocked, by, channelID, "channel")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LockServer locks every channel with a chat.
|
||||||
|
func (e *Engine) LockServer(ctx context.Context, guildID, by string) (locked, failed int, err error) {
|
||||||
|
chans, err := e.d.GuildChannels(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, fmt.Errorf("listing channels: %w", err)
|
||||||
|
}
|
||||||
|
for _, ch := range chans {
|
||||||
|
if !lockable(ch.Type) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := e.LockChannel(ctx, guildID, ch.ID, by, true); err != nil {
|
||||||
|
log.Printf("security: locking channel %s in guild %s: %v", ch.ID, guildID, err)
|
||||||
|
failed++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
locked++
|
||||||
|
}
|
||||||
|
e.event(ctx, guildID, securityrepo.EventLocked, by, "", fmt.Sprintf("server: %d channels locked, %d failed", locked, failed))
|
||||||
|
return locked, failed, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnlockServer unlocks every channel the last server lockdown locked.
|
||||||
|
func (e *Engine) UnlockServer(ctx context.Context, guildID, by string) (unlocked, failed int, err error) {
|
||||||
|
recs, err := e.st.ListLockdowns(ctx, parseID(guildID), true)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
if len(recs) == 0 {
|
||||||
|
return 0, 0, UserError("The server isn't locked by Velox.")
|
||||||
|
}
|
||||||
|
for _, r := range recs {
|
||||||
|
if err := e.UnlockChannel(ctx, guildID, id(r.ChannelID), by); err != nil {
|
||||||
|
log.Printf("security: unlocking channel %d in guild %s: %v", r.ChannelID, guildID, err)
|
||||||
|
failed++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
unlocked++
|
||||||
|
}
|
||||||
|
e.event(ctx, guildID, securityrepo.EventUnlocked, by, "", fmt.Sprintf("server: %d channels unlocked, %d failed", unlocked, failed))
|
||||||
|
return unlocked, failed, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetJoinLock turns the join lockdown on ("kick" or "ban") or off. Turning it
|
||||||
|
// off relaxes protection, so callers must check the actor first.
|
||||||
|
func (e *Engine) SetJoinLock(ctx context.Context, guildID, mode, by string) error {
|
||||||
|
switch mode {
|
||||||
|
case securityrepo.ActionOff, securityrepo.ActionKick, securityrepo.ActionBan:
|
||||||
|
default:
|
||||||
|
return UserError("Joins can be locked with kick or ban.")
|
||||||
|
}
|
||||||
|
if err := e.st.SetJoinLock(ctx, parseID(guildID), mode); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e.Invalidate(guildID)
|
||||||
|
kind := securityrepo.EventLocked
|
||||||
|
if mode == securityrepo.ActionOff {
|
||||||
|
kind = securityrepo.EventUnlocked
|
||||||
|
}
|
||||||
|
e.event(ctx, guildID, kind, by, "", "joins: "+mode)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,446 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// nukeCounter counts destructive actions per server, person and kind over a
|
||||||
|
// sliding window, and remembers whom it just punished so one nuke attempt
|
||||||
|
// is answered once.
|
||||||
|
type nukeCounter struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
hits map[string][]time.Time
|
||||||
|
punished map[string]time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxHitsPerKey = 200
|
||||||
|
punishCooldown = time.Minute
|
||||||
|
)
|
||||||
|
|
||||||
|
func newNukeCounter() *nukeCounter {
|
||||||
|
return &nukeCounter{hits: map[string][]time.Time{}, punished: map[string]time.Time{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add records a hit and returns how many fall inside the window.
|
||||||
|
func (c *nukeCounter) add(key string, now time.Time, window time.Duration) int {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
h := append(c.hits[key], now)
|
||||||
|
cut := 0
|
||||||
|
for cut < len(h) && now.Sub(h[cut]) > window {
|
||||||
|
cut++
|
||||||
|
}
|
||||||
|
h = h[cut:]
|
||||||
|
if len(h) > maxHitsPerKey {
|
||||||
|
h = h[len(h)-maxHitsPerKey:]
|
||||||
|
}
|
||||||
|
c.hits[key] = h
|
||||||
|
// Opportunistic cleanup so the map can't grow forever.
|
||||||
|
if len(c.hits) > 5000 {
|
||||||
|
for k, v := range c.hits {
|
||||||
|
if len(v) == 0 || now.Sub(v[len(v)-1]) > window {
|
||||||
|
delete(c.hits, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return len(h)
|
||||||
|
}
|
||||||
|
|
||||||
|
// claim reports whether this person may be punished now (not punished in the
|
||||||
|
// last minute), and marks them.
|
||||||
|
func (c *nukeCounter) claim(key string, now time.Time) bool {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
if t, ok := c.punished[key]; ok && now.Sub(t) < punishCooldown {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
c.punished[key] = now
|
||||||
|
if len(c.punished) > 5000 {
|
||||||
|
for k, t := range c.punished {
|
||||||
|
if now.Sub(t) > punishCooldown {
|
||||||
|
delete(c.punished, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func changeKey(c *discordgo.AuditLogChange) string {
|
||||||
|
if c == nil || c.Key == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return string(*c.Key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func permValue(v any) int64 {
|
||||||
|
switch x := v.(type) {
|
||||||
|
case string:
|
||||||
|
n, _ := strconv.ParseInt(x, 10, 64)
|
||||||
|
return n
|
||||||
|
case float64:
|
||||||
|
return int64(x)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Classify maps an audit log entry to a limit key, or flags it as always
|
||||||
|
// forbidden for untrusted members (instant). rolePerms looks up a role's
|
||||||
|
// permissions for role grants.
|
||||||
|
func Classify(entry *discordgo.AuditLogEntry, rolePerms func(roleID string) int64) (key string, instant bool, what string) {
|
||||||
|
if entry == nil || entry.ActionType == nil {
|
||||||
|
return "", false, ""
|
||||||
|
}
|
||||||
|
switch *entry.ActionType {
|
||||||
|
case discordgo.AuditLogActionChannelCreate:
|
||||||
|
return securityrepo.LimitChannelCreate, false, "created channels"
|
||||||
|
case discordgo.AuditLogActionChannelDelete:
|
||||||
|
return securityrepo.LimitChannelDelete, false, "deleted channels"
|
||||||
|
case discordgo.AuditLogActionRoleDelete:
|
||||||
|
return securityrepo.LimitRoleDelete, false, "deleted roles"
|
||||||
|
case discordgo.AuditLogActionMemberBanAdd:
|
||||||
|
return securityrepo.LimitBan, false, "banned members"
|
||||||
|
case discordgo.AuditLogActionMemberKick:
|
||||||
|
return securityrepo.LimitKick, false, "kicked members"
|
||||||
|
case discordgo.AuditLogActionWebhookCreate:
|
||||||
|
return securityrepo.LimitWebhookCreate, false, "created webhooks"
|
||||||
|
case discordgo.AuditLogActionMemberPrune:
|
||||||
|
return "", true, "pruned members"
|
||||||
|
case discordgo.AuditLogActionRoleCreate:
|
||||||
|
for _, c := range entry.Changes {
|
||||||
|
if changeKey(c) == "permissions" && permValue(c.NewValue)&DangerousPerms != 0 {
|
||||||
|
return "", true, "created a role with dangerous permissions"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return securityrepo.LimitRoleCreate, false, "created roles"
|
||||||
|
case discordgo.AuditLogActionRoleUpdate:
|
||||||
|
for _, c := range entry.Changes {
|
||||||
|
if changeKey(c) == "permissions" {
|
||||||
|
added := permValue(c.NewValue) &^ permValue(c.OldValue)
|
||||||
|
if added&DangerousPerms != 0 {
|
||||||
|
return "", true, "gave a role dangerous permissions"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case discordgo.AuditLogActionMemberRoleUpdate:
|
||||||
|
for _, c := range entry.Changes {
|
||||||
|
if changeKey(c) != "$add" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
list, _ := c.NewValue.([]any)
|
||||||
|
for _, item := range list {
|
||||||
|
if r, ok := item.(map[string]any); ok {
|
||||||
|
if rid, _ := r["id"].(string); rid != "" && rolePerms != nil && rolePerms(rid)&DangerousPerms != 0 {
|
||||||
|
return "", true, "gave someone a role with dangerous permissions"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case discordgo.AuditLogActionGuildUpdate:
|
||||||
|
for _, c := range entry.Changes {
|
||||||
|
if changeKey(c) == "vanity_url_code" {
|
||||||
|
return "", true, "changed the vanity URL"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnAuditLogEntry watches what members do: bots added by untrusted members
|
||||||
|
// (Join Gate) and destructive actions over the limits (anti-nuke).
|
||||||
|
func (e *Engine) OnAuditLogEntry(ctx context.Context, guildID string, entry *discordgo.AuditLogEntry) {
|
||||||
|
if entry == nil || entry.ActionType == nil || entry.UserID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s, _, err := e.load(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("security: settings for guild %s: %v", guildID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if *entry.ActionType == discordgo.AuditLogActionBotAdd {
|
||||||
|
e.checkBotAdd(ctx, guildID, s, entry)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !s.AntiNukeEnabled || !worthChecking(entry) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Role grants need care: someone could give themselves (or an alt) a
|
||||||
|
// trusted role to slip past everything below. Roles added by this very
|
||||||
|
// entry never count towards the actor's own trust, and an untrusted
|
||||||
|
// member granting a trusted role is stopped and the role taken back.
|
||||||
|
added := addedRoles(entry)
|
||||||
|
var actorRoles []string
|
||||||
|
if m, err := e.d.GuildMember(guildID, entry.UserID); err == nil {
|
||||||
|
actorRoles = m.Roles
|
||||||
|
if entry.TargetID == entry.UserID {
|
||||||
|
actorRoles = without(actorRoles, added)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if e.IsTrusted(ctx, guildID, entry.UserID, nonNil(actorRoles)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
key, instant, what := Classify(entry, func(roleID string) int64 { return e.rolePerms(guildID, roleID) })
|
||||||
|
if granted := e.trustedAmong(ctx, guildID, added); len(granted) > 0 {
|
||||||
|
instant, what = true, "gave someone a trusted role"
|
||||||
|
e.revokeRoles(guildID, entry.TargetID, granted)
|
||||||
|
}
|
||||||
|
if *entry.ActionType == discordgo.AuditLogActionRoleUpdate && e.isBotRole(guildID, entry.TargetID) {
|
||||||
|
instant, what = true, "changed Velox's own role"
|
||||||
|
}
|
||||||
|
if key == "" && !instant {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
who := guildID + "|" + entry.UserID
|
||||||
|
if !instant {
|
||||||
|
limit, ok := s.AntiNukeLimits[key]
|
||||||
|
if !ok {
|
||||||
|
limit = securityrepo.DefaultLimits()[key]
|
||||||
|
}
|
||||||
|
n := e.nukes.add(who+"|"+key, e.now(), time.Duration(s.AntiNukeWindowSeconds)*time.Second)
|
||||||
|
if n <= limit {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
what = fmt.Sprintf("%s %d times in %d seconds (limit %d)", what, n, s.AntiNukeWindowSeconds, limit)
|
||||||
|
}
|
||||||
|
if !e.nukes.claim(who, e.now()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
e.stopNuke(ctx, guildID, s, entry.UserID, what)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) rolePerms(guildID, roleID string) int64 {
|
||||||
|
for _, r := range e.guildRoles(guildID) {
|
||||||
|
if r.ID == roleID {
|
||||||
|
return r.Permissions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// worthChecking skips audit log entries that can never trigger anti-nuke
|
||||||
|
// before anything is looked up on Discord. A busy server writes many entries
|
||||||
|
// (message deletes, nickname changes...), and lookups for each would eat
|
||||||
|
// into the rate limit the bot needs most during an attack.
|
||||||
|
func worthChecking(entry *discordgo.AuditLogEntry) bool {
|
||||||
|
switch *entry.ActionType {
|
||||||
|
case discordgo.AuditLogActionChannelCreate, discordgo.AuditLogActionChannelDelete,
|
||||||
|
discordgo.AuditLogActionRoleCreate, discordgo.AuditLogActionRoleDelete, discordgo.AuditLogActionRoleUpdate,
|
||||||
|
discordgo.AuditLogActionMemberBanAdd, discordgo.AuditLogActionMemberKick, discordgo.AuditLogActionMemberPrune,
|
||||||
|
discordgo.AuditLogActionWebhookCreate:
|
||||||
|
return true
|
||||||
|
case discordgo.AuditLogActionMemberRoleUpdate:
|
||||||
|
return len(addedRoles(entry)) > 0 // removing roles is harmless
|
||||||
|
case discordgo.AuditLogActionGuildUpdate:
|
||||||
|
for _, c := range entry.Changes {
|
||||||
|
if changeKey(c) == "vanity_url_code" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const roleCacheTTL = 10 * time.Second
|
||||||
|
|
||||||
|
type cachedRoles struct {
|
||||||
|
roles []*discordgo.Role
|
||||||
|
at time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// guildRoles returns the guild's roles, cached briefly. Staleness can't hide
|
||||||
|
// an escalation: creating or editing a role with dangerous permissions is
|
||||||
|
// caught from its own audit log entry.
|
||||||
|
func (e *Engine) guildRoles(guildID string) []*discordgo.Role {
|
||||||
|
e.mu.Lock()
|
||||||
|
c, ok := e.roleCache[guildID]
|
||||||
|
e.mu.Unlock()
|
||||||
|
if ok && e.now().Sub(c.at) < roleCacheTTL {
|
||||||
|
return c.roles
|
||||||
|
}
|
||||||
|
roles, err := e.d.GuildRoles(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
e.mu.Lock()
|
||||||
|
if e.roleCache == nil {
|
||||||
|
e.roleCache = map[string]cachedRoles{}
|
||||||
|
}
|
||||||
|
e.roleCache[guildID] = cachedRoles{roles: roles, at: e.now()}
|
||||||
|
e.mu.Unlock()
|
||||||
|
return roles
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) stopNuke(ctx context.Context, guildID string, s *securityrepo.Settings, userID, what string) {
|
||||||
|
reason := "anti-nuke: " + what
|
||||||
|
e.event(ctx, guildID, securityrepo.EventNuke, userID, userID, what)
|
||||||
|
|
||||||
|
desc := fmt.Sprintf("<@%s> %s.", userID, what)
|
||||||
|
if err := e.Quarantine(ctx, guildID, userID, reason); err != nil {
|
||||||
|
log.Printf("security: quarantining %s in guild %s: %v", userID, guildID, err)
|
||||||
|
desc += "\n⚠️ I couldn't fully quarantine them: " + clip(err.Error(), 300) +
|
||||||
|
"\nMove the Velox role above their highest role and give it Manage Roles, Moderate Members and Kick Members."
|
||||||
|
} else {
|
||||||
|
desc += "\nThey were quarantined: roles removed (saved) and timed out. Release them with `/security release` if this was a mistake."
|
||||||
|
}
|
||||||
|
if s.AntiNukeLockServer {
|
||||||
|
locked, failed, err := e.LockServer(ctx, guildID, e.botID)
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
desc += "\n⚠️ Locking the server failed: " + clip(err.Error(), 200)
|
||||||
|
default:
|
||||||
|
desc += fmt.Sprintf("\nThe server is locked (%d channels", locked)
|
||||||
|
if failed > 0 {
|
||||||
|
desc += fmt.Sprintf(", %d couldn't be locked", failed)
|
||||||
|
}
|
||||||
|
desc += "). Unlock it with `/unlock server`."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if s.AntiNukeAutoRestore {
|
||||||
|
desc += "\nRestoring deleted roles and channels from the latest snapshot; a report follows."
|
||||||
|
go func() {
|
||||||
|
// Give the attack a moment to stop, then rebuild.
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
if _, err := e.Restore(context.Background(), guildID, e.botID); err != nil {
|
||||||
|
log.Printf("security: auto restore in guild %s: %v", guildID, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
e.send(ctx, guildID, s, alert{
|
||||||
|
ping: true, pingOwner: true, critical: true,
|
||||||
|
embed: &discordgo.MessageEmbed{
|
||||||
|
Title: "🛡️ Nuke attempt stopped", Description: desc, Color: colorAlert,
|
||||||
|
Timestamp: e.now().UTC().Format(time.RFC3339),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkBotAdd removes a bot that someone other than the owner or a trusted
|
||||||
|
// member added, when the Join Gate is on.
|
||||||
|
func (e *Engine) checkBotAdd(ctx context.Context, guildID string, s *securityrepo.Settings, entry *discordgo.AuditLogEntry) {
|
||||||
|
if !s.JoinGateEnabled || s.UnauthorizedBot == securityrepo.ActionOff || entry.TargetID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if e.IsTrusted(ctx, guildID, entry.UserID, nil) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
bot := &discordgo.User{ID: entry.TargetID}
|
||||||
|
if err := e.punish(guildID, bot, s.UnauthorizedBot, fmt.Sprintf("bot added by %s, who isn't trusted", entry.UserID)); err != nil {
|
||||||
|
log.Printf("security: removing unauthorized bot %s in guild %s: %v", entry.TargetID, guildID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
e.event(ctx, guildID, securityrepo.EventUnauthorizedBot, entry.UserID, entry.TargetID, s.UnauthorizedBot)
|
||||||
|
e.send(ctx, guildID, s, alert{
|
||||||
|
ping: true, critical: true,
|
||||||
|
embed: &discordgo.MessageEmbed{
|
||||||
|
Title: "Bot removed",
|
||||||
|
Description: fmt.Sprintf("<@%s> added the bot <@%s> but isn't on the trusted list, so the bot was %s.", entry.UserID, entry.TargetID, map[string]string{securityrepo.ActionKick: "kicked", securityrepo.ActionBan: "banned"}[s.UnauthorizedBot]),
|
||||||
|
Color: colorWarn,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// addedRoles lists the role IDs a member role update added.
|
||||||
|
func addedRoles(entry *discordgo.AuditLogEntry) []string {
|
||||||
|
if entry.ActionType == nil || *entry.ActionType != discordgo.AuditLogActionMemberRoleUpdate {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var out []string
|
||||||
|
for _, c := range entry.Changes {
|
||||||
|
if changeKey(c) != "$add" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
list, _ := c.NewValue.([]any)
|
||||||
|
for _, item := range list {
|
||||||
|
if r, ok := item.(map[string]any); ok {
|
||||||
|
if rid, _ := r["id"].(string); rid != "" {
|
||||||
|
out = append(out, rid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func without(list, remove []string) []string {
|
||||||
|
drop := map[string]bool{}
|
||||||
|
for _, r := range remove {
|
||||||
|
drop[r] = true
|
||||||
|
}
|
||||||
|
out := []string{}
|
||||||
|
for _, r := range list {
|
||||||
|
if !drop[r] {
|
||||||
|
out = append(out, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// nonNil keeps an empty role list from meaning "look the member up".
|
||||||
|
func nonNil(roles []string) []string {
|
||||||
|
if roles == nil {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
return roles
|
||||||
|
}
|
||||||
|
|
||||||
|
// trustedAmong returns which of the roles are on the trusted list.
|
||||||
|
func (e *Engine) trustedAmong(ctx context.Context, guildID string, roles []string) []string {
|
||||||
|
if len(roles) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
_, trusted, err := e.load(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var out []string
|
||||||
|
for _, r := range roles {
|
||||||
|
for _, t := range trusted {
|
||||||
|
if t.Kind == "role" && id(t.TargetID) == r {
|
||||||
|
out = append(out, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// revokeRoles takes roles back from a member (best effort; never the owner).
|
||||||
|
func (e *Engine) revokeRoles(guildID, userID string, roles []string) {
|
||||||
|
if userID == "" || e.IsOwner(guildID, userID) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m, err := e.d.GuildMember(guildID, userID)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
next := without(m.Roles, roles)
|
||||||
|
if _, err := e.d.GuildMemberEdit(guildID, userID, &discordgo.GuildMemberParams{Roles: &next},
|
||||||
|
discordgo.WithAuditLogReason("Velox anti-nuke: trusted role granted by someone untrusted")); err != nil {
|
||||||
|
log.Printf("security: taking back trusted roles from %s in guild %s: %v", userID, guildID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// isBotRole reports whether roleID is one of Velox's own roles.
|
||||||
|
func (e *Engine) isBotRole(guildID, roleID string) bool {
|
||||||
|
m, err := e.d.GuildMember(guildID, e.botID)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, r := range m.Roles {
|
||||||
|
if r == roleID {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,187 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"velox-bot/internal/db/repos/securityrepo"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// usableQuarantineRole returns the configured quarantine role if it is safe
|
||||||
|
// to hand out right now: it exists, isn't @everyone or managed by an
|
||||||
|
// integration, and has none of the dangerous permissions. Checked at use, so
|
||||||
|
// a role that gained permissions since it was configured is never assigned.
|
||||||
|
func usableQuarantineRole(s *securityrepo.Settings, guildID string, roles []*discordgo.Role) string {
|
||||||
|
if s.QuarantineRoleID == 0 || id(s.QuarantineRoleID) == guildID {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
for _, r := range roles {
|
||||||
|
if r.ID == id(s.QuarantineRoleID) {
|
||||||
|
if r.Managed || r.Permissions&DangerousPerms != 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return r.ID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quarantine neutralises a member: every role it can remove is taken away
|
||||||
|
// (and saved, so it can be given back), the quarantine role is added if one
|
||||||
|
// is configured, and the member is timed out for the longest Discord allows.
|
||||||
|
// Bots are kicked instead, since their own integration role can't be
|
||||||
|
// removed. The owner and the bot itself are never touched.
|
||||||
|
func (e *Engine) Quarantine(ctx context.Context, guildID, userID, reason string) error {
|
||||||
|
if userID == "" || userID == e.botID || e.IsOwner(guildID, userID) {
|
||||||
|
return errors.New("refusing to quarantine the owner or the bot")
|
||||||
|
}
|
||||||
|
s, _, err := e.load(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m, err := e.d.GuildMember(guildID, userID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("looking up the member: %w", err)
|
||||||
|
}
|
||||||
|
if m.User != nil && m.User.Bot {
|
||||||
|
return e.d.GuildMemberDeleteWithReason(guildID, userID, clip("Velox anti-nuke: "+reason, 400))
|
||||||
|
}
|
||||||
|
roles, err := e.d.GuildRoles(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("listing roles: %w", err)
|
||||||
|
}
|
||||||
|
managed := map[string]bool{}
|
||||||
|
for _, r := range roles {
|
||||||
|
if r.Managed {
|
||||||
|
managed[r.ID] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var keep []string
|
||||||
|
var previous []int64
|
||||||
|
for _, r := range m.Roles {
|
||||||
|
if managed[r] {
|
||||||
|
keep = append(keep, r) // can't be removed (e.g. a Nitro booster role)
|
||||||
|
} else {
|
||||||
|
previous = append(previous, parseID(r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
qRole := usableQuarantineRole(s, guildID, roles)
|
||||||
|
withRole := keep
|
||||||
|
if qRole != "" {
|
||||||
|
withRole = append(append([]string(nil), keep...), qRole)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save first: if Discord fails half-way, the roles are still on record.
|
||||||
|
if err := e.st.SaveQuarantine(ctx, securityrepo.Quarantine{
|
||||||
|
GuildID: parseID(guildID), UserID: parseID(userID), PreviousRoleIDs: previous, Reason: clip(reason, 300),
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("saving the quarantine: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
audit := discordgo.WithAuditLogReason(clip("Velox anti-nuke: "+reason, 400))
|
||||||
|
var errs []error
|
||||||
|
if _, err := e.d.GuildMemberEdit(guildID, userID, &discordgo.GuildMemberParams{Roles: &withRole}, audit); err != nil {
|
||||||
|
// A quarantine role Velox can't assign (e.g. moved above its own
|
||||||
|
// role) must not stop the roles from being removed: retry without it.
|
||||||
|
if qRole == "" {
|
||||||
|
errs = append(errs, fmt.Errorf("removing roles: %w", err))
|
||||||
|
} else if _, err2 := e.d.GuildMemberEdit(guildID, userID, &discordgo.GuildMemberParams{Roles: &keep}, audit); err2 != nil {
|
||||||
|
errs = append(errs, fmt.Errorf("removing roles: %w", err2))
|
||||||
|
} else {
|
||||||
|
log.Printf("security: quarantine role %s couldn't be assigned in guild %s (%v); roles were still removed", qRole, guildID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
until := e.now().Add(timeoutQuarantine)
|
||||||
|
if err := e.d.GuildMemberTimeout(guildID, userID, &until, audit); err != nil {
|
||||||
|
errs = append(errs, fmt.Errorf("timing out: %w", err))
|
||||||
|
}
|
||||||
|
e.event(ctx, guildID, securityrepo.EventQuarantined, e.botID, userID, reason)
|
||||||
|
return errors.Join(errs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release undoes a quarantine: the saved roles that still exist are given
|
||||||
|
// back, the quarantine role and the timeout are removed. Releasing relaxes
|
||||||
|
// protection, so callers must check the actor first.
|
||||||
|
func (e *Engine) Release(ctx context.Context, guildID, userID, by string) error {
|
||||||
|
rec, err := e.st.GetQuarantine(ctx, parseID(guildID), parseID(userID))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if rec == nil {
|
||||||
|
return UserError("That member isn't quarantined.")
|
||||||
|
}
|
||||||
|
s, _, err := e.load(ctx, guildID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m, err := e.d.GuildMember(guildID, userID)
|
||||||
|
if err != nil {
|
||||||
|
// They left: there's nothing to give back.
|
||||||
|
if derr := e.st.DeleteQuarantine(ctx, parseID(guildID), parseID(userID)); derr != nil {
|
||||||
|
return derr
|
||||||
|
}
|
||||||
|
e.event(ctx, guildID, securityrepo.EventReleased, by, userID, "member had left")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
roles, err := e.d.GuildRoles(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("listing roles: %w", err)
|
||||||
|
}
|
||||||
|
exists := map[string]bool{}
|
||||||
|
for _, r := range roles {
|
||||||
|
if !r.Managed && r.ID != guildID {
|
||||||
|
exists[r.ID] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
qRole := ""
|
||||||
|
if s.QuarantineRoleID != 0 {
|
||||||
|
qRole = id(s.QuarantineRoleID)
|
||||||
|
}
|
||||||
|
|
||||||
|
seen := map[string]bool{}
|
||||||
|
var next []string
|
||||||
|
add := func(r string) {
|
||||||
|
if !seen[r] && r != qRole {
|
||||||
|
seen[r] = true
|
||||||
|
next = append(next, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, r := range m.Roles {
|
||||||
|
add(r)
|
||||||
|
}
|
||||||
|
for _, r := range rec.PreviousRoleIDs {
|
||||||
|
if exists[id(r)] {
|
||||||
|
add(id(r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
audit := discordgo.WithAuditLogReason("Velox security: quarantine released")
|
||||||
|
if _, err := e.d.GuildMemberEdit(guildID, userID, &discordgo.GuildMemberParams{Roles: &next}, audit); err != nil {
|
||||||
|
return fmt.Errorf("giving the roles back: %w", err)
|
||||||
|
}
|
||||||
|
if err := e.d.GuildMemberTimeout(guildID, userID, nil, audit); err != nil {
|
||||||
|
log.Printf("security: removing the timeout of %s in guild %s: %v", userID, guildID, err)
|
||||||
|
}
|
||||||
|
if err := e.st.DeleteQuarantine(ctx, parseID(guildID), parseID(userID)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e.event(ctx, guildID, securityrepo.EventReleased, by, userID, fmt.Sprintf("%d roles given back", len(rec.PreviousRoleIDs)))
|
||||||
|
e.send(ctx, guildID, s, alert{embed: &discordgo.MessageEmbed{
|
||||||
|
Title: "Quarantine released",
|
||||||
|
Description: fmt.Sprintf("<@%s> was released by <@%s> and got their roles back.", userID, by),
|
||||||
|
Color: colorOK,
|
||||||
|
Timestamp: e.now().UTC().Format(time.RFC3339),
|
||||||
|
}})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quarantines lists who is quarantined in a guild.
|
||||||
|
func (e *Engine) Quarantines(ctx context.Context, guildID string) ([]securityrepo.Quarantine, error) {
|
||||||
|
return e.st.ListQuarantines(ctx, parseID(guildID))
|
||||||
|
}
|
||||||
Loaded 100 of 117 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user