-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (122 loc) · 3.1 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"os"
"runtime/debug"
"github.com/bwmarrin/discordgo"
flag "github.com/spf13/pflag"
)
func version() string {
info, ok := debug.ReadBuildInfo()
if !ok {
// Goモジュールが無効など
return "(devel)"
}
return info.Main.Version
}
type CliOutput struct {
// Channel string `json:"channel"`
// Timestamp string `json:"timestamp"`
}
type Options struct {
session *discordgo.Session
token string
postOpts *PostOptions
}
type PostOptions struct {
channel string
text string
filePaths []string
}
func main() {
var (
optText = flag.String("text", "", "post text")
optFile = flag.String("file", "", "post file path")
envToken = os.Getenv("DISCORD_TOKEN")
optToken = flag.String("token", "", "discord app token")
optChannel = flag.String("channel", "", "post slack channel id")
// envProfile = os.Getenv("DISCORD_QUICKPOST_PROFILE")
// optProfile = flag.String("profile", "", "discord quickpost profile name")
noFail = flag.Bool("nofail", false, "always return success code(0)")
printVersion = flag.Bool("version", false, "print version")
errText []string
)
flag.Parse()
if *printVersion {
fmt.Println(version())
os.Exit(0)
}
opts := &Options{
postOpts: &PostOptions{},
}
opts.postOpts.text = *optText
// var profile = &profile{}
// profileName := strGetFirstOne(*optProfile, envProfile)
// if profileName != "" {
// var err error
// profile, err = getProfile(profileName)
// if err != nil {
// errText = append(errText, fmt.Sprintf("error: failed read profile %s. %s", profPath, err.Error()))
// }
// }
// opts.token = strGetFirstOne(*optToken, envToken, profile.Token)
opts.token = strGetFirstOne(*optToken, envToken)
if opts.token == "" {
errText = append(errText, "error: slack token is required")
}
// opts.postOpts.channel = strGetFirstOne(*optChannel, profile.Channel)
opts.postOpts.channel = strGetFirstOne(*optChannel)
if opts.postOpts.channel == "" {
errText = append(errText, "error: channel is required")
}
if *optFile != "" {
opts.postOpts.filePaths = []string{*optFile}
}
// Discord initialize
session, err := discordgo.New("Bot " + opts.token)
if err != nil {
fmt.Println(err.Error())
if *noFail {
os.Exit(0)
}
os.Exit(1)
}
opts.session = session
if _, err := Do(opts); err != nil {
fmt.Println(err.Error())
if *noFail {
os.Exit(0)
}
os.Exit(1)
}
return
}
func Do(opts *Options) (*CliOutput, error) {
var output *CliOutput
var sendParameter = &discordgo.MessageSend{}
if opts.postOpts.text != "" {
sendParameter.Content = opts.postOpts.text
}
// TODO: 2つ以上に対応する
var f *os.File
if len(opts.postOpts.filePaths) > 0 {
var err error
if f, err = os.Open(opts.postOpts.filePaths[0]); err != nil {
return nil, err
}
defer f.Close()
file := &discordgo.File{
Name: f.Name(),
ContentType: "text/plain",
Reader: f,
}
sendParameter.Files = []*discordgo.File{file}
}
if _, err := opts.session.ChannelMessageSendComplex(
opts.postOpts.channel,
sendParameter,
); err != nil {
return nil, err
}
return output, nil
}