Beispiel #1
0
func giveBits(cmd *modulebase.ModuleCommand) (string, error) {
	if len(cmd.Args) < 2 {
		return giveBitsHelpString, nil
	}

	amount, err := strconv.Atoi(cmd.Args[0])
	if err != nil {
		return "Invalid amount", nil
	}

	if amount <= 0 {
		return "Amount must be a positive nonzero integer", nil
	}

	userName := strings.Join(cmd.Args[1:], " ")
	user, err := utils.FindUser(cmd.Guild, userName)
	if err != nil {
		return "Unable to find user", nil
	}

	gifter := cmd.Message.Author
	if bits.GetBits(cmd.Guild.ID, gifter.ID) < amount {
		return "Not enough bits", nil
	}

	bits.RemoveBits(cmd.Session, cmd.Guild.ID, gifter.ID, amount, "Gift")
	bits.AddBits(cmd.Session, cmd.Guild.ID, user.ID, amount, "Gift", false)

	message := fmt.Sprintf("Transferred %d bits from %v to %v",
		amount,
		utils.GetPreferredName(cmd.Guild, gifter.ID),
		utils.GetPreferredName(cmd.Guild, user.ID))

	return message, nil
}
Beispiel #2
0
func takeBits(cmd *modulebase.ModuleCommand) (string, error) {
	if len(cmd.Args) < 2 {
		return takeBitsHelpString, nil
	}

	amount, err := strconv.Atoi(cmd.Args[0])
	if err != nil {
		return "Invalid amount", nil
	}

	if amount <= 0 {
		return "Amount must be a positive nonzero integer", nil
	}

	userName := strings.Join(cmd.Args[1:], " ")
	user, err := utils.FindUser(cmd.Guild, userName)
	if err != nil {
		return "Unable to find user", nil
	}

	bits.RemoveBits(cmd.Session, cmd.Guild.ID, user.ID, amount, "Took bits")

	message := fmt.Sprintf("Took %d bits from %v",
		amount,
		utils.GetPreferredName(cmd.Guild, user.ID))

	return message, nil
}
Beispiel #3
0
func showBits(cmd *modulebase.ModuleCommand) (string, error) {
	w := &tabwriter.Writer{}
	buf := &bytes.Buffer{}
	w.Init(buf, 0, 3, 0, ' ', 0)
	fmt.Fprint(w, "```\n")

	if len(cmd.Args) == 0 {
		for _, b := range bits.GetBitsLeaderboard(cmd.Guild.ID, 10) {
			name := utils.GetPreferredName(cmd.Guild, b.UserID)
			if b.Value != nil && *b.Value != 0 {
				fmt.Fprintf(w, "%s: \t %d bits\n", name, *b.Value)
			}
		}
		fmt.Fprint(w, "```\n")
		w.Flush()
		return buf.String(), nil
	}

	var userId string
	if cmd.Args[0] == "me" {
		userId = cmd.Message.Author.ID
	} else if cmd.Args[0] == "help" {
		return showBitsHelpString, nil
	} else {
		return showBitsHelpString, nil
	}

	b := bits.GetBits(cmd.Guild.ID, userId)

	name := utils.GetPreferredName(cmd.Guild, userId)
	fmt.Fprintf(w, "%s: \t %d bits\n", name, b)

	fmt.Fprint(w, "```\n")
	w.Flush()
	return buf.String(), nil
}
Beispiel #4
0
func (r *Round) Render() (im image.Image, err error) {
	guild, _ := r.Session.Guild(r.GuildID)
	if guild == nil {
		log.WithFields(log.Fields{
			"guild": r.GuildID,
		}).Warning("Failed to grab guild")
		return
	}

	dc := gg.NewContext(390, 220)
	dc.DrawCircle(195, 0, 220)
	dc.SetRGB(0.32, 0.63, 0.20)
	dc.Fill()

	dc.SetFontFace(inconsolata.Regular8x16)
	dc.SetRGB(0, 0, 0)

	for idx, player := range r.Players {
		hand := player.Hands[0]
		slot := tableDrawingSlots[idx]

		username := utils.GetPreferredName(guild, player.UserID)

		if len(username) > 14 {
			username = username[:11] + "..."
		}

		dc.DrawStringAnchored(username, slot.NameX, slot.NameY,
			slot.NameAnchorX, slot.NameAnchorY)

		cardX, cardY := slot.CardX, slot.CardY
		for _, card := range hand.Pile.Cards {
			cardIm, err := card.GetImage()
			if err != nil {
				log.Errorf("Error getting card image %v", err)
				continue
			}

			dc.DrawImage(cardIm, cardX, cardY)
			cardX += 10
			cardY += 5
		}
	}

	dc.DrawStringAnchored("Dealer",
		tableDealerDrawingSlot.NameX, tableDealerDrawingSlot.NameY,
		tableDealerDrawingSlot.NameAnchorX, tableDealerDrawingSlot.NameAnchorY)

	cardX, cardY := tableDealerDrawingSlot.CardX, tableDealerDrawingSlot.CardY
	for _, card := range r.Dealer.Hands[0].Pile.Cards {
		cardIm, err := card.GetImage()
		if err != nil {
			log.Errorf("Error getting card image %v", err)
			continue
		}

		dc.DrawImage(cardIm, cardX, cardY)
		cardX += 10
		cardY += 5
	}

	return dc.Image(), nil
}
Beispiel #5
0
func voiceStateUpdateCallback(s *discordgo.Session, v *discordgo.VoiceStateUpdate) {
	log.Debugf("Voice bonus On voice state update: %v", v.VoiceState)

	// Check if it was a part
	if v.ChannelID == "" {
		return
	}

	guild, _ := s.State.Guild(v.GuildID)
	if guild == nil {
		log.WithFields(log.Fields{
			"guild": v.GuildID,
		}).Warning("Failed to grab guild")
		return
	}

	member, _ := s.State.Member(v.GuildID, v.UserID)
	if member == nil {
		log.WithFields(log.Fields{
			"member": member,
		}).Warning("Failed to grab member")
		return
	}

	if member.User.Bot {
		return
	}

	c := voicebonusCollection{ramendb.GetCollection(v.GuildID, ConfigName)}
	if !c.Enabled() {
		return
	}

	span := c.Timespan()
	if span == nil {
		log.Error("Timespan was nil for %v", guild.ID)
		return
	}

	log.Debugf("Timespan is: %v", span)

	currentTime := time.Now().UTC()

	year, month, day := currentTime.Date()
	weekday := currentTime.Weekday()
	nextDay := day + utils.GetDaysTillWeekday(int(weekday), span.Weekday)
	startDate := time.Date(year, month, nextDay, span.Hour, span.Minute, 0, 0, time.UTC)
	previousDate := time.Date(year, month, nextDay-7, span.Hour, span.Minute, 0, 0, time.UTC)

	log.Debugf("Start Date is: %v", startDate)
	log.Debugf("Previous Date is: %v", previousDate)

	lastJoinTime := time.Unix(getUserLastJoin(v.GuildID, v.UserID), 0)

	log.Debugf("Last join date: %v, now: %v", lastJoinTime, currentTime)

	if currentTime.Before(startDate) {
		// Check for previous week parts. Needed if timespan crosses UTC midnight
		if time.Since(previousDate).Hours() > float64(span.Duration) ||
			lastJoinTime.After(previousDate) {

			log.Debugf("Previous time status: %v %v",
				time.Since(previousDate).Hours() > float64(span.Duration),
				lastJoinTime.After(previousDate))
			return
		}
	} else if time.Since(startDate).Hours() > float64(span.Duration) ||
		lastJoinTime.After(startDate) {

		log.Debugf("Current time status: %v %v",
			time.Since(startDate).Hours() > float64(span.Duration),
			lastJoinTime.After(startDate))
		return
	}

	log.Debug("Giving bits for join")
	bits.AddBits(s, v.GuildID, v.UserID, c.Amount(), "Voice join bonus", true)

	username := utils.GetPreferredName(guild, v.UserID)
	message := fmt.Sprintf(joinMessage, username, c.Amount(),
		bits.GetBits(v.GuildID, v.UserID))

	channel, _ := s.UserChannelCreate(v.UserID)
	s.ChannelMessageSend(channel.ID, message)

	updateUserLastJoin(v.GuildID, v.UserID, currentTime.Unix())
}