Add commands from Python bot

This commit is contained in:
2024-01-21 20:34:54 +01:00
parent f3d11de24e
commit bdb985c74b
5 changed files with 227 additions and 16 deletions

12
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}

13
.vscode/settings.json vendored
View File

@ -1,3 +1,14 @@
{
"cSpell.words": ["bwmarrin", "discordgo", "godotenv", "joho"]
"cSpell.words": [
"bwmarrin",
"discordgo",
"godotenv",
"GoodAnimemes",
"joho",
"Ryouiki",
"vartanbeno",
"waifu",
"waifus",
"Zettai"
]
}

10
go.mod
View File

@ -2,10 +2,18 @@ module github.com/TheLovinator1/ANewDawn
go 1.21.6
require github.com/bwmarrin/discordgo v0.27.1
require (
github.com/bwmarrin/discordgo v0.27.1
github.com/vartanbeno/go-reddit/v2 v2.0.1
)
require (
github.com/golang/protobuf v1.2.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
google.golang.org/appengine v1.4.0 // indirect
)

27
go.sum
View File

@ -1,12 +1,39 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/vartanbeno/go-reddit/v2 v2.0.1 h1:P6ITpf5YHjdy7DHZIbUIDn/iNAoGcEoDQnMa+L4vutw=
github.com/vartanbeno/go-reddit/v2 v2.0.1/go.mod h1:758/S10hwZSLm43NPtwoNQdZFSg3sjB5745Mwjb0ANI=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

181
main.go
View File

@ -1,44 +1,197 @@
package main
import (
"context"
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"github.com/bwmarrin/discordgo"
"github.com/vartanbeno/go-reddit/v2/reddit"
)
/*
/ping - The bot responds with "Pong!".
*/
var config Config
func init() {
loadedConfig, err := Load()
if err != nil {
log.Fatal(err)
}
config = *loadedConfig
}
func GetPostsFromReddit(subreddit string) (string, error) {
client, err := reddit.NewReadonlyClient()
if err != nil {
log.Println("Failed to create Reddit client:", err)
return "", err
}
posts, _, err := client.Subreddit.TopPosts(context.Background(), subreddit, &reddit.ListPostOptions{
ListOptions: reddit.ListOptions{
Limit: 100,
},
Time: "all",
})
if err != nil {
return "", err
}
// [Title](<https://old.reddit.com{Permalink}>)\n{URL}
randInt := rand.Intn(len(posts))
discordMessage := fmt.Sprintf("[%v](<https://old.reddit.com%v>)\n%v", posts[randInt].Title, posts[randInt].Permalink, posts[randInt].URL)
return discordMessage, nil
}
func handleRedditCommand(s *discordgo.Session, i *discordgo.InteractionCreate, subreddit string) {
post, err := GetPostsFromReddit(subreddit)
if err != nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Cannot get a random post: %v", err),
Flags: discordgo.MessageFlagsEphemeral,
},
})
return
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: post,
},
})
}
var (
commands = []*discordgo.ApplicationCommand{
{
Name: "ping",
Description: "Pong!",
Name: "dank_memes",
Description: "Sends dank meme from /r/GoodAnimemes",
},
{
Name: "waifus",
Description: "Sends waifu from /r/WatchItForThePlot",
},
{
Name: "milkers",
Description: "Sends milkers from /r/RetrousseTits",
},
{
Name: "thighs",
Description: "Sends thighs from /r/ZettaiRyouiki",
},
{
Name: "help",
Description: "Sends help message",
},
{
Name: "echo",
Description: "Echoes your message",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "message",
Description: "The message to echo",
Required: true,
},
},
},
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
// Ping command
"ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Dank memes command
"dank_memes": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
handleRedditCommand(s, i, "GoodAnimemes")
},
// Waifus command
"waifus": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
handleRedditCommand(s, i, "WatchItForThePlot")
},
// Milkers command
"milkers": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
handleRedditCommand(s, i, "RetrousseTits")
},
// Thighs command
"thighs": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
handleRedditCommand(s, i, "ZettaiRyouiki")
},
// Help command
"help": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Send the help message to the channel where the command was used
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Pong!",
Content: "**Commands**\n\n/dank_memes - Sends dank meme from /r/GoodAnimemes\n/waifus - Sends waifu from /r/WatchItForThePlot\n/milkers - Sends milkers from /r/RetrousseTits\n/thighs - Sends thighs from /r/ZettaiRyouiki\n/help - Sends help message\n/echo - Echoes your message",
},
})
},
// Echo command
"echo": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Check if the user provided a message
if len(i.ApplicationCommandData().Options) == 0 {
// If not, send an ephemeral message to the user
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You need to provide a message!",
Flags: discordgo.MessageFlagsEphemeral,
},
})
return
}
// Check that the option contains text
if i.ApplicationCommandData().Options[0].Type != discordgo.ApplicationCommandOptionString {
// If not, send an ephemeral message to the user
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "The message needs to be text!",
Flags: discordgo.MessageFlagsEphemeral,
},
})
return
}
// Check that the option is not empty
if i.ApplicationCommandData().Options[0].StringValue() == "" {
// If not, send an ephemeral message to the user
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "The message cannot be empty!",
Flags: discordgo.MessageFlagsEphemeral,
},
})
return
}
// Respond to the original message so we don't get "This interaction failed" error
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "love u",
Flags: discordgo.MessageFlagsEphemeral,
},
})
// Send the message to the channel where the command was used
s.ChannelMessageSend(i.ChannelID, i.ApplicationCommandData().Options[0].StringValue())
},
}
)
func main() {
config, err := Load()
if err != nil {
log.Fatal(err)
}
// Print the token for debugging purposes.
discordToken := config.DiscordToken
fmt.Println("Discord Token:", discordToken)
@ -72,7 +225,7 @@ func main() {
log.Println("Adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {
cmd, err := session.ApplicationCommandCreate(session.State.User.ID, "341001473661992962", v)
cmd, err := session.ApplicationCommandCreate(session.State.User.ID, "", v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}