Compare commits

..

1 Commits

Author SHA1 Message Date
37c872c50a Start some code organization 2025-02-11 00:05:42 -05:00
8 changed files with 140 additions and 262 deletions

View File

@ -1,21 +0,0 @@
name: Run Go Tests
on:
pull_request:
branches: [master]
jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'
- name: Run Tests
run: make test

View File

@ -1,3 +0,0 @@
test:
@echo "Running tests for main and all packages"
go test ./...

View File

@ -6,7 +6,7 @@ Currently in development.
- `ffmpeg` make sure libopus is included
- `yt-dlp`
## Running locally
## Current testing steps
Copy .env.example to .env
@ -14,8 +14,4 @@ Populate the discord bot keys
`go run .`
type `!` in any channel in the discord while you're in a voice channel.
## Running tests locally
`make test`
type `!` in any channel in the discord while you're in a voice channel.

110
main.go
View File

@ -4,46 +4,31 @@ import (
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"git.thegrind.dev/thegrind/papibot/pkg/bot"
"git.thegrind.dev/thegrind/papibot/pkg/opusframes"
"git.thegrind.dev/thegrind/papibot/pkg/youtube"
dg "github.com/bwmarrin/discordgo"
"github.com/javif89/dotenv"
)
var commandHandlers = map[string]func(s *dg.Session, i *dg.InteractionCreate){
"play": playCommand,
}
func main() {
env := dotenv.Load(".env")
botToken := env.Get("DISCORD_BOT_TOKEN")
appId := env.Get("DISCORD_APP_ID")
bot := bot.New(botToken, appId)
log.Println("Starting bot")
err := bot.Start()
log.Println("Error starting the bot", err)
var discord *dg.Session
var err error
discord, err = dg.New(fmt.Sprintf("Bot %s", botToken))
if err != nil {
log.Println("Error starting the bot", err)
return
}
discord.AddHandler(ready)
discord.Identify.Intents = dg.IntentsGuilds | dg.IntentsGuildMessages | dg.IntentsGuildVoiceStates
err = discord.Open()
if err != nil {
log.Println("Error opening Discord session: ", err)
}
// Register slash commands
discord.AddHandler(handleSlashCommand)
log.Println("Adding command handlers")
bot.AddSlashCommand("play", playCommand)
log.Println("Done")
log.Println("Bot is running. Press CTRL+C to exit.")
sc := make(chan os.Signal, 1)
@ -51,15 +36,7 @@ func main() {
<-sc
log.Println("Bot is exiting...")
log.Println("Removing commands")
appId := env.Get("DISCORD_APP_ID")
cmds, _ := discord.ApplicationCommands(appId, "338782945110392832")
for _, cmd := range cmds {
discord.ApplicationCommandDelete(appId, "338782945110392832", cmd.ID)
}
discord.Close()
bot.Stop()
log.Println("Goodbye!")
}
@ -75,7 +52,12 @@ func playCommand(s *dg.Session, i *dg.InteractionCreate) {
},
})
downloadVideo(url)
os.Remove("vid.webm")
youtube.Download(url, "vid.webm")
start := time.Now()
opusframes.Encode("vid.webm", "vid.of")
duration := time.Since(start)
log.Printf("Encoding took: %s", duration)
// Get the channel
msgServer, err := s.State.Guild(i.GuildID)
@ -130,62 +112,4 @@ func playOnVoiceChannel(voiceChannel *dg.VoiceConnection) {
log.Printf("Sending packet: %d bytes", len(p))
voiceChannel.OpusSend <- p
}
}
func handleSlashCommand(s *dg.Session, i *dg.InteractionCreate) {
log.Println("Command received?")
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
}
func ready(s *dg.Session, event *dg.Ready) {
commands := []*dg.ApplicationCommand{
{
Name: "play",
Description: "Play a song from youtube, spotify, apple music, etc",
Options: []*dg.ApplicationCommandOption{
{
Type: dg.ApplicationCommandOptionString,
Name: "url",
Description: "URL to the song",
Required: true,
},
},
},
}
env := dotenv.Load(".env")
appId := env.Get("DISCORD_APP_ID")
log.Println("Bot is ready")
log.Println("Bulk registering commands")
_, err := s.ApplicationCommandBulkOverwrite(appId, "338782945110392832", commands)
if err != nil {
log.Fatal(err)
}
log.Println("Done")
}
func downloadVideo(url string) error {
log.Printf("Downloading: %s", url)
os.Remove("vid.webm")
cmd := exec.Command(
"yt-dlp",
"-i",
"-q",
"-f", "bestaudio",
"-o",
"vid.%(ext)s",
url)
err := cmd.Run()
log.Println("Downloaded")
start := time.Now()
opusframes.Encode("vid.webm", "vid.of")
duration := time.Since(start)
log.Printf("Encoding took: %s", duration)
return err
}

97
pkg/bot/bot.go Normal file
View File

