From ca2ca868195c4b1f935b926b45c30fe000012644 Mon Sep 17 00:00:00 2001 From: alexvanin Date: Fri, 10 May 2019 11:53:58 +0300 Subject: [PATCH] Added discord module skeleton --- .gitignore | 1 + go.mod | 2 ++ go.sum | 8 +++++++ main.go | 24 +++++++++++++++++++-- modules/discord/discord.go | 33 +++++++++++++++++++++++++++++ modules/grace/graceful.go | 2 +- modules/settings/settings.go | 26 +++++++++++++++++------ {modules/tokens => tokens}/.gitkeep | 0 8 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 modules/discord/discord.go rename {modules/tokens => tokens}/.gitkeep (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..be5407e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +./tokens/*token \ No newline at end of file diff --git a/go.mod b/go.mod index a279677..2008330 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index f41b6c3..31b3e85 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 8ac8bd3..d2073ab 100644 --- a/main.go +++ b/main.go @@ -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()) diff --git a/modules/discord/discord.go b/modules/discord/discord.go new file mode 100644 index 0000000..c12da33 --- /dev/null +++ b/modules/discord/discord.go @@ -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() +} diff --git a/modules/grace/graceful.go b/modules/grace/graceful.go index 1570c85..b0559c1 100644 --- a/modules/grace/graceful.go +++ b/modules/grace/graceful.go @@ -8,7 +8,7 @@ import ( "syscall" ) -func NewGracefulContext() context.Context { +func New() context.Context { ctx, cancel := context.WithCancel(context.Background()) go func() { diff --git a/modules/settings/settings.go b/modules/settings/settings.go index 6e6f4ba..0847de5 100644 --- a/modules/settings/settings.go +++ b/modules/settings/settings.go @@ -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 } diff --git a/modules/tokens/.gitkeep b/tokens/.gitkeep similarity index 100% rename from modules/tokens/.gitkeep rename to tokens/.gitkeep