-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,5 @@ const ( | |
) | ||
|
||
const FailedHeartbeatAcks = 5 * time.Millisecond | ||
|
||
const APIBase = "https://discordapp.com/api/v6" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/labstack/gommon/log" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func (s *Shard) GatewayBot() (st *GatewayBotResponse, err error) { | ||
|
||
client := &http.Client{Timeout: time.Second * 10} | ||
|
||
req, err := http.NewRequest("GET", APIBase+"/gateway/bot", nil) | ||
|
||
req.Header.Set("Authorization", "Bot "+s.Token) | ||
|
||
response, err := client.Do(req) | ||
if err != nil { | ||
log.Fatal("Error getting Gateway data: ", err) | ||
return | ||
} | ||
|
||
defer func() { | ||
err := response.Body.Close() | ||
if err != nil { | ||
return | ||
} | ||
}() | ||
|
||
body, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
log.Fatal("error reading gateway response body ", err) | ||
} | ||
|
||
err = json.Unmarshal(body, &st) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Ensure the gateway always has a trailing slash. | ||
// MacOS will fail to connect if we add query params without a trailing slash on the base domain. | ||
if !strings.HasSuffix(st.URL, "/") { | ||
st.URL += "/" | ||
} | ||
|
||
return | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,38 @@ | ||
package main | ||
|
||
import "fmt" | ||
import ( | ||
"fmt" | ||
"github.com/labstack/gommon/log" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
func main() { | ||
fmt.Println("Testing") | ||
log.SetLevel(log.INFO) | ||
token := os.Getenv("TOKEN") | ||
client := Shard{Token: token} | ||
GatewayData, err := client.GatewayBot() | ||
if err != nil { | ||
log.Fatal("Unable to get GatewayBot data") | ||
} | ||
shards := make([]Shard, GatewayData.Shards) | ||
initSequence := int64(0) | ||
for sid, shard := range shards { | ||
shard.Sequence = &initSequence | ||
shard.SessionID = "" | ||
shard.Token = token | ||
shard.ShardCount = GatewayData.Shards | ||
shard.ShardId = sid | ||
_ = shard.Open(GatewayData.URL) | ||
} | ||
|
||
// Wait here until CTRL-C or other term signal is received. | ||
fmt.Println("Bot is now running. Press CTRL-C to exit.") | ||
sc := make(chan os.Signal, 1) | ||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) | ||
<-sc | ||
|
||
// Cleanly close down the Discord session. | ||
_ = client.Close() | ||
} |
This file contains 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
This file contains 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