-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (104 loc) · 3.31 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
package main
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/AraanBranco/immersive/config"
"github.com/AraanBranco/immersive/roleplay"
"github.com/bwmarrin/discordgo"
badgerhold "github.com/timshannon/badgerhold"
)
var (
CmdHandler *roleplay.CommandHandler
configuration *config.Configuration
botId string
PREFIX string
)
func init() {
configuration = config.Get()
PREFIX = configuration.GetEnvConfString("prefix")
}
func main() {
CmdHandler = roleplay.NewCommandHandler()
registerCommands()
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New(configuration.GetEnvConfString("discord_token"))
if err != nil {
fmt.Println("error creating Discord session, ", err)
return
}
if configuration.GetEnvConfBool("user_sharding") {
dg.ShardID = configuration.GetInteger("shard_id")
dg.ShardCount = configuration.GetInteger("shard_count")
}
dg.AddHandler(commandHandler)
// Open a websocket connection to Discord and begin listening.
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("Immersive 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()
}
func commandHandler(discord *discordgo.Session, message *discordgo.MessageCreate) {
user := message.Author
if user.ID == botId || user.Bot {
return
}
content := message.Content
if len(content) <= len(PREFIX) {
return
}
if content[:len(PREFIX)] != PREFIX {
return
}
content = content[len(PREFIX):]
if len(content) < 1 {
return
}
args := strings.Fields(content)
name := strings.ToLower(args[0])
command, found := CmdHandler.Get(name)
if !found {
return
}
channel, err := discord.State.Channel(message.ChannelID)
if err != nil {
fmt.Println("Error getting channel: ", err)
return
}
guild, err := discord.State.Guild(channel.GuildID)
if err != nil {
fmt.Println("Error getting guild: ", err)
return
}
optionsDB := badgerhold.DefaultOptions
optionsDB.Dir = configuration.GetEnvConfString("localdb")
optionsDB.ValueDir = configuration.GetEnvConfString("localdb")
db, err := badgerhold.Open(optionsDB)
if err != nil {
fmt.Println("Error instance db: ", err)
}
defer db.Close()
ctx := roleplay.NewContext(discord, guild, channel, user, message, configuration, db, CmdHandler)
ctx.Args = args[1:]
c := *command
c(*ctx)
}
func registerCommands() {
CmdHandler.Register("ajuda", roleplay.HelpCommand, "Lista de comandos do immersive.")
CmdHandler.Register("radio", roleplay.RadioCommand, "Gera uma nova frequência para rádio.")
CmdHandler.Register("outfit", roleplay.OutfitCommand, "Para listar os outfits de organizações.")
CmdHandler.Register("cidades", roleplay.GetCities, "Lista as cidades disponiveis.")
CmdHandler.Register("chest", roleplay.ChestCommand, "Contole de itens no chest")
CmdHandler.Register("clear", roleplay.ClearChannel, "Limpa as mensagens do canal [Admin]")
CmdHandler.Register("contato", roleplay.ContactCommand, "Comandos criar um contato")
}