-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbottomode.go
114 lines (94 loc) · 2.63 KB
/
bottomode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/bwmarrin/discordgo"
)
// Config represents the configuration file structure
type Config struct {
Token string `json:"token"`
}
var config Config
func main() {
// Load the configuration
file, err := os.Open("config.json")
if err != nil {
log.Fatalf("Error opening config file: %v", err)
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
log.Fatalf("Error decoding config file: %v", err)
}
// Create a new Discord session
dg, err := discordgo.New("Bot " + config.Token)
if err != nil {
log.Fatalf("Error creating Discord session: %v", err)
}
// Register the messageCreate func as a callback for MessageCreate events
dg.AddHandler(messageCreate)
// Open a websocket connection to Discord and begin listening
err = dg.Open()
if err != nil {
log.Fatalf("Error opening connection: %v", err)
}
// Wait here until CTRL-C or other term signal is received
fmt.Println("Bot is now running. Press CTRL-C to exit.")
select {}
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the authenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
// If the message is "/meme", fetch and post a meme
if m.Content == "/meme" {
meme, err := getRandomMeme()
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Failed to fetch a meme.")
return
}
s.ChannelMessageSend(m.ChannelID, meme)
}
}
// getRandomMeme fetches a random meme from Reddit
func getRandomMeme() (string, error) {
url := "https://www.reddit.com/r/memes/random/.json"
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var result []interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return "", err
}
if len(result) == 0 {
return "", fmt.Errorf("no meme found")
}
postData := result[0].(map[string]interface{})
data := postData["data"].(map[string]interface{})
children := data["children"].([]interface{})
if len(children) == 0 {
return "", fmt.Errorf("no meme found")
}
memeData := children[0].(map[string]interface{})["data"].(map[string]interface{})
title := memeData["title"].(string)
url = memeData["url"].(string)
return fmt.Sprintf("%s\n%s", title, url), nil
}