-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (108 loc) · 2.83 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
)
// Variables
var (
Token string
quoteCommand string
quotes []string
err error
buffer = make([][]byte, 0)
)
//Leer desde Archivo
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
//Escribir en Archivo
func writeLines(lines []string, path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range lines {
fmt.Fprintln(w, line)
}
return w.Flush()
}
func main() {
Token = "DiscordBotToken"
quoteCommand = "exec"
//Cargamos las citas.
quotes, err = readLines("quotes.txt")
if err != nil {
log.Fatalf("readLines: %s", err)
}
//Creamos una sesión de DiscordGo utilizando el Token.
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
dg.AddHandler(ready) //Agregamos un handler con la función Ready. (Status del Bot)
dg.AddHandler(guildCreate) //Agregamos la función guildCreate (Cuando el Bot entra al servidor)
dg.AddHandler(messageCreate) //Agregamos la función messageCreate (Cuando se reciben mensajes)
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session.
dg.Close()
}
//Ready
func ready(s *discordgo.Session, event *discordgo.Ready) {
// Actualizamos el status del bot.
s.UpdateStatus(0, "DOOM")
}
func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
if event.Guild.Unavailable {
return
}
//Añadimos el mensaje que mandará el bot en cada canal que se encuentre. (y tenga permiso)
for _, channel := range event.Guild.Channels {
if channel.ID == event.Guild.ID {
_, _ = s.ChannelMessageSend(channel.ID, "¡QuoteBot está de vuelta!")
return
}
}
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
//Generamos un random
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
//ignoramos los mensajes que envíe el bot.
if m.Author.ID == s.State.User.ID {
return
}
if strings.Contains(m.Content, quoteCommand) { //Si el mensaje contiene el comando "exec" envia un mensaje random del quotes.txt
number := r1.Intn(len(quotes))
s.ChannelMessageSend(m.ChannelID, quotes[number])
}
}