/*
Global handler for when new guild members join a discord guild. Typically used to welcome them if the server has enabled it.
*/
Global handler for when new guild members join a discord guild. Typically used to welcome them if the server has enabled it.
/ * Global handler for when new guild members join a discord guild . Typically used to welcome them if the server has enabled it . * /
func guildMemberAdd(session *discordgo.Session, member *discordgo.GuildMemberAdd) {
	guild, err := moeDiscord.GetGuild(member.GuildID, session)
	if err != nil {
		session.ChannelMessageSend(masterDebugChannel, fmt.Sprint("Error fetching guild during guild member add", err, member))
		return
	}
	server, err := db.ServerQueryOrInsert(guild.ID)
	if err != nil || !server.Enabled {
		return
	}
	if server.WelcomeMessage.Valid {
		var channelId string
		if server.WelcomeChannel.Valid {
			channelId = server.WelcomeChannel.String
		} else {
			dmChannel, err := session.UserChannelCreate(member.User.ID)
			if err != nil {
				log.Println("ERROR! Unable to make DM channel with userID ", member.User.ID)
				return
			}
			channelId = dmChannel.ID
		}
		session.ChannelMessageSend(channelId, server.WelcomeMessage.String)
	}
	if server.StarterRole.Valid {
		var starterRole *discordgo.Role
		for _, guildRole := range guild.Roles {
			if guildRole.ID == server.StarterRole.String {
				starterRole = guildRole
				break
			}
		}
		if starterRole == nil {
			if server.BotChannel.Valid {
				session.ChannelMessageSend(server.WelcomeChannel.String, "Hello, I couldn't find the starter role for this server! "+
					"Please notify a server admin (Like "+util.UserIdToMention(guild.OwnerID)+") Starter role will be removed.")
			} else if server.WelcomeChannel.Valid {
				session.ChannelMessageSend(server.WelcomeChannel.String, "Hello, I couldn't find the starter role for this server! "+
					"Please notify a server admin (Like "+util.UserIdToMention(guild.OwnerID)+") Starter role will be removed.")
			}
			log.Println("ERROR! Unable to find starter role for guild " + guild.Name + ". Deleting starter role.")
			server.StarterRole.Scan(nil)
			db.ServerFullUpdate(server)
		} else {
			session.GuildMemberRoleAdd(member.GuildID, member.User.ID, starterRole.ID)
		}
	}
}
func guildMemberAdd ( session * discordgo . Session , member * discordgo . GuildMemberAdd ) { guild , err := moeDiscord . GetGuild ( member . GuildID , session ) if err != nil { session . ChannelMessageSend ( masterDebugChannel , fmt . Sprint ( " " , err , member ) ) return } server , err := db . ServerQueryOrInsert ( guild . ID ) if err != nil || ! server . Enabled { return } if server . WelcomeMessage . Valid { var channelId string if server . WelcomeChannel . Valid { channelId = server . WelcomeChannel . String } else { dmChannel , err := session . UserChannelCreate ( member . User . ID ) if err != nil { log . Println ( " " , member . User . ID ) return } channelId = dmChannel . ID } session . ChannelMessageSend ( channelId , server . WelcomeMessage . String ) } if server . StarterRole . Valid { var starterRole * discordgo . Role for _ , guildRole := range guild . Roles { if guildRole . ID == server . StarterRole . String { starterRole = guildRole break } } if starterRole == nil { if server . BotChannel . Valid { session . ChannelMessageSend ( server . WelcomeChannel . String , " " + " " + util . UserIdToMention ( guild . OwnerID ) + " " ) } else if server . WelcomeChannel . Valid { session . ChannelMessageSend ( server . WelcomeChannel . String , " " + " " + util . UserIdToMention ( guild . OwnerID ) + " " ) } log . Println ( " " + guild . Name + " " ) server . StarterRole . Scan ( nil ) db . ServerFullUpdate ( server ) } else { session . GuildMemberRoleAdd ( member . GuildID , member . User . ID , starterRole . ID ) } } }
Global handler for when new guild members join a discord guild.
Global handler for when new guild members join a discord guild .
// only send out a welcome message is the server has one // then decide if we want to PM this welcome or post in a channel // then only assign a starter role if they have one set // couldn't find the starter role, try to let them know and then delete the starter role to prevent this error from appearing again