Created
December 29, 2016 15:29
-
-
Save zet4/1ccba7af6e8625a6cb6a4540f01e35c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"discord_token": "Bot TOKENHERE", | |
"github_user": "YOURUSERNAMEHERE", | |
"github_token": "TOKENHERE", | |
"repo": "TARGETREPOHERE" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"strings" | |
"time" | |
"github.com/bwmarrin/discordgo" | |
) | |
type ( | |
config struct { | |
DiscordToken string `json:"discord_token"` | |
GithubUser string `json:"github_user"` | |
GithubToken string `json:"github_token"` | |
Repo string `json:"repo"` | |
} | |
request struct { | |
Title string `json:"title"` | |
Body string `json:"body"` | |
} | |
response struct { | |
HTMLURL string `json:"html_url"` | |
} | |
) | |
var ( | |
conf config | |
sess *discordgo.Session | |
client = &http.Client{Timeout: 20 * time.Second} | |
) | |
func submitIssue(name, body string) (string, error) { | |
var buf bytes.Buffer | |
err := json.NewEncoder(&buf).Encode(request{name, body}) | |
if err != nil { | |
return "", err | |
} | |
req, err := http.NewRequest("POST", fmt.Sprintf("https://api.github.com/repos/%s/issues", conf.Repo), bytes.NewReader(buf.Bytes())) | |
if err != nil { | |
return "", err | |
} | |
req.SetBasicAuth(conf.GithubUser, conf.GithubToken) | |
rsp, err := client.Do(req) | |
if err != nil { | |
return "", err | |
} | |
defer rsp.Body.Close() | |
if rsp.StatusCode != 201 { | |
raw, err := ioutil.ReadAll(rsp.Body) | |
if err != nil { | |
return "", err | |
} | |
return "", fmt.Errorf("Issue while making an issue: %s", string(raw)) | |
} | |
var resp response | |
err = json.NewDecoder(rsp.Body).Decode(&resp) | |
if err != nil { | |
return "", err | |
} | |
return resp.HTMLURL, nil | |
} | |
func handleMessages(s *discordgo.Session, m *discordgo.MessageCreate) { | |
if !strings.HasPrefix(m.Content, "!issue") { | |
return | |
} | |
content := strings.SplitN(strings.TrimSpace(strings.TrimPrefix(m.Content, "!issue")), "|", 2) | |
if len(content) != 2 { | |
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Invalid argument count '%d', expected 2.", len(content))) | |
return | |
} | |
result, err := submitIssue(strings.TrimSpace(content[0]), strings.TrimSpace(content[1])) | |
if err != nil { | |
s.ChannelMessageSend(m.ChannelID, err.Error()) | |
return | |
} | |
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Your issue has been created: %s", result)) | |
} | |
func init() { | |
file, err := os.Open("config.json") | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = json.NewDecoder(file).Decode(&conf) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func main() { | |
sess, err := discordgo.New(conf.DiscordToken) | |
if err != nil { | |
log.Fatal(err) | |
} | |
sess.AddHandler(handleMessages) | |
err = sess.Open() | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Print("Bot is now running.") | |
<-make(chan struct{}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment