2021-08-27 03:59:12 +00:00
package cmd
import (
"context"
2022-01-31 06:46:43 +00:00
"errors"
"fmt"
"os"
2021-08-27 03:59:12 +00:00
"time"
"git.handmade.network/hmn/hmn/src/db"
"git.handmade.network/hmn/hmn/src/discord"
2021-12-13 07:40:54 +00:00
"git.handmade.network/hmn/hmn/src/logging"
2021-08-27 03:59:12 +00:00
"git.handmade.network/hmn/hmn/src/website"
"github.com/spf13/cobra"
)
func init ( ) {
2021-12-13 07:40:54 +00:00
rootCommand := & cobra . Command {
Use : "discord" ,
Short : "Commands for interacting with Discord" ,
}
website . WebsiteCommand . AddCommand ( rootCommand )
2021-08-27 03:59:12 +00:00
scrapeCommand := & cobra . Command {
2021-12-13 07:40:54 +00:00
Use : "scrapechannel [<channel id>...]" ,
2021-08-27 03:59:12 +00:00
Short : "Scrape the entire history of Discord channels" ,
Long : "Scrape the entire history of Discord channels, saving message content (but not creating snippets)" ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
ctx := context . Background ( )
2022-05-07 18:58:00 +00:00
conn := db . NewConnPool ( )
2021-08-27 03:59:12 +00:00
defer conn . Close ( )
for _ , channelID := range args {
discord . Scrape ( ctx , conn , channelID , time . Time { } , false )
}
} ,
}
2021-12-13 07:40:54 +00:00
rootCommand . AddCommand ( scrapeCommand )
makeSnippetCommand := & cobra . Command {
2022-01-31 06:46:43 +00:00
Use : "makesnippet <channel id> [<message id>...]" ,
2022-02-07 12:25:21 +00:00
Short : "Make snippets from Discord messages" ,
Long : "Creates snippets from the specified messages in the specified channel. Will create a snippet as long as the poster of the message linked their account regardless of user settings." ,
2021-12-13 07:40:54 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2022-01-31 06:46:43 +00:00
if len ( args ) < 2 {
cmd . Usage ( )
os . Exit ( 1 )
}
2021-12-13 07:40:54 +00:00
ctx := context . Background ( )
2022-05-07 18:58:00 +00:00
conn := db . NewConn ( )
defer conn . Close ( ctx )
2021-08-27 03:59:12 +00:00
2022-01-31 06:46:43 +00:00
chanID := args [ 0 ]
count := 0
for _ , msgID := range args [ 1 : ] {
message , err := discord . GetChannelMessage ( ctx , chanID , msgID )
if errors . Is ( err , discord . NotFound ) {
logging . Warn ( ) . Msg ( fmt . Sprintf ( "no message found on discord for id %s" , msgID ) )
continue
} else if err != nil {
logging . Error ( ) . Msg ( fmt . Sprintf ( "failed to fetch discord message id %s" , msgID ) )
continue
}
err = discord . InternMessage ( ctx , conn , message )
if err != nil {
logging . Error ( ) . Msg ( fmt . Sprintf ( "failed to intern discord message id %s" , msgID ) )
continue
}
err = discord . HandleInternedMessage ( ctx , conn , message , false , true )
if err != nil {
logging . Error ( ) . Msg ( fmt . Sprintf ( "failed to handle interned message id %s" , msgID ) )
continue
}
count += 1
2021-12-13 07:40:54 +00:00
}
2022-01-31 06:46:43 +00:00
logging . Info ( ) . Msg ( fmt . Sprintf ( "Handled %d messages" , count ) )
2021-12-13 07:40:54 +00:00
} ,
}
rootCommand . AddCommand ( makeSnippetCommand )
2021-08-27 03:59:12 +00:00
}