Added discord module skeleton

This commit is contained in:
Alex Vanin 2019-05-10 11:53:58 +03:00
parent 0507cdb3d1
commit ca2ca86819
8 changed files with 87 additions and 9 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
./tokens/*token

2
go.mod
View file

@ -1,6 +1,8 @@
module galched-bot
require (
github.com/bwmarrin/discordgo v0.19.0
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0 // indirect
go.uber.org/atomic v1.3.2 // indirect
go.uber.org/dig v1.7.0 // indirect

8
go.sum
View file

@ -1,5 +1,11 @@
github.com/bwmarrin/discordgo v0.19.0 h1:kMED/DB0NR1QhRcalb85w0Cu3Ep2OrGAqZH1R5awQiY=
github.com/bwmarrin/discordgo v0.19.0/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q=
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/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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=
@ -15,3 +21,5 @@ go.uber.org/goleak v0.10.0 h1:G3eWbSNIskeRqtsN/1uI5B+eP73y3JUuBsv9AZjehb4=
go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

24
main.go
View file

@ -4,6 +4,7 @@ import (
"context"
"log"
"galched-bot/modules/discord"
"galched-bot/modules/grace"
"galched-bot/modules/settings"
@ -17,22 +18,41 @@ type (
fx.In
Context context.Context
Settings settings.Settings
Discord *discord.Discord
Settings *settings.Settings
}
)
func (s *silentPrinter) Printf(str string, i ...interface{}) {}
func start(p appParam) {
var err error
log.Print("main: starting galched-bot v", p.Settings.Version)
err = p.Discord.Start()
if err != nil {
log.Fatal("discord: cannot start instance", err)
}
log.Print("main: discord instance running")
log.Print("main: — — —")
<-p.Context.Done()
log.Print("main: stopping galched-bot")
err = p.Discord.Stop()
if err != nil {
log.Fatal("discord: cannot stop instance", err)
}
log.Print("main: galched bot successfully stopped")
}
func main() {
var err error
app := fx.New(
fx.Logger(new(silentPrinter)),
fx.Provide(settings.NewSettings, grace.NewGracefulContext),
fx.Provide(settings.New, grace.New, discord.New),
fx.Invoke(start))
err = app.Start(context.Background())

View 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()
}

View file

@ -8,7 +8,7 @@ import (
"syscall"
)
func NewGracefulContext() context.Context {
func New() context.Context {
ctx, cancel := context.WithCancel(context.Background())
go func() {

View file

@ -1,5 +1,13 @@
package settings
import (
"io/ioutil"
"log"
"os"
"github.com/pkg/errors"
)
const (
version = "3.0.0"
discordTokenPath = "./tokens/.discordtoken"
@ -8,13 +16,19 @@ const (
type (
Settings struct {
Version string
DiscordTokenPath 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
}