Skip to content

Commit

Permalink
Merge pull request #5 from PoCInnovation/update/migrateFromBotToWebhook
Browse files Browse the repository at this point in the history
update: functional webhook request replacing discord bot
  • Loading branch information
Doozers authored Jun 5, 2022
2 parents c6bfa38 + a8e6973 commit 502769b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
35 changes: 25 additions & 10 deletions getNewPosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
embed "github.com/Clinet/discordgo-embed"
"github.com/enescakir/emoji"
abci "github.com/gnolang/gno/pkgs/bft/abci/types"
"github.com/gnolang/gno/pkgs/bft/rpc/client"
Expand Down Expand Up @@ -51,6 +50,18 @@ type Post struct {
Id int
}

type Author struct {
Name string `json:"name"`
IconUrl string `json:"icon_url"`
}

type Embed struct {
Title string `json:"title"`
Description string `json:"description"`
Author Author `json:"author"`
Color int `json:"color"`
}

func GetPostInfos(post string, id int) Post {
regAuthor := regexp.MustCompile(`\\- \[(@[a-z]+)\]`)
regTitle := regexp.MustCompile(`## \[([^\[\]]+)\]`)
Expand Down Expand Up @@ -81,7 +92,7 @@ func GetPostID(s string) (int, error) {
return strconv.Atoi(match[1])
}

func parseNewPosts(BoardPosts string, board string) []*embed.Embed {
func parseNewPosts(BoardPosts string, board string) []Embed {
var post []Post
newMaxId := maxId[board]
a := strings.Split(BoardPosts, "----------------------------------------")
Expand All @@ -98,20 +109,24 @@ func parseNewPosts(BoardPosts string, board string) []*embed.Embed {
return EmbedNewPosts(post, board)
}

func EmbedNewPosts(posts []Post, board string) []*embed.Embed {
embeds := make([]*embed.Embed, 0)
func EmbedNewPosts(posts []Post, board string) []Embed {
embeds := make([]Embed, 0)
for _, post := range posts {
embeds = append(embeds, embed.NewEmbed().
SetTitle(fmt.Sprintf("New post on: %s %v ", board, emoji.OpenMailboxWithRaisedFlag)).
SetDescription(fmt.Sprintf("**%s**\n%s\n\nhttps://gno.land/r/boards:%s/%d", post.Title, post.Description, board, post.Id)).
SetAuthor(post.Author).
SetColor(0x6e0e08))
embeds = append(embeds, Embed{
Title: fmt.Sprintf("New post on: %s %v ", board, emoji.OpenMailboxWithRaisedFlag),
Description: fmt.Sprintf("**%s**\n%s\n\nhttps://gno.land/r/boards:%s/%d", post.Title, post.Description, board, post.Id),
Author: Author{
Name: post.Author,
IconUrl: "https://cdn.discordapp.com/attachments/981266192390049846/983052513932607488/unknown.png",
},
Color: 7212552,
})
}
fmt.Printf("THERE IS %d NEW POSTS\n", len(embeds))
return embeds
}

func getNewPosts(board string) ([]*embed.Embed, error) {
func getNewPosts(board string) ([]Embed, error) {
// this return the posts from the watched board
BoardPosts, err := getBoardsPosts(board)
if err != nil {
Expand Down
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"github.com/bwmarrin/discordgo"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)

type Body struct {
Embeds []Embed `json:"embeds"`
}

func main() {
err := setup(Boards)

Expand Down Expand Up @@ -39,10 +46,12 @@ func main() {
err = dg.Close()
return
}
// post request on webhook
for _, p := range newPosts {
_, err := dg.ChannelMessageSendEmbed(ChannelID, p.MessageEmbed)
e := Body{Embeds: []Embed{p}}
jsonData, _ := json.Marshal(e)
_, err = http.Post(`https://discord.com/api/webhooks/982746889868935198/CVXU-yDUWej-1WbgIYETpIaehMU1GY37u8hDrYqBZJl4UItt313C-J_t-WQ9L8Ey0wG8`, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
err = dg.Close()
return
}
}
Expand Down

0 comments on commit 502769b

Please sign in to comment.