Skip to content

Commit

Permalink
Don't try and calculate shards because it's impossible you idiot.
Browse files Browse the repository at this point in the history
  • Loading branch information
iopred committed Sep 17, 2016
1 parent 1fdc012 commit 90922a0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
3 changes: 3 additions & 0 deletions cmd/bruxism/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var discordEmail string
var discordPassword string
var discordApplicationClientID string
var discordOwnerUserID string
var discordShards int
var ircServer string
var ircUsername string
var ircPassword string
Expand All @@ -58,6 +59,7 @@ func init() {
flag.StringVar(&discordPassword, "discordpassword", "", "Discord account password.")
flag.StringVar(&discordOwnerUserID, "discordowneruserid", "", "Discord owner user id.")
flag.StringVar(&discordApplicationClientID, "discordapplicationclientid", "", "Discord application client id.")
flag.IntVar(&discordShards, "discordshards", 1, "Number of discord shards.")
flag.StringVar(&ircServer, "ircserver", "", "IRC server.")
flag.StringVar(&ircUsername, "ircusername", "", "IRC user name.")
flag.StringVar(&ircPassword, "ircpassword", "", "IRC password.")
Expand Down Expand Up @@ -113,6 +115,7 @@ func main() {
}
discord.ApplicationClientID = discordApplicationClientID
discord.OwnerUserID = discordOwnerUserID
discord.Shards = discordShards
bot.RegisterService(discord)

bot.RegisterPlugin(discord, cp)
Expand Down
28 changes: 9 additions & 19 deletions discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type Discord struct {
args []interface{}
messageChan chan Message

Shards int

// The first session, used to send messages (and maintain backwards compatibility).
Session *discordgo.Session
Sessions []*discordgo.Session
Expand Down Expand Up @@ -161,32 +163,20 @@ func (d *Discord) Name() string {

// Open opens the service and returns a channel which all messages will be sent on.
func (d *Discord) Open() (<-chan Message, error) {
var err error

session, err := discordgo.New(d.args...)
if err != nil {
return nil, err
}

numShards := 0

guilds, err := session.UserGuilds()
if err == nil {
numShards = len(guilds) / numGuildsPerShard
shards := d.Shards
if shards < 1 {
shards = 1
}

numShards++
d.Sessions = make([]*discordgo.Session, shards)

d.Sessions = make([]*discordgo.Session, numShards)

for i := 0; i < numShards; i++ {
for i := 0; i < shards; i++ {
session, err := discordgo.New(d.args...)
if err != nil {
return nil, err
}

session.ShardCount = numShards
session.ShardID = 0
session.ShardCount = shards
session.ShardID = i
session.AddHandler(d.onMessageCreate)
session.AddHandler(d.onMessageUpdate)
session.AddHandler(d.onMessageDelete)
Expand Down

0 comments on commit 90922a0

Please sign in to comment.