/*
Global handler for when new messages are sent in any guild. The entry point for commands and other general handling
*/
Global handler for when new messages are sent in any guild. The entry point for commands and other general handling
/ * Global handler for when new messages are sent in any guild . The entry point for commands and other general handling * /
func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate) {
	if message.Author.ID == session.State.User.ID || message.Author.Bot {
		return
	}
	timer := event.StartNamedTimer("channel_start")
	channel, err := moeDiscord.GetChannel(message.ChannelID, session)
	if err != nil {
		log.Println("ERROR! Unable to get guild in messageCreate ", err, channel)
		return
	}
	timer.AddMark("end_channel")
	guild, err := moeDiscord.GetGuild(channel.GuildID, session)
	if err != nil {
		log.Println("ERROR! Unable to get guild in messageCreate ", err, guild)
		return
	}
	timer.AddMark("db_server_start")
	server, err := db.ServerQueryOrInsert(guild.ID)
	if err != nil {
		session.ChannelMessageSend(channel.ID, "Sorry, there was an error fetching this server. This is an issue with moebot not discord. "+
			"Please contact a moebot developer/admin.")
		return
	}
	timer.AddMark("db_server_end")
	timer.AddMark(event.TimerMarkDbBegin + "user_profile")
	userProfile, err := db.UserQueryOrInsert(message.Author.ID)
	if err != nil {
		session.ChannelMessageSend(channel.ID, "Sorry, there was an error fetching your user profile. This is an issue with moebot not discord. "+
			"Please contact a moebot developer/admin.")
		return
	}
	timer.AddMark(event.TimerMarkDbEnd + "user_profile")
	member, err := moeDiscord.GetMember(message.Author.ID, guild.ID, session)
	if err != nil {
		log.Println("ERROR! Unable to get member in messageCreate ", err, message)
		return
	}
	isMaster := checker.IsMaster(message.Author.ID)
	isGuildOwner := permissions.IsGuildOwner(guild, message.Author.ID)
	if !server.Enabled && !isMaster && !isGuildOwner {
		return
	}
	var baseRole *discordgo.Role
	var starterRole *discordgo.Role
	for _, guildRole := range guild.Roles {
		if server.StarterRole.Valid && guildRole.ID == server.StarterRole.String {
			starterRole = guildRole
		}
		if server.BaseRole.Valid && guildRole.ID == server.BaseRole.String {
			baseRole = guildRole
		}
	}
	isNewUser := !isMaster && !isGuildOwner && server.RuleAgreement.Valid && starterRole != nil &&
		util.StrContains(member.Roles, starterRole.ID, util.CaseSensitive)
	if strings.HasPrefix(strings.ToUpper(message.Content), strings.ToUpper(ComPrefix)) {
		if isNewUser {
			session.ChannelMessageSend(channel.ID, "Sorry "+message.Author.Mention()+", but you have to agree to the rules first to use bot commands! "+
				"Check the rules channel or ask an admin for more info.")
			return
		}
		runCommand(session, message.Message, guild, channel, member, &userProfile, &timer)
	}
	if isNewUser {
		sanitizedMessage := util.MakeAlphaOnly(message.Content)
		if strings.HasPrefix(strings.ToUpper(sanitizedMessage), strings.ToUpper(server.RuleAgreement.String)) {
			if baseRole == nil {
				session.ChannelMessageSend(channel.ID, "Hey... this is awkward... It seems like this server's admins setup a rule agreement but no base role. "+
					"Please notify a server admin (Like "+util.UserIdToMention(guild.OwnerID)+") Rule agreement will now be removed.")
				server.RuleAgreement.Scan(nil)
				err = db.ServerFullUpdate(server)
				if err != nil {
					log.Println("Error updateing server", err)
				}
				return
			}
			session.ChannelMessageSend(message.ChannelID, "Welcome "+message.Author.Mention()+"! We hope you enjoy your stay in our Discord server!")
			session.GuildMemberRoleAdd(guild.ID, member.User.ID, baseRole.ID)
			session.GuildMemberRoleRemove(guild.ID, member.User.ID, starterRole.ID)
			log.Println("Updated user <" + member.User.Username + "> after reading the rules")
		}
	}
}
func messageCreate ( session * discordgo . Session , message * discordgo . MessageCreate ) { if message . Author . ID == session . State . User . ID || message . Author . Bot { return } timer := event . StartNamedTimer ( " " ) channel , err := moeDiscord . GetChannel ( message . ChannelID , session ) if err != nil { log . Println ( " " , err , channel ) return } timer . AddMark ( " " ) guild , err := moeDiscord . GetGuild ( channel . GuildID , session ) if err != nil { log . Println ( " " , err , guild ) return } timer . AddMark ( " " ) server , err := db . ServerQueryOrInsert ( guild . ID ) if err != nil { session . ChannelMessageSend ( channel . ID , " " + " " ) return } timer . AddMark ( " " ) timer . AddMark ( event . TimerMarkDbBegin + " " ) userProfile , err := db . UserQueryOrInsert ( message . Author . ID ) if err != nil { session . ChannelMessageSend ( channel . ID , " " + " " ) return } timer . AddMark ( event . TimerMarkDbEnd + " " ) member , err := moeDiscord . GetMember ( message . Author . ID , guild . ID , session ) if err != nil { log . Println ( " " , err , message ) return } isMaster := checker . IsMaster ( message . Author . ID ) isGuildOwner := permissions . IsGuildOwner ( guild , message . Author . ID ) if ! server . Enabled && ! isMaster && ! isGuildOwner { return } var baseRole * discordgo . Role var starterRole * discordgo . Role for _ , guildRole := range guild . Roles { if server . StarterRole . Valid && guildRole . ID == server . StarterRole . String { starterRole = guildRole } if server . BaseRole . Valid && guildRole . ID == server . BaseRole . String { baseRole = guildRole } } isNewUser := ! isMaster && ! isGuildOwner && server . RuleAgreement . Valid && starterRole != nil && util . StrContains ( member . Roles , starterRole . ID , util . CaseSensitive ) if strings . HasPrefix ( strings . ToUpper ( message . Content ) , strings . ToUpper ( ComPrefix ) ) { if isNewUser { session . ChannelMessageSend ( channel . ID , " " + message . Author . Mention ( ) + " " + " " ) return } runCommand ( session , message . Message , guild , channel , member , & userProfile , & timer ) } if isNewUser { sanitizedMessage := util . MakeAlphaOnly ( message . Content ) if strings . HasPrefix ( strings . ToUpper ( sanitizedMessage ) , strings . ToUpper ( server . RuleAgreement . String ) ) { if baseRole == nil { session . ChannelMessageSend ( channel . ID , " " + " " + util . UserIdToMention ( guild . OwnerID ) + " " ) server . RuleAgreement . Scan ( nil ) err = db . ServerFullUpdate ( server ) if err != nil { log . Println ( " " , err ) } return } session . ChannelMessageSend ( message . ChannelID , " " + message . Author . Mention ( ) + " " ) session . GuildMemberRoleAdd ( guild . ID , member . User . ID , baseRole . ID ) session . GuildMemberRoleRemove ( guild . ID , member . User . ID , starterRole . ID ) log . Println ( " " + member . User . Username + " " ) } } }
Global handler for when new messages are sent in any guild.
Global handler for when new messages are sent in any guild .
// bail out if we have any messages we want to ignore such as bot messages // missing channel // If the server is disabled, then don't allow any message processing // HOWEVER, if the user posting the message is this bot's owner or the guild's owner then let it through so they can enable the server // Check if this user is a new user. This will determine what they can/can't do on the server. // Masters and guild owners are never a new user // todo: [rate-limit-spam] should add a check here for command spam // if a starter role requested a command and the server has rule agreements, let them know they can't do that // We don't need to process anything else since by typing a bot command they couldn't type a rule confirmation // In this case we don't care about the error state as the user doesn't need to know we failed to serialize the metric and we already logged it // Disabled for now as we're getting incorrect information in the db... //db.MetricInsertTimer(timer, userProfile) // make sure to also check if they agreed to the rules // Server only had a partial setup (rule agreement + starter role but no base role)