@ -0,0 +1,97 @@
package bot
import (
"fmt"
"log"
dg "github.com/bwmarrin/discordgo"
)
type Bot struct {
token string
appId string
isPlaying bool
session *dg.Session
slashCommands map[string]func(s *dg.Session, i *dg.InteractionCreate)
}
func New(token, appId string) *Bot {
return &Bot{
token: token,
appId: appId,
slashCommands: make(map[string]func(s *dg.Session, i *dg.InteractionCreate)),
}
}
// TODO: Let's get some custom error types going
// at some point
func (b *Bot) Start() error {
var err error
b.session, err = dg.New(fmt.Sprintf("Bot %s", b.token))
if err != nil {
return err
}
b.session.AddHandler(b.ready)
b.session.Identify.Intents = dg.IntentsGuilds | dg.IntentsGuildMessages | dg.IntentsGuildVoiceStates
err = b.session.Open()
if err != nil {
return err
}
// Register slash commands
b.session.AddHandler(b.handleSlashCommand)
return nil
}
// TODO: Don't hard code the server. We can
// register the commands globally
func (b *Bot) Stop() {
cmds, _ := b.session.ApplicationCommands(b.appId, "338782945110392832")
for _, cmd := range cmds {
b.session.ApplicationCommandDelete(b.appId, "338782945110392832", cmd.ID)
}
b.session.Close()
}
func (b *Bot) AddSlashCommand(name string, handler func(*dg.Session, *dg.InteractionCreate)) {
b.slashCommands[name] = handler
}
func (b *Bot) handleSlashCommand(s *dg.Session, i *dg.InteractionCreate) {
log.Println("Command received?")
if h, ok := b.slashCommands[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
}
func (b *Bot) ready(s *dg.Session, event *dg.Ready) {
commands := []*dg.ApplicationCommand{
{
Name: "play",
Description: "Play a song from youtube, spotify, apple music, etc",
Options: []*dg.ApplicationCommandOption{
{
Type: dg.ApplicationCommandOptionString,
Name: "url",
Description: "URL to the song",
Required: true,
},
},
},
}
log.Println("Bot is ready")
log.Println("Bulk registering commands")
_, err := s.ApplicationCommandBulkOverwrite(b.appId, "338782945110392832", commands)
if err != nil {
log.Fatal(err)
}
log.Println("Done")
}

View File

@ -1,54 +0,0 @@
package validation
import (
"errors"
"fmt"
neturl "net/url"
"strings"
)
// Errors
var (
ErrNotAUrl = errors.New("not a URL")
ErrIncorrectProtocol = errors.New("incorrect url protocol")
ErrServiceUnsupported = errors.New("not a URL")
)
// Music hosts that we support
var musicHosts = []string{
"youtube.com",
"www.youtube.com",
}
func IsUrl(url string) (bool, *neturl.URL) {
// If a URL has no scheme, this will fail.
// So we'll add one if not present
if !strings.Contains(url, "://") {
url = fmt.Sprintf("https://%s", url)
}
parsed, err := neturl.ParseRequestURI(url)
return (err == nil), parsed
}
func IsMusicUrl(url string) (bool, error) {
isUrl, parsed := IsUrl(url)
if !isUrl {
return false, ErrNotAUrl
}
// If we have a scheme and it's not http/https we fail
if parsed.Scheme != "" && !strings.Contains(parsed.Scheme, "http") {
return false, ErrIncorrectProtocol
}
for _, host := range musicHosts {
if host == parsed.Host {
return true, nil
}
}
return false, ErrServiceUnsupported
}

View File

@ -1,85 +0,0 @@
package validation
import (
"testing"
)
func TestIsUrl(t *testing.T) {
is, _ := IsUrl("definitely not a url")
if is {
t.Error("Non-url text detected as URL")
}
is, _ = IsUrl("https://example.com")
if !is {
t.Error("URL not detected as URL")
}
}
func TestSchemeHandling(t *testing.T) {
// No scheme but valid url
is, _ := IsUrl("youtube.com")
if !is {
t.Error("URL without scheme came back as not a url")
}
// Preserve scheme
is, parsed := IsUrl("ftp://youtube.com")
if !is {
t.Error("URL without scheme came back as not a url")
}
if parsed.Scheme != "ftp" {
t.Error("URL scheme was replaced incorrectly")
}
}
func TestSupportedMusicUrls(t *testing.T) {
// Test actual music url
for _, url := range musicHosts {
is, _ := IsMusicUrl(url)
if !is {
t.Error("Supported service was detected as unsupported")
}
}
}
func TestIsMusicUrlErrors(t *testing.T) {
// Not a URL
is, err := IsMusicUrl("not a url")
if is {
t.Error("Non-URL was detected as url")
}
if err != ErrNotAUrl {
t.Error("Incorrect error returned for Non-url link")
}
// Incorrect protocol
is, err = IsMusicUrl("ssh://youtube.com")
if is {
t.Error("Incorrect protocol was not caught")
}
if err != ErrIncorrectProtocol {
t.Error("Incorrect error returned for incorrect protocol")
}
// Unsupported service
is, err = IsMusicUrl("https://www.deezer.com/")
if is {
t.Error("Unsupported service was not caught")
}
if err != ErrServiceUnsupported {
t.Error("Unsupported service did not return the correct error")
}
}

24
pkg/youtube/youtube.go Normal file
View File

@ -0,0 +1,24 @@
package youtube
import (
"os/exec"
)
func Download(url, outPath string) error {
cmd := exec.Command(
"yt-dlp",
"-i",
"-q",
"-f", "bestaudio",
"-o",
outPath,
url)
err := cmd.Run()
if err != nil {
return err
}
return err
}