Added discord module skeleton
This commit is contained in:
parent
0507cdb3d1
commit
ca2ca86819
8 changed files with 87 additions and 9 deletions
33
modules/discord/discord.go
Normal file
33
modules/discord/discord.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package discord
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"galched-bot/modules/settings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type (
|
||||
Discord struct {
|
||||
session *discordgo.Session
|
||||
}
|
||||
)
|
||||
|
||||
func New(s *settings.Settings) (*Discord, error) {
|
||||
key := fmt.Sprintf("Bot %s", s.DiscordToken)
|
||||
instance, err := discordgo.New(key)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot create discord instance")
|
||||
}
|
||||
return &Discord{session: instance}, nil
|
||||
}
|
||||
|
||||
func (d *Discord) Start() error {
|
||||
return d.session.Open()
|
||||
}
|
||||
|
||||
func (d *Discord) Stop() error {
|
||||
return d.session.Close()
|
||||
}
|
|
@ -8,7 +8,7 @@ import (
|
|||
"syscall"
|
||||
)
|
||||
|
||||
func NewGracefulContext() context.Context {
|
||||
func New() context.Context {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
go func() {
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
package settings
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
version = "3.0.0"
|
||||
discordTokenPath = "./tokens/.discordtoken"
|
||||
|
@ -7,14 +15,20 @@ const (
|
|||
|
||||
type (
|
||||
Settings struct {
|
||||
Version string
|
||||
DiscordTokenPath string
|
||||
Version string
|
||||
DiscordToken string
|
||||
}
|
||||
)
|
||||
|
||||
func NewSettings() Settings {
|
||||
return Settings{
|
||||
Version: version,
|
||||
DiscordTokenPath: discordTokenPath,
|
||||
func New() (*Settings, error) {
|
||||
log.Print(os.Getwd())
|
||||
discordToken, err := ioutil.ReadFile(discordTokenPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot read discord token file")
|
||||
}
|
||||
|
||||
return &Settings{
|
||||
Version: version,
|
||||
DiscordToken: string(discordToken),
|
||||
}, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue