Exemplo n.º 1
0
func handleSet(cmd *modulebase.ModuleCommand) (string, error) {
	log.Debug("Called handleSet")
	if len(cmd.Args) == 0 || cmd.Args[0] == "help" {
		return vbHelpString, nil
	}

	c := voicebonusCollection{ramendb.GetCollection(cmd.Guild.ID, ConfigName)}
	enable, err := utils.EnableToBool(cmd.Args[0])
	if err != nil {
		return vbHelpString, nil
	}

	err = c.Enable(enable)
	if err != nil {
		errString := fmt.Sprintf("Error updating enable state: %v", err)
		return errString, nil
	}

	var enabledString string
	if enable {
		enabledString = "Enabled"
	} else {
		enabledString = "Disabled"
	}
	return fmt.Sprintf("Voice Bonus %v", enabledString), nil
}
Exemplo n.º 2
0
func handleNickChange(cmd *modulebase.ModuleCommand) (string, error) {
	nicknames := nickCollection{ramendb.GetCollection(cmd.Guild.ID, nickCollName)}
	user := cmd.Message.Author
	guild := cmd.Guild
	s := cmd.Session
	if bits.GetBits(guild.ID, user.ID) < 200 {
		return "**FAILED TO CHANGE NICKNAME:** Insufficient bits.", nil
	}
	if (len(cmd.Args) < 1) || (cmd.Args[0] == "help") {
		return nickHelpString, nil
	}
	nickname := getNickName(cmd.Message.Content)
	upsertdata := bson.M{"$set": nicknameConfig{
		UserID:   user.ID,
		Nickname: nickname,
	}}
	nicknames.Upsert(nicknameConfig{UserID: user.ID}, upsertdata)
	err := s.GuildMemberNickname(guild.ID, user.ID, nickname)
	if err != nil {
		log.Errorf("Failed to update user's nickname: %v", err)
		return "**Failed to update user's nickname**", nil
	}
	bits.RemoveBits(s, guild.ID, user.ID, 200, "Changed nickname to "+nickname)
	return "", nil
}
Exemplo n.º 3
0
func getUserLastJoin(guildId string, userId string) int64 {
	c := ramendb.GetCollection(guildId, ConfigName+"lastjoins")
	result := &voicebonusLastJoin{}
	c.Find(voicebonusLastJoin{UserID: userId}).One(result)
	if result.LastJoin == nil {
		return 0
	}

	return *result.LastJoin
}
Exemplo n.º 4
0
func handleSetAmount(cmd *modulebase.ModuleCommand) (string, error) {
	log.Debug("Called handleSetAmount")
	if len(cmd.Args) == 0 {
		return amountHelpString, nil
	}

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

	c := voicebonusCollection{ramendb.GetCollection(cmd.Guild.ID, ConfigName)}
	err = c.SetAmount(amount)
	if err != nil {
		errString := fmt.Sprintf("Error updating amount: %v", err)
		return errString, nil
	}

	return fmt.Sprintf("Voice Bonus amount set to %v", amount), nil
}
Exemplo n.º 5
0
func handleSetTime(cmd *modulebase.ModuleCommand) (string, error) {
	log.Debug("Called handleSetTime")
	if len(cmd.Args) != 3 || cmd.Args[0] == "help" {
		return timeHelpString, nil
	}

	weekday := utils.ToWeekday(cmd.Args[0])
	time := strings.Split(cmd.Args[1], ":")
	hour, err := strconv.Atoi(time[0])
	if err != nil {
		return "Invalid hour", nil
	}
	minute, err := strconv.Atoi(time[1])
	if err != nil {
		return "Invalid minute", nil
	}

	if !strings.Contains(cmd.Args[2], "h") {
		return "Missing 'h' in duration", nil
	}
	duration, err := strconv.Atoi(strings.Replace(cmd.Args[2], "h", "", 1))
	if err != nil {
		return "Invalid duration", nil
	}

	span := voicebonusTimespan{
		Weekday:  int(weekday),
		Hour:     hour,
		Minute:   minute,
		Duration: duration,
	}

	c := voicebonusCollection{ramendb.GetCollection(cmd.Guild.ID, ConfigName)}
	err = c.SetTimespan(span)

	if err != nil {
		errString := fmt.Sprintf("Error updating time: %v", err)
		return errString, nil
	}
	return fmt.Sprintf("Voice Bonus time set to %v:%v on %v for %v hours", hour, minute, cmd.Args[0], duration), nil
}
Exemplo n.º 6
0
func nickChangeUpdateCallback(s *discordgo.Session, m *discordgo.GuildMemberUpdate) {
	nicknames := nickCollection{ramendb.GetCollection(m.GuildID, nickCollName)}

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

	member := m.Member
	user := member.User
	if member == nil {
		log.WithFields(log.Fields{
			"member": member,
		}).Warning("Failed to grab member")
		return
	}

	if member.User.Bot {
		return
	}

	data := nicknameConfig{}
	nicknames.Find(nicknameConfig{UserID: user.ID}).One(&data)
	trueNick := data.Nickname
	if trueNick == "" {
		log.Debug("No Nickname DB Info")
		return
	}
	if member.Nick != trueNick {
		err := s.GuildMemberNickname(guild.ID, user.ID, trueNick)
		if err != nil {
			log.Errorf("Failed to correct user's nickname: %v", err)
			return
		}
		return
	}
}
Exemplo n.º 7
0
func getBetRollCollection(guildId string) *gamblingCollection {
	return &gamblingCollection{ramendb.GetCollection(guildId, ConfigName+betRollDBSuffix)}
}
Exemplo n.º 8
0
func getRunningRoundCollection(guildId string) *mgo.Collection {
	return ramendb.GetCollection(guildId,
		collectionBaseName+"_"+runningRoundDBSuffix)
}
Exemplo n.º 9
0
func updateUserLastJoin(guildId string, userId string, time int64) {
	c := ramendb.GetCollection(guildId, ConfigName+"lastjoins")
	c.Upsert(voicebonusLastJoin{UserID: userId},
		bson.M{"$set": voicebonusLastJoin{LastJoin: &time}})
}
Exemplo n.º 10
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())
